Steps

  • Create a DTO.
  • Create a custom validator of AbstractValidator type.
  • Create rules.
  • Informed location of custom validators to dot net.
  • Injected validator in controller.
  • Executed ValidatorAsync.
  • Received result.
  • Check result Is not Valid.
  • Return error response.

Validation Result

  • Validation successful – IsValid true.
  • Validation failed – IsValid false.
  • List of validation failure received.

ValidationFailure

  • Property Name
  • Error Message
  • Property Value
  • Custom State
  • Severity
  • Error Code

Model Binding & Custom Validator

  • FluentValidation Custom validator will only run once asp.net model binding is successful.
  • When asp.net model binding is failed, error is stored in ModelState.
  • FluentValidation custom validator is automatically validate the model after model binding by framework when [ApiController] attribute is applied and FluentValidation is registered.