项目作者: appany

项目描述 :
Input field HotChocolate GraphQL + FluentValidation integration
高级语言: C#
项目地址: git://github.com/appany/AppAny.HotChocolate.FluentValidation.git
创建时间: 2021-01-05T21:06:12Z
项目社区:https://github.com/appany/AppAny.HotChocolate.FluentValidation

开源协议:MIT License

下载


AppAny.HotChocolate.FluentValidation

License
Nuget
Downloads
Tests
codecov

Feature-rich, but simple, fast and memory efficient input field HotChocolate + FluentValidation integration ⚡️

🔧 Installation

  1. $> dotnet add package AppAny.HotChocolate.FluentValidation

💡 Features

🚩 You don’t pay for validation middleware if the field has no validatable inputs

🚩 You are not validating, and even trying to validate empty or not marked as validatable inputs

🚩 Most of extensibility points are just composable delegates

🚩 Fine-tuning of validation for each field: conditional validation skipping, multiple validators or error mappers per input

🚩 Strongly typed ValidationStrategy<T> support

🚩 First-class attribute-based approach support

🎨 Usage

✅ Add FluentValidation validator

  1. public class ExampleInput
  2. {
  3. public string Example { get; set; }
  4. }
  5. public class ExampleInputValidator : AbstractValidator<ExampleInput>
  6. {
  7. public ExampleInputValidator()
  8. {
  9. RuleFor(input => input.ExampleProperty)
  10. .NotEmpty()
  11. .WithMessage("Property is empty");
  12. }
  13. }

✅ Configure HotChocolate + FluentValidation integration

  1. # Since 10.2.0 https://github.com/FluentValidation/FluentValidation/releases/tag/10.2.0
  2. services.AddFluentValidation();
  3. services.AddGraphQLServer()
  4. .AddFluentValidation();
  5. descriptor.Field(x => x.Example(default!))
  6. // Explicit over implicit preferred
  7. // You have to add .UseFluentValidation()/attribute to all arguments requiring validation
  8. .Argument("input", argument => argument.UseFluentValidation());
  9. ... Example([UseFluentValidation] ExampleInput input) { ... }

✅ Extend and customize

  1. services.AddGraphQLServer()
  2. .AddFluentValidation(options =>
  3. {
  4. options.SkipValidation(...)
  5. .UseErrorMapper(...)
  6. .UseInputValidators(...);
  7. });
  8. descriptor.Field(x => x.Example(default!))
  9. .Argument("input", argument => argument.UseFluentValidation(options =>
  10. {
  11. options.SkipValidation(...)
  12. .UseErrorMapper(...)
  13. .UseInputValidators(...)
  14. .UseValidationStrategy(...)
  15. .UseValidator<ExampleInputValidator>()
  16. .UseValidator<ExampleInput, ExampleInputValidator>()
  17. .UseValidator<ExampleInput, ExampleInputValidator>(strategy =>
  18. {
  19. strategy.IncludeProperties(input => input.ExampleProperty);
  20. // ...
  21. });
  22. }));
  23. ... Example([UseFluentValidation, UseValidator((typeof(ExampleInputValidator))] ExampleInput input) { ... }

📝 Docs

♿️ Benchmarks 🚀

🚧 I swear I will check correctness, run these benchmarks on my own environment and only after that I will make conclusions 🚧

Breaking changes

  • From 0.10.x to 0.11.x
    • Replace ValidationDefaults.Interceptors static class with ValidationDefaults.Interceptor property
    • Replace ValidationInterceptors static class with ValidationInterceptor : TypeInterceptor class
  • From 0.9.x to 0.10.x
    • Update HC version to 13 preview
  • From 0.6.x to 0.7.x
    • Default input validator throws InvalidOperationException if argument has [UseFluentValidation], but no validator registered in IServiceCollection