Table of Contents

Interface IValidate

Namespace
Excalibur.Dispatch.Validation
Assembly
Excalibur.Dispatch.dll

Defines a message that can perform self-validation.

public interface IValidate : IDispatchMessage
Extension Methods

Examples

public record CreateOrderCommand(decimal Amount, string Currency) : IDispatchAction, IValidate
{
public ValidationResult Validate()
{
if (Amount <= 0)
return ValidationResult.Failure("Amount must be greater than zero");

if (string.IsNullOrWhiteSpace(Currency))
return ValidationResult.Failure("Currency is required");

return ValidationResult.Success();
}
}

Remarks

Messages implementing this interface can provide custom validation logic beyond what declarative validation (attributes, FluentValidation) can handle. This is useful for:

  • Complex business rule validation
  • Cross-field validation that requires business logic
  • Validation that depends on external state or services
  • Performance-critical validation scenarios

Self-validation is invoked by the validation middleware alongside the other validation sources rather than after them: every applicable source runs and its errors accumulate, so implementing this interface does not suppress the declarative attributes on the same message. Keep validation logic lightweight and avoid external dependencies where possible for better testability and performance.

Methods

Validate()

Performs custom validation logic and returns the validation result.

ValidationResult Validate()

Returns

ValidationResult

A ValidationResult indicating whether validation passed or failed.

Remarks

Implement this method to provide message-specific validation logic. The method should be deterministic and avoid side effects. Consider performance implications as this method may be called frequently. Return ValidationResult.Success() for valid messages or SerializableValidationResult.Failed() with error details for invalid messages.