Skip to content

shaykhullinsergey/AppAny.HotChocolate.FluentValidation

 
 

Repository files navigation

💥 AppAny.HotChocolate.FluentValidation 💥

License Nuget Downloads Tests codecov

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

🔧 Installation 🔧

$> 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, validation strategy support

🚩 Strongly typed ValidationStrategy<T> support

🚩 First-class attribute-based approach support

🎨 Usage 🎨

✅ Add FluentValidation validator

public class ExampleInput
{
  public string Example { get; set; }
}

public class ExampleInputValidator : AbstractValidator<ExampleInput>
{
  public ExampleInputValidator()
  {
    RuleFor(input => input.ExampleProperty)
      .NotEmpty()
      .WithMessage("Property is empty");
  }
}

✅ Configure HotChocolate + FluentValidation integration

services.AddGraphQLServer()
  .AddFluentValidation();

descriptor.Field(x => x.Example(default!))
  // Explicit over implicit preferred
  // You have to add .UseFluentValidation()/attribute to all arguments requiring validation
  .Argument("input", argument => argument.UseFluentValidation());

... Example([UseFluentValidation] ExampleInput input) { ... }

✅ Extend and customize

services.AddGraphQLServer()
  .AddFluentValidation(options =>
  {
    options.SkipValidation(...)
      .UseErrorMapper(...)
      .UseInputValidators(...);
  });

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

... 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 🚧

About

Input field HotChocolate GraphQL + FluentValidation integration

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C# 100.0%