From aa2eaefaf1764ac00dc8f972829fbfcce7e91813 Mon Sep 17 00:00:00 2001 From: Sergey Shaykhullin <46970457+sergeyshaykhullin@users.noreply.github.com> Date: Thu, 20 May 2021 23:20:08 +0300 Subject: [PATCH] Extract steps (#73) * Add nested Steps type * Run full cleanup --- .../UseFluentValidationAttribute.cs | 4 +- src/Builders/ArgumentValidationBuilder.cs | 2 +- src/Builders/Core/CanSkipValidation.cs | 2 +- src/Builders/Core/CanUseErrorMapper.cs | 2 +- src/Builders/Core/CanUseInputValidators.cs | 2 +- .../UseValidationStrategyExtensions.cs | 14 +- .../Extensions/UseValidatorExtensions.cs | 28 +-- ...lidatorWithValidationStrategyExtensions.cs | 84 ++++---- .../Extensions/ValidationBuilderExtensions.cs | 12 +- src/Builders/ValidationBuilder.cs | 2 +- .../ArgumentDescriptorExtensions.cs | 2 +- src/Extensions/ArgumentsExtensions.cs | 2 +- src/Extensions/ExtensionDataExtensions.cs | 2 +- .../RequestExecutorBuilderExtensions.cs | 2 +- src/MapError/ErrorMappingContext.cs | 6 +- src/MapError/MapError.cs | 2 +- src/Options/ObjectFieldValidationOptions.cs | 4 +- src/SkipValidation/SkipValidation.cs | 2 +- src/SkipValidation/SkipValidationContext.cs | 4 +- .../Extensions/UseInputValidatorExtensions.cs | 2 +- src/ValidateInput/GetArgumentValue.cs | 2 +- src/ValidateInput/GetValidationContext.cs | 2 +- src/ValidateInput/GetValidationResult.cs | 2 +- src/ValidateInput/GetValidatorType.cs | 2 +- src/ValidateInput/InputValidatorContext.cs | 4 +- src/ValidateInput/ValidateInput.cs | 2 +- src/ValidationDefaults.cs | 187 +++++++++--------- .../Core/BenchmarkSetup.cs | 14 +- .../EmptyInputsValidationBenchmarks.cs | 2 +- .../MultipleArgumentsBenchmarks.cs | 4 +- .../NullInputsValidationBenchmarks.cs | 2 +- .../ScopedValidationBenchmarks.cs | 2 +- .../SingletonValidationBenchmarks.cs | 2 +- .../Core/TestSetup.cs | 30 +-- 34 files changed, 225 insertions(+), 212 deletions(-) diff --git a/src/Attributes/UseFluentValidationAttribute.cs b/src/Attributes/UseFluentValidationAttribute.cs index d53f01e..ba74963 100644 --- a/src/Attributes/UseFluentValidationAttribute.cs +++ b/src/Attributes/UseFluentValidationAttribute.cs @@ -6,8 +6,8 @@ namespace AppAny.HotChocolate.FluentValidation { /// - /// Use default validation options. - /// To override options use Code-first approach + /// Use default validation options. + /// To override options use Code-first approach /// [AttributeUsage(AttributeTargets.Parameter)] public sealed class UseFluentValidationAttribute : ArgumentDescriptorAttribute diff --git a/src/Builders/ArgumentValidationBuilder.cs b/src/Builders/ArgumentValidationBuilder.cs index 7dfe23c..339e7b4 100644 --- a/src/Builders/ArgumentValidationBuilder.cs +++ b/src/Builders/ArgumentValidationBuilder.cs @@ -3,7 +3,7 @@ namespace AppAny.HotChocolate.FluentValidation { /// - /// Configures input field validation options + /// Configures input field validation options /// public interface ArgumentValidationBuilder : CanSkipValidation, diff --git a/src/Builders/Core/CanSkipValidation.cs b/src/Builders/Core/CanSkipValidation.cs index 1cb35a2..1bbb69a 100644 --- a/src/Builders/Core/CanSkipValidation.cs +++ b/src/Builders/Core/CanSkipValidation.cs @@ -3,7 +3,7 @@ namespace AppAny.HotChocolate.FluentValidation public interface CanSkipValidation { /// - /// Implementation specific. Can add or override + /// Implementation specific. Can add or override /// TBuilder SkipValidation(SkipValidation skipValidation); } diff --git a/src/Builders/Core/CanUseErrorMapper.cs b/src/Builders/Core/CanUseErrorMapper.cs index 859c987..f585477 100644 --- a/src/Builders/Core/CanUseErrorMapper.cs +++ b/src/Builders/Core/CanUseErrorMapper.cs @@ -3,7 +3,7 @@ namespace AppAny.HotChocolate.FluentValidation public interface CanUseErrorMapper { /// - /// Implementation specific. Can add or override + /// Implementation specific. Can add or override /// TBuilder UseErrorMapper(MapError errorMapper); } diff --git a/src/Builders/Core/CanUseInputValidators.cs b/src/Builders/Core/CanUseInputValidators.cs index 5e878d8..6de186f 100644 --- a/src/Builders/Core/CanUseInputValidators.cs +++ b/src/Builders/Core/CanUseInputValidators.cs @@ -3,7 +3,7 @@ namespace AppAny.HotChocolate.FluentValidation public interface CanUseInputValidators { /// - /// Implementation specific. Can add or override + /// Implementation specific. Can add or override /// TBuilder UseInputValidators(params ValidateInput[] inputValidators); } diff --git a/src/Builders/Extensions/UseValidationStrategyExtensions.cs b/src/Builders/Extensions/UseValidationStrategyExtensions.cs index f44f787..aae7c3b 100644 --- a/src/Builders/Extensions/UseValidationStrategyExtensions.cs +++ b/src/Builders/Extensions/UseValidationStrategyExtensions.cs @@ -6,7 +6,7 @@ namespace AppAny.HotChocolate.FluentValidation public static class UseValidationStrategyExtensions { /// - /// Overrides . + /// Overrides . /// public static ArgumentValidationBuilder UseValidationStrategy( this ArgumentValidationBuilder builder, @@ -16,7 +16,7 @@ public static ArgumentValidationBuilder UseValidationStrategy( } /// - /// Overrides . + /// Overrides . /// public static ArgumentValidationBuilder UseValidationStrategy( this ArgumentValidationBuilder builder, @@ -26,7 +26,7 @@ public static ArgumentValidationBuilder UseValidationStrategy( } /// - /// Overrides . + /// Overrides . /// public static ArgumentValidationBuilder UseValidationStrategy( this ArgumentValidationBuilder builder, @@ -36,17 +36,17 @@ public static ArgumentValidationBuilder UseValidationStrategy( } /// - /// Overrides . + /// Overrides . /// public static ArgumentValidationBuilder UseValidationStrategy( this ArgumentValidationBuilder builder, Action> validationStrategy) { return builder.UseInputValidator( - ValidationDefaults.InputValidators.ArgumentValue, - ValidationDefaults.InputValidators.ValidationContextWithStrategy(validationStrategy)!, + ValidationDefaults.InputValidators.Steps.ArgumentValue, + ValidationDefaults.InputValidators.Steps.ValidationContextWithStrategy(validationStrategy)!, inputValidatorContext => inputValidatorContext.Argument.GetGenericValidatorType(), - ValidationDefaults.InputValidators.Validators); + ValidationDefaults.InputValidators.Steps.Validators); } } } diff --git a/src/Builders/Extensions/UseValidatorExtensions.cs b/src/Builders/Extensions/UseValidatorExtensions.cs index 5b42fa3..78f4852 100644 --- a/src/Builders/Extensions/UseValidatorExtensions.cs +++ b/src/Builders/Extensions/UseValidatorExtensions.cs @@ -6,8 +6,8 @@ namespace AppAny.HotChocolate.FluentValidation public static class UseValidatorExtensions { /// - /// Overrides global . - /// Uses to resolve + /// Overrides global . + /// Uses to resolve /// public static ArgumentValidationBuilder UseValidator(this ArgumentValidationBuilder builder) where TValidator : class, IValidator @@ -16,8 +16,8 @@ public static ArgumentValidationBuilder UseValidator(this ArgumentVa } /// - /// Overrides global . - /// Uses all to resolve + /// Overrides global . + /// Uses all to resolve /// public static ArgumentValidationBuilder UseValidators(this ArgumentValidationBuilder builder) where TValidator : class, IValidator @@ -26,33 +26,33 @@ public static ArgumentValidationBuilder UseValidators(this ArgumentV } /// - /// Overrides global . - /// Uses type to resolve + /// Overrides global . + /// Uses type to resolve /// public static ArgumentValidationBuilder UseValidator( this ArgumentValidationBuilder builder, Type validatorType) { return builder.UseInputValidator( - ValidationDefaults.InputValidators.ArgumentValue, - ValidationDefaults.InputValidators.ValidationContext, + ValidationDefaults.InputValidators.Steps.ArgumentValue, + ValidationDefaults.InputValidators.Steps.ValidationContext, _ => validatorType, - ValidationDefaults.InputValidators.Validator); + ValidationDefaults.InputValidators.Steps.Validator); } /// - /// Overrides global . - /// Uses type to resolve + /// Overrides global . + /// Uses type to resolve /// public static ArgumentValidationBuilder UseValidators( this ArgumentValidationBuilder builder, Type validatorType) { return builder.UseInputValidator( - ValidationDefaults.InputValidators.ArgumentValue, - ValidationDefaults.InputValidators.ValidationContext, + ValidationDefaults.InputValidators.Steps.ArgumentValue, + ValidationDefaults.InputValidators.Steps.ValidationContext, _ => validatorType, - ValidationDefaults.InputValidators.Validators); + ValidationDefaults.InputValidators.Steps.Validators); } } } diff --git a/src/Builders/Extensions/UseValidatorWithValidationStrategyExtensions.cs b/src/Builders/Extensions/UseValidatorWithValidationStrategyExtensions.cs index 5e7911b..551194d 100644 --- a/src/Builders/Extensions/UseValidatorWithValidationStrategyExtensions.cs +++ b/src/Builders/Extensions/UseValidatorWithValidationStrategyExtensions.cs @@ -7,8 +7,8 @@ namespace AppAny.HotChocolate.FluentValidation public static class UseValidatorWithValidationStrategyExtensions { /// - /// Overrides global . - /// Uses to resolve with + /// Overrides global . + /// Uses to resolve with /// public static ArgumentValidationBuilder UseValidator( this ArgumentValidationBuilder builder, @@ -19,8 +19,8 @@ public static ArgumentValidationBuilder UseValidator( } /// - /// Overrides global . - /// Uses to resolve with + /// Overrides global . + /// Uses to resolve with /// public static ArgumentValidationBuilder UseValidator( this ArgumentValidationBuilder builder, @@ -31,8 +31,8 @@ public static ArgumentValidationBuilder UseValidator( } /// - /// Overrides global . - /// Uses all to resolve with + /// Overrides global . + /// Uses all to resolve with /// public static ArgumentValidationBuilder UseValidators( this ArgumentValidationBuilder builder, @@ -43,8 +43,8 @@ public static ArgumentValidationBuilder UseValidators( } /// - /// Overrides global . - /// Uses all to resolve with + /// Overrides global . + /// Uses all to resolve with /// public static ArgumentValidationBuilder UseValidators( this ArgumentValidationBuilder builder, @@ -55,8 +55,8 @@ public static ArgumentValidationBuilder UseValidators( } /// - /// Overrides global . - /// Uses type to resolve with + /// Overrides global . + /// Uses type to resolve with /// public static ArgumentValidationBuilder UseValidator( this ArgumentValidationBuilder builder, @@ -67,8 +67,8 @@ public static ArgumentValidationBuilder UseValidator( } /// - /// Overrides global . - /// Uses type to resolve with + /// Overrides global . + /// Uses type to resolve with /// public static ArgumentValidationBuilder UseValidator( this ArgumentValidationBuilder builder, @@ -79,8 +79,8 @@ public static ArgumentValidationBuilder UseValidator( } /// - /// Overrides global . - /// Uses type to resolve + /// Overrides global . + /// Uses type to resolve /// public static ArgumentValidationBuilder UseValidators( this ArgumentValidationBuilder builder, @@ -91,8 +91,8 @@ public static ArgumentValidationBuilder UseValidators( } /// - /// Overrides global . - /// Uses type to resolve + /// Overrides global . + /// Uses type to resolve /// public static ArgumentValidationBuilder UseValidators( this ArgumentValidationBuilder builder, @@ -103,8 +103,9 @@ public static ArgumentValidationBuilder UseValidators( } /// - /// Overrides global . - /// Uses to resolve with strategy + /// Overrides global . + /// Uses to resolve with + /// strategy /// public static ArgumentValidationBuilder UseValidator(this ArgumentValidationBuilder builder) where TValidator : class, IValidator @@ -113,8 +114,9 @@ public static ArgumentValidationBuilder UseValidator(this Ar } /// - /// Overrides global . - /// Uses all to resolve with strategy + /// Overrides global . + /// Uses all to resolve with + /// strategy /// public static ArgumentValidationBuilder UseValidators(this ArgumentValidationBuilder builder) where TValidator : class, IValidator @@ -123,8 +125,9 @@ public static ArgumentValidationBuilder UseValidators(this A } /// - /// Overrides global . - /// Uses to resolve , with custom + /// Overrides global . + /// Uses to resolve , with custom + /// /// public static ArgumentValidationBuilder UseValidator( this ArgumentValidationBuilder builder, @@ -135,8 +138,9 @@ public static ArgumentValidationBuilder UseValidator( } /// - /// Overrides global . - /// Uses to resolve , with custom + /// Overrides global . + /// Uses to resolve , with custom + /// /// public static ArgumentValidationBuilder UseValidator( this ArgumentValidationBuilder builder, @@ -147,8 +151,9 @@ public static ArgumentValidationBuilder UseValidator( } /// - /// Overrides global . - /// Uses all to resolve , with custom + /// Overrides global . + /// Uses all to resolve , with custom + /// /// public static ArgumentValidationBuilder UseValidators( this ArgumentValidationBuilder builder, @@ -159,8 +164,9 @@ public static ArgumentValidationBuilder UseValidators( } /// - /// Overrides global . - /// Uses all to resolve , with custom + /// Overrides global . + /// Uses all to resolve , with custom + /// /// public static ArgumentValidationBuilder UseValidators( this ArgumentValidationBuilder builder, @@ -170,11 +176,9 @@ public static ArgumentValidationBuilder UseValidators( return builder.UseValidators(typeof(TValidator), validationStrategy); } - - /// - /// Overrides global . - /// Uses type to resolve with + /// Overrides global . + /// Uses type to resolve with /// public static ArgumentValidationBuilder UseValidator( this ArgumentValidationBuilder builder, @@ -182,15 +186,15 @@ public static ArgumentValidationBuilder UseValidator( Action> validationStrategy) { return builder.UseInputValidator( - ValidationDefaults.InputValidators.ArgumentValue, - ValidationDefaults.InputValidators.ValidationContextWithStrategy(validationStrategy)!, + ValidationDefaults.InputValidators.Steps.ArgumentValue, + ValidationDefaults.InputValidators.Steps.ValidationContextWithStrategy(validationStrategy)!, _ => validatorType, - ValidationDefaults.InputValidators.Validator); + ValidationDefaults.InputValidators.Steps.Validator); } /// - /// Overrides global . - /// Uses type to resolve + /// Overrides global . + /// Uses type to resolve /// public static ArgumentValidationBuilder UseValidators( this ArgumentValidationBuilder builder, @@ -198,10 +202,10 @@ public static ArgumentValidationBuilder UseValidators( Action> validationStrategy) { return builder.UseInputValidator( - ValidationDefaults.InputValidators.ArgumentValue, - ValidationDefaults.InputValidators.ValidationContextWithStrategy(validationStrategy)!, + ValidationDefaults.InputValidators.Steps.ArgumentValue, + ValidationDefaults.InputValidators.Steps.ValidationContextWithStrategy(validationStrategy)!, _ => validatorType, - ValidationDefaults.InputValidators.Validators); + ValidationDefaults.InputValidators.Steps.Validators); } } } diff --git a/src/Builders/Extensions/ValidationBuilderExtensions.cs b/src/Builders/Extensions/ValidationBuilderExtensions.cs index c1a061c..10bbded 100644 --- a/src/Builders/Extensions/ValidationBuilderExtensions.cs +++ b/src/Builders/Extensions/ValidationBuilderExtensions.cs @@ -5,7 +5,7 @@ namespace AppAny.HotChocolate.FluentValidation public static class ValidationBuilderExtensions { /// - /// Always skips validation + /// Always skips validation /// public static TBuilder SkipValidation(this CanSkipValidation builder) { @@ -13,7 +13,7 @@ public static TBuilder SkipValidation(this CanSkipValidation } /// - /// Uses default . See + /// Uses default . See /// public static TBuilder UseDefaultErrorMapper( this CanUseErrorMapper builder, @@ -27,7 +27,8 @@ public static TBuilder UseDefaultErrorMapper( } /// - /// Adds default with details. See and + /// Adds default with details. See and + /// /// public static TBuilder UseDefaultErrorMapperWithDetails( this CanUseErrorMapper builder, @@ -41,7 +42,8 @@ public static TBuilder UseDefaultErrorMapperWithDetails( } /// - /// Adds default with details. See and + /// Adds default with details. See and + /// /// public static TBuilder UseDefaultErrorMapperWithExtendedDetails( this CanUseErrorMapper builder, @@ -55,7 +57,7 @@ public static TBuilder UseDefaultErrorMapperWithExtendedDetails( } /// - /// Adds default . See + /// Adds default . See /// public static TBuilder UseDefaultInputValidator( this CanUseInputValidators builder, diff --git a/src/Builders/ValidationBuilder.cs b/src/Builders/ValidationBuilder.cs index 4778ce7..ceb3a45 100644 --- a/src/Builders/ValidationBuilder.cs +++ b/src/Builders/ValidationBuilder.cs @@ -1,7 +1,7 @@ namespace AppAny.HotChocolate.FluentValidation { /// - /// Configures global validation options + /// Configures global validation options /// public interface ValidationBuilder : CanSkipValidation, diff --git a/src/Extensions/ArgumentDescriptorExtensions.cs b/src/Extensions/ArgumentDescriptorExtensions.cs index ba4007e..67cf6a7 100644 --- a/src/Extensions/ArgumentDescriptorExtensions.cs +++ b/src/Extensions/ArgumentDescriptorExtensions.cs @@ -6,7 +6,7 @@ namespace AppAny.HotChocolate.FluentValidation public static class ArgumentDescriptorExtensions { /// - /// Configures argument for validation + /// Configures argument for validation /// public static IArgumentDescriptor UseFluentValidation( this IArgumentDescriptor argumentDescriptor, diff --git a/src/Extensions/ArgumentsExtensions.cs b/src/Extensions/ArgumentsExtensions.cs index 0aa118c..49fc395 100644 --- a/src/Extensions/ArgumentsExtensions.cs +++ b/src/Extensions/ArgumentsExtensions.cs @@ -1,6 +1,6 @@ -using HotChocolate.Types; using System.Collections.Generic; using System.Runtime.CompilerServices; +using HotChocolate.Types; namespace AppAny.HotChocolate.FluentValidation { diff --git a/src/Extensions/ExtensionDataExtensions.cs b/src/Extensions/ExtensionDataExtensions.cs index 1307a2e..ae75e67 100644 --- a/src/Extensions/ExtensionDataExtensions.cs +++ b/src/Extensions/ExtensionDataExtensions.cs @@ -1,6 +1,6 @@ -using HotChocolate; using System.Collections.Generic; using System.Runtime.CompilerServices; +using HotChocolate; namespace AppAny.HotChocolate.FluentValidation { diff --git a/src/Extensions/RequestExecutorBuilderExtensions.cs b/src/Extensions/RequestExecutorBuilderExtensions.cs index ad323c2..08a1791 100644 --- a/src/Extensions/RequestExecutorBuilderExtensions.cs +++ b/src/Extensions/RequestExecutorBuilderExtensions.cs @@ -7,7 +7,7 @@ namespace AppAny.HotChocolate.FluentValidation public static class RequestExecutorBuilderExtensions { /// - /// Adds default validation services with overrides + /// Adds default validation services with overrides /// public static IRequestExecutorBuilder AddFluentValidation( this IRequestExecutorBuilder builder, diff --git a/src/MapError/ErrorMappingContext.cs b/src/MapError/ErrorMappingContext.cs index 4986da6..9178072 100644 --- a/src/MapError/ErrorMappingContext.cs +++ b/src/MapError/ErrorMappingContext.cs @@ -1,11 +1,11 @@ -using HotChocolate.Types; -using HotChocolate.Resolvers; using FluentValidation.Results; +using HotChocolate.Resolvers; +using HotChocolate.Types; namespace AppAny.HotChocolate.FluentValidation { /// - /// Context for + /// Context for /// public readonly ref struct ErrorMappingContext { diff --git a/src/MapError/MapError.cs b/src/MapError/MapError.cs index 7ce326a..0cde61c 100644 --- a/src/MapError/MapError.cs +++ b/src/MapError/MapError.cs @@ -3,7 +3,7 @@ namespace AppAny.HotChocolate.FluentValidation { /// - /// Maps validation result to properties + /// Maps validation result to properties /// public delegate void MapError(IErrorBuilder errorBuilder, ErrorMappingContext mappingContext); } diff --git a/src/Options/ObjectFieldValidationOptions.cs b/src/Options/ObjectFieldValidationOptions.cs index 6a9f24c..f6970dc 100644 --- a/src/Options/ObjectFieldValidationOptions.cs +++ b/src/Options/ObjectFieldValidationOptions.cs @@ -1,6 +1,6 @@ -using HotChocolate.Types; -using System.Collections.Generic; using System.Collections.Concurrent; +using System.Collections.Generic; +using HotChocolate.Types; namespace AppAny.HotChocolate.FluentValidation { diff --git a/src/SkipValidation/SkipValidation.cs b/src/SkipValidation/SkipValidation.cs index 26a7ed5..8983f52 100644 --- a/src/SkipValidation/SkipValidation.cs +++ b/src/SkipValidation/SkipValidation.cs @@ -3,7 +3,7 @@ namespace AppAny.HotChocolate.FluentValidation { /// - /// Checks for validation skip + /// Checks for validation skip /// public delegate ValueTask SkipValidation(SkipValidationContext skipValidationContext); } diff --git a/src/SkipValidation/SkipValidationContext.cs b/src/SkipValidation/SkipValidationContext.cs index f27ad0f..3230778 100644 --- a/src/SkipValidation/SkipValidationContext.cs +++ b/src/SkipValidation/SkipValidationContext.cs @@ -1,10 +1,10 @@ -using HotChocolate.Types; using HotChocolate.Resolvers; +using HotChocolate.Types; namespace AppAny.HotChocolate.FluentValidation { /// - /// Context for + /// Context for /// public readonly struct SkipValidationContext { diff --git a/src/ValidateInput/Extensions/UseInputValidatorExtensions.cs b/src/ValidateInput/Extensions/UseInputValidatorExtensions.cs index 2f5a682..258a481 100644 --- a/src/ValidateInput/Extensions/UseInputValidatorExtensions.cs +++ b/src/ValidateInput/Extensions/UseInputValidatorExtensions.cs @@ -6,7 +6,7 @@ namespace AppAny.HotChocolate.FluentValidation public static class UseInputValidatorExtensions { /// - /// Core extension to generalize input validation + /// Core extension to generalize input validation /// public static ArgumentValidationBuilder UseInputValidator( this ArgumentValidationBuilder builder, diff --git a/src/ValidateInput/GetArgumentValue.cs b/src/ValidateInput/GetArgumentValue.cs index 375043a..7c5b006 100644 --- a/src/ValidateInput/GetArgumentValue.cs +++ b/src/ValidateInput/GetArgumentValue.cs @@ -1,7 +1,7 @@ namespace AppAny.HotChocolate.FluentValidation { /// - /// Abstracts argument value resolving + /// Abstracts argument value resolving /// public delegate TInput GetArgumentValue(InputValidatorContext inputValidatorContext); } diff --git a/src/ValidateInput/GetValidationContext.cs b/src/ValidateInput/GetValidationContext.cs index e5c5ae1..0af735f 100644 --- a/src/ValidateInput/GetValidationContext.cs +++ b/src/ValidateInput/GetValidationContext.cs @@ -3,7 +3,7 @@ namespace AppAny.HotChocolate.FluentValidation { /// - /// Abstracts argument value resolving + /// Abstracts argument value resolving /// public delegate IValidationContext GetValidationContext( InputValidatorContext inputValidatorContext, diff --git a/src/ValidateInput/GetValidationResult.cs b/src/ValidateInput/GetValidationResult.cs index 90f20c6..1232783 100644 --- a/src/ValidateInput/GetValidationResult.cs +++ b/src/ValidateInput/GetValidationResult.cs @@ -6,7 +6,7 @@ namespace AppAny.HotChocolate.FluentValidation { /// - /// Abstracts resolving + /// Abstracts resolving /// public delegate Task GetValidationResult( InputValidatorContext inputValidatorContext, diff --git a/src/ValidateInput/GetValidatorType.cs b/src/ValidateInput/GetValidatorType.cs index ea1d4fc..02b18a8 100644 --- a/src/ValidateInput/GetValidatorType.cs +++ b/src/ValidateInput/GetValidatorType.cs @@ -3,7 +3,7 @@ namespace AppAny.HotChocolate.FluentValidation { /// - /// Abstracts validator type resolving + /// Abstracts validator type resolving /// public delegate Type GetValidatorType(InputValidatorContext inputValidatorContext); } diff --git a/src/ValidateInput/InputValidatorContext.cs b/src/ValidateInput/InputValidatorContext.cs index f68c116..39686d7 100644 --- a/src/ValidateInput/InputValidatorContext.cs +++ b/src/ValidateInput/InputValidatorContext.cs @@ -1,10 +1,10 @@ -using HotChocolate.Types; using HotChocolate.Resolvers; +using HotChocolate.Types; namespace AppAny.HotChocolate.FluentValidation { /// - /// Context for + /// Context for /// public readonly struct InputValidatorContext { diff --git a/src/ValidateInput/ValidateInput.cs b/src/ValidateInput/ValidateInput.cs index d397bed..802fe22 100644 --- a/src/ValidateInput/ValidateInput.cs +++ b/src/ValidateInput/ValidateInput.cs @@ -5,7 +5,7 @@ namespace AppAny.HotChocolate.FluentValidation { /// - /// Abstracts execution + /// Abstracts execution /// public delegate Task ValidateInput(InputValidatorContext inputValidatorContext); } diff --git a/src/ValidationDefaults.cs b/src/ValidationDefaults.cs index d5a741e..c88a993 100644 --- a/src/ValidationDefaults.cs +++ b/src/ValidationDefaults.cs @@ -1,10 +1,10 @@ using System; -using System.Threading.Tasks; using System.Runtime.CompilerServices; -using HotChocolate; +using System.Threading.Tasks; using FluentValidation; -using FluentValidation.Results; using FluentValidation.Internal; +using FluentValidation.Results; +using HotChocolate; using HotChocolate.Configuration; using HotChocolate.Resolvers; using Microsoft.Extensions.DependencyInjection; @@ -14,27 +14,27 @@ namespace AppAny.HotChocolate.FluentValidation public static class ValidationDefaults { /// - /// Default key for + /// Default key for /// public const string ValidationOptionsKey = "ValidationOptions"; /// - /// Default key for + /// Default key for /// public const string ArgumentOptionsKey = "ArgumentValidationOptions"; /// - /// Default key for + /// Default key for /// public const string ObjectFieldOptionsKey = "ObjectFieldValidationOptions"; /// - /// Default validation field middleware + /// Default validation field middleware /// public static FieldMiddleware Middleware { get; } = ValidationMiddlewares.Field; /// - /// Default HotChocolate interceptors + /// Default HotChocolate interceptors /// public static class Interceptors { @@ -43,7 +43,7 @@ public static class Interceptors } /// - /// Default graphql error extensions keys + /// Default graphql error extensions keys /// public static class ExtensionKeys { @@ -58,12 +58,12 @@ public static class ExtensionKeys } /// - /// Default implementations + /// Default implementations /// public static class SkipValidation { /// - /// Default implementation. Never skips validation + /// Default implementation. Never skips validation /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public static ValueTask Default(SkipValidationContext skipValidationContext) @@ -72,7 +72,7 @@ public static ValueTask Default(SkipValidationContext skipValidationContex } /// - /// Always skip implementation + /// Always skip implementation /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public static ValueTask Skip(SkipValidationContext skipValidationContext) @@ -82,12 +82,12 @@ public static ValueTask Skip(SkipValidationContext skipValidationContext) } /// - /// Default implementations + /// Default implementations /// public static class ErrorMappers { /// - /// Maps graphql error code, path and message + /// Maps graphql error code, path and message /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void Default(IErrorBuilder errorBuilder, ErrorMappingContext mappingContext) @@ -99,7 +99,7 @@ public static void Default(IErrorBuilder errorBuilder, ErrorMappingContext mappi } /// - /// Maps useful extensions about input field, property, used validator, invalid value and severity + /// Maps useful extensions about input field, property, used validator, invalid value and severity /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void Details(IErrorBuilder errorBuilder, ErrorMappingContext mappingContext) @@ -112,7 +112,7 @@ public static void Details(IErrorBuilder errorBuilder, ErrorMappingContext mappi } /// - /// Maps custom state and formatted message placeholder values + /// Maps custom state and formatted message placeholder values /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void Extended(IErrorBuilder errorBuilder, ErrorMappingContext mappingContext) @@ -127,120 +127,127 @@ public static void Extended(IErrorBuilder errorBuilder, ErrorMappingContext mapp } /// - /// Default implementations + /// Default implementations /// public static class InputValidators { /// - /// Default implementation + /// Default implementation /// public static Task Default(InputValidatorContext inputValidatorContext) { - var argumentValue = ArgumentValue(inputValidatorContext); + var argumentValue = Steps.ArgumentValue(inputValidatorContext); if (argumentValue is null) { return Task.FromResult(null); } - var validationContext = ValidationContext(inputValidatorContext, argumentValue); + var validationContext = Steps.ValidationContext(inputValidatorContext, argumentValue); var validatorType = inputValidatorContext.Argument.GetGenericValidatorType(); - return Validators(inputValidatorContext, validationContext, validatorType); + return Steps.Validators(inputValidatorContext, validationContext, validatorType); } /// - /// Default implementation + /// Default steps implementations /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static TInput? ArgumentValue(InputValidatorContext inputValidatorContext) + public static class Steps { - return inputValidatorContext - .MiddlewareContext - .ArgumentValue(inputValidatorContext.Argument.Name); - } + /// + /// Default implementation + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static TInput? ArgumentValue(InputValidatorContext inputValidatorContext) + { + return inputValidatorContext + .MiddlewareContext + .ArgumentValue(inputValidatorContext.Argument.Name); + } - /// - /// Default implementation - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static IValidationContext ValidationContext( - InputValidatorContext inputValidatorContext, - TInput argumentValue) - { - return new ValidationContext(argumentValue); - } + /// + /// Default implementation + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static IValidationContext ValidationContext( + InputValidatorContext inputValidatorContext, + TInput argumentValue) + { + return new ValidationContext(argumentValue); + } - /// - /// Default implementation with - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static GetValidationContext ValidationContextWithStrategy( - Action> validationStrategy) - { - return (inputValidatorContext, argumentValue) => + /// + /// Default implementation with + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static GetValidationContext ValidationContextWithStrategy( + Action> validationStrategy) { - // TODO: Hacks - return global::FluentValidation.ValidationContext.CreateWithOptions( - argumentValue, - strategy => validationStrategy(inputValidatorContext, strategy)); - }; - } + return (inputValidatorContext, argumentValue) => + { + // TODO: Hacks + return global::FluentValidation.ValidationContext.CreateWithOptions( + argumentValue, + strategy => validationStrategy(inputValidatorContext, strategy)); + }; + } - /// - /// Default implementation - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static Task Validator( - InputValidatorContext inputValidatorContext, - IValidationContext validationContext, - Type validatorType) - { - var validator = (IValidator)inputValidatorContext.MiddlewareContext - .Services - .GetRequiredService(validatorType); + /// + /// Default implementation + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Task Validator( + InputValidatorContext inputValidatorContext, + IValidationContext validationContext, + Type validatorType) + { + var validator = (IValidator)inputValidatorContext.MiddlewareContext + .Services + .GetRequiredService(validatorType); - return validator.ValidateAsync(validationContext, inputValidatorContext.MiddlewareContext.RequestAborted); - } + return validator.ValidateAsync(validationContext, inputValidatorContext.MiddlewareContext.RequestAborted); + } - /// - /// Default implementation for multiple - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static async Task Validators( - InputValidatorContext inputValidatorContext, - IValidationContext validationContext, - Type validatorType) - { - var validators = (IValidator[])inputValidatorContext.MiddlewareContext.Services.GetServices(validatorType); + /// + /// Default implementation for multiple + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static async Task Validators( + InputValidatorContext inputValidatorContext, + IValidationContext validationContext, + Type validatorType) + { + var validators = (IValidator[])inputValidatorContext.MiddlewareContext.Services.GetServices(validatorType); - ValidationResult? validationResult = null; + ValidationResult? validationResult = null; - for (var validatorIndex = 0; validatorIndex < validators.Length; validatorIndex++) - { - var validator = validators[validatorIndex]; + for (var validatorIndex = 0; validatorIndex < validators.Length; validatorIndex++) + { + var validator = validators[validatorIndex]; - var validatorResult = await validator - .ValidateAsync(validationContext, inputValidatorContext.MiddlewareContext.RequestAborted) - .ConfigureAwait(false); + var validatorResult = await validator + .ValidateAsync(validationContext, inputValidatorContext.MiddlewareContext.RequestAborted) + .ConfigureAwait(false); - // Shared ValidationResult - validationResult = validatorResult; - } + // Shared ValidationResult + validationResult = validatorResult; + } - return validationResult; + return validationResult; + } } } /// - /// Default implementations + /// Default implementations /// public static class ValidationStrategies { /// - /// Doing nothing by default. - /// To override validation strategy use + /// Doing nothing by default. + /// To override validation strategy use + /// /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void Default(ValidationStrategy validationStrategy) diff --git a/tests/AppAny.HotChocolate.FluentValidation.Benchmarks/Core/BenchmarkSetup.cs b/tests/AppAny.HotChocolate.FluentValidation.Benchmarks/Core/BenchmarkSetup.cs index 6e155d3..481f051 100644 --- a/tests/AppAny.HotChocolate.FluentValidation.Benchmarks/Core/BenchmarkSetup.cs +++ b/tests/AppAny.HotChocolate.FluentValidation.Benchmarks/Core/BenchmarkSetup.cs @@ -8,13 +8,6 @@ namespace AppAny.HotChocolate.FluentValidation.Benchmarks { public static class BenchmarkSetup { - public static class Mutations - { - public const string WithEmptyName = "mutation { test(input: { name: \"\" }) }"; - public const string WithEmptyInput = "mutation { test() }"; - public const string WithNullInput = "mutation { test(input: null) }"; - } - public static ValueTask CreateRequestExecutor(Action configure) { var services = new ServiceCollection(); @@ -25,5 +18,12 @@ public static ValueTask CreateRequestExecutor(Action CreateRequestExecutor( + Action configureExecutor, + Action? configureServices = null) + { + var services = new ServiceCollection(); + + var executorBuilder = services.AddGraphQL().AddQueryType(); + + configureExecutor.Invoke(executorBuilder); + + configureServices?.Invoke(services); + + return executorBuilder.BuildRequestExecutorAsync(); + } + public static class Mutations { public const string WithEmptyName = "mutation { test(input: { name: \"\" }) }"; @@ -27,20 +42,5 @@ public static string WithAddress(string address) return $"mutation {{ test(input: {{ name: \"\", address: \"{address}\" }}) }}"; } } - - public static ValueTask CreateRequestExecutor( - Action configureExecutor, - Action? configureServices = null) - { - var services = new ServiceCollection(); - - var executorBuilder = services.AddGraphQL().AddQueryType(); - - configureExecutor.Invoke(executorBuilder); - - configureServices?.Invoke(services); - - return executorBuilder.BuildRequestExecutorAsync(); - } } }