Skip to content

Latest commit

 

History

History
66 lines (54 loc) · 2.15 KB

validation-strategies.md

File metadata and controls

66 lines (54 loc) · 2.15 KB

Validation strategies

This library supports strongly typed FluentValidation.ValidationStrategy configuration

  • By default ValidationDefaults.ValidationStrategies.Default is used. It is doing nothing with validation rules
  • Validation strategies can be configured only on arguments directly
  • Validation strategies is not supporting attribute-based approach out of the box for now
descriptor.Field(x => x.Example(default!))
  .Argument("input", argument => argument.UseFluentValidation(...))

To configure validation strategy use UseValidator/s with two generic parameter overloads (because you need to know not only TValidator, but TInput itself to use with ValidationStrategy<TInput> for strongly typed suggestions)

descriptor.Field(x => x.Example(default!))
  .Argument("input", argument => argument.UseFluentValidation(options =>
  {
    // ValidationStrategy<Example>
    options.UseValidator<Example, ExampleInput>(strategy =>
    {
      strategy.IncludeProperties(input => input.ExampleProperty);
    });
  })))

Dynamic validation strategy

descriptor.Field(x => x.Example(default!))
  .Argument("input", argument => argument.UseFluentValidation(options =>
  {
    // ValidationStrategy<Example>
    options.UseValidationStrategy<Example>((context, strategy) =>
    {
      var strategyService = context.MiddlewareContext.Service<IStrategyService>();

      if (strategyService.ShouldIncludeExampleProperty())
      {
        strategy.IncludeProperties(input => input.ExampleProperty);
      }
    });
  })))

Dynamic validation strategy with custom input validator

descriptor.Field(x => x.Example(default!))
  .Argument("input", argument => argument.UseFluentValidation(options =>
  {
    // ValidationStrategy<Example>
    options.UseValidator<Example, ExampleInput>((context, strategy) =>
    {
      var strategyService = context.MiddlewareContext.Service<IStrategyService>();

      if (strategyService.ShouldIncludeExampleProperty())
      {
        strategy.IncludeProperties(input => input.ExampleProperty);
      }
    });
  })))

Read more on FluentValidation site https://docs.fluentvalidation.net/en/latest/rulesets.html