Skip to content

Commit

Permalink
Merge pull request #23 from appany/0.1.10
Browse files Browse the repository at this point in the history
Tests, minor refactorings, rename extensions
  • Loading branch information
sergeyshaykhullin authored Feb 1, 2021
2 parents add8d68 + 3d8e45f commit 04fc4d2
Show file tree
Hide file tree
Showing 37 changed files with 220 additions and 347 deletions.
4 changes: 2 additions & 2 deletions src/AppAny.HotChocolate.FluentValidation.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Nullable>enable</Nullable>
<Version>0.1.9</Version>
<Version>0.1.10</Version>
<LangVersion>9</LangVersion>
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<IncludeSymbols>true</IncludeSymbols>
Expand All @@ -24,7 +24,7 @@
</PropertyGroup>

<ItemGroup Label="Packages">
<PackageReference Include="FluentValidation" Version="9.4.0" />
<PackageReference Include="FluentValidation" Version="9.5.0" />
<PackageReference Include="HotChocolate.Execution" Version="11.0.9" />
</ItemGroup>

Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ public static class ArgumentValidationBuilderExtensions
/// Overrides global <see cref="InputValidatorProvider"/>.
/// Uses <see cref="TValidator"/> to resolve <see cref="InputValidator"/>
/// </summary>
public static ArgumentValidationBuilder UseValidator<TValidator>(
this ArgumentValidationBuilder builder)
public static ArgumentValidationBuilder UseValidator<TValidator>(this ArgumentValidationBuilder builder)
where TValidator : class, IValidator
{
return builder.UseInputValidatorProviders(context =>
Expand All @@ -25,8 +24,7 @@ public static ArgumentValidationBuilder UseValidator<TValidator>(
/// Overrides global <see cref="InputValidatorProvider"/>.
/// Uses all <see cref="TValidator"/> to resolve <see cref="InputValidator"/>
/// </summary>
public static ArgumentValidationBuilder UseValidators<TValidator>(
this ArgumentValidationBuilder builder)
public static ArgumentValidationBuilder UseValidators<TValidator>(this ArgumentValidationBuilder builder)
where TValidator : class, IValidator
{
return builder.UseInputValidatorProviders(context =>
Expand Down Expand Up @@ -64,8 +62,7 @@ public static ArgumentValidationBuilder UseValidators(
/// Overrides global <see cref="InputValidatorProvider"/>.
/// Uses <see cref="TValidator"/> to resolve <see cref="InputValidator"/> with <see cref="ValidationDefaults.ValidationStrategies.Default{TInput}"/> strategy
/// </summary>
public static ArgumentValidationBuilder UseValidator<TInput, TValidator>(
this ArgumentValidationBuilder builder)
public static ArgumentValidationBuilder UseValidator<TInput, TValidator>(this ArgumentValidationBuilder builder)
where TValidator : class, IValidator<TInput>
{
return builder.UseValidator<TInput, TValidator>(ValidationDefaults.ValidationStrategies.Default);
Expand All @@ -75,8 +72,7 @@ public static ArgumentValidationBuilder UseValidator<TInput, TValidator>(
/// Overrides global <see cref="InputValidatorProvider"/>.
/// Uses all <see cref="TValidator"/> to resolve <see cref="InputValidator"/> with <see cref="ValidationDefaults.ValidationStrategies.Default{TInput}"/> strategy
/// </summary>
public static ArgumentValidationBuilder UseValidators<TInput, TValidator>(
this ArgumentValidationBuilder builder)
public static ArgumentValidationBuilder UseValidators<TInput, TValidator>(this ArgumentValidationBuilder builder)
where TValidator : class, IValidator<TInput>
{
return builder.UseValidators<TInput, TValidator>(ValidationDefaults.ValidationStrategies.Default);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
using System.Linq;

namespace AppAny.HotChocolate.FluentValidation
{
public static class CanUseErrorMappersExtensions
public static class ValidationBuilderExtensions
{
/// <summary>
/// Always skips validation <see cref="ValidationDefaults.SkipValidation.Skip"/>
/// </summary>
public static TBuilder SkipValidation<TBuilder>(this CanSkipValidation<TBuilder> builder)
{
return builder.SkipValidation(ValidationDefaults.SkipValidation.Skip);
}

/// <summary>
/// Uses default <see cref="ErrorMapper"/>. See <see cref="ValidationDefaults.ErrorMappers.Default"/>
/// </summary>
Expand Down Expand Up @@ -43,5 +53,18 @@ public static TBuilder UseDefaultErrorMapperWithExtendedDetails<TBuilder>(
errorMapper?.Invoke(errorBuilder, context);
});
}

/// <summary>
/// Adds default <see cref="InputValidatorProvider"/>. See <see cref="ValidationDefaults.InputValidatorProviders.Default"/>
/// </summary>
public static TBuilder UseDefaultInputValidatorProvider<TBuilder>(
this CanUseInputValidatorProviders<TBuilder> builder,
params InputValidatorProvider[] inputValidatorProviders)
{
return builder.UseInputValidatorProviders(
new InputValidatorProvider[] { ValidationDefaults.InputValidatorProviders.Default }
.Concat(inputValidatorProviders)
.ToArray());
}
}
}
14 changes: 0 additions & 14 deletions src/Extensions/CanSkipValidationExtensions.cs

This file was deleted.

20 changes: 0 additions & 20 deletions src/Extensions/CanUseInputValidatorProvidersExtensions.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace AppAny.HotChocolate.FluentValidation
{
internal static class DictionaryExtensions
internal static class ExtensionDataExtensions
{
public static IInputField? TryGetArgument(this IDictionary<string, IInputField> arguments, string name)
{
Expand Down Expand Up @@ -61,8 +61,7 @@ public static ArgumentValidationOptions GetOrCreateArgumentOptions(this Extensio
: null;
}

public static ArgumentValidationOptions GetArgumentOptions(
this IReadOnlyDictionary<string, object?> contextData)
public static ArgumentValidationOptions GetArgumentOptions(this IReadOnlyDictionary<string, object?> contextData)
{
return (ArgumentValidationOptions)contextData[ValidationDefaults.ArgumentOptionsKey]!;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace AppAny.HotChocolate.FluentValidation
{
internal static class InputValidatorProviderContextExtensions
internal static class InputFieldExtensions
{
private static readonly ConcurrentDictionary<Type, Type> ArgumentTypeToValidatorType = new();

Expand Down
3 changes: 1 addition & 2 deletions src/InputValidator/InputValidator.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Threading;
using System.Threading.Tasks;
using FluentValidation;
using FluentValidation.Results;
Expand All @@ -8,5 +7,5 @@ namespace AppAny.HotChocolate.FluentValidation
/// <summary>
/// Abstracts <see cref="IValidator"/> execution
/// </summary>
public delegate ValueTask<ValidationResult?> InputValidator(object argument, CancellationToken cancellationToken);
public delegate ValueTask<ValidationResult?> InputValidator(InputValidatorContext inputValidatorContext);
}
16 changes: 16 additions & 0 deletions src/InputValidator/InputValidatorContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.Threading;

namespace AppAny.HotChocolate.FluentValidation
{
public readonly struct InputValidatorContext
{
public InputValidatorContext(object argument, CancellationToken cancellationToken)
{
Argument = argument;
CancellationToken = cancellationToken;
}

public object Argument { get; }
public CancellationToken CancellationToken { get; }
}
}
4 changes: 3 additions & 1 deletion src/Skipping/SkipValidationContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ namespace AppAny.HotChocolate.FluentValidation
{
public readonly struct SkipValidationContext
{
public SkipValidationContext(IMiddlewareContext middlewareContext, IInputField argument)
public SkipValidationContext(
IMiddlewareContext middlewareContext,
IInputField argument)
{
MiddlewareContext = middlewareContext;
Argument = argument;
Expand Down
24 changes: 12 additions & 12 deletions src/ValidationDefaults.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,11 @@ public static class InputValidators
/// </summary>
public static InputValidator FromValidator(IValidator validator)
{
return async (argument, cancellationToken) =>
return async context =>
{
var validationContext = new ValidationContext<object>(argument);
var validationContext = new ValidationContext<object>(context.Argument);

return await validator.ValidateAsync(validationContext, cancellationToken).ConfigureAwait(false);
return await validator.ValidateAsync(validationContext, context.CancellationToken).ConfigureAwait(false);
};
}

Expand All @@ -143,16 +143,16 @@ public static InputValidator FromValidator(IValidator validator)
/// </summary>
public static InputValidator FromValidators(IEnumerable<IValidator> validators)
{
return async (argument, cancellationToken) =>
return async context =>
{
var validationContext = new ValidationContext<object>(argument);
var validationContext = new ValidationContext<object>(context.Argument);

ValidationResult? validationResult = null;

foreach (var validator in validators)
{
var validatorResult = await validator.ValidateAsync(
validationContext, cancellationToken).ConfigureAwait(false);
validationContext, context.CancellationToken).ConfigureAwait(false);

if (validationResult is null)
{
Expand All @@ -178,13 +178,13 @@ public static InputValidator FromValidatorWithStrategy<TInput>(
IValidator<TInput> validator,
Action<ValidationStrategy<TInput>> validationStrategy)
{
return async (argument, cancellationToken) =>
return async context =>
{
var validationContext = ValidationContext<TInput>.CreateWithOptions(
(TInput)argument,
(TInput)context.Argument,
validationStrategy);

return await validator.ValidateAsync(validationContext, cancellationToken).ConfigureAwait(false);
return await validator.ValidateAsync(validationContext, context.CancellationToken).ConfigureAwait(false);
};
}

Expand All @@ -195,18 +195,18 @@ public static InputValidator FromValidatorsWithStrategy<TInput>(
IEnumerable<IValidator<TInput>> validators,
Action<ValidationStrategy<TInput>> validationStrategy)
{
return async (argument, cancellationToken) =>
return async context =>
{
var validationContext = ValidationContext<TInput>.CreateWithOptions(
(TInput)argument,
(TInput)context.Argument,
validationStrategy);

ValidationResult? validationResult = null;

foreach (var validator in validators)
{
var validatorResult = await validator.ValidateAsync(
validationContext, cancellationToken).ConfigureAwait(false);
validationContext, context.CancellationToken).ConfigureAwait(false);

if (validationResult is null)
{
Expand Down
28 changes: 15 additions & 13 deletions src/ValidationFieldMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ public static FieldDelegate Use(FieldDelegate next)

for (var nodeIndex = 0; nodeIndex < argumentNodes.Count; nodeIndex++)
{
var passedArgument = argumentNodes[nodeIndex];
var argumentNode = argumentNodes[nodeIndex];

var argument = objectFieldOptions.Arguments.TryGetArgument(passedArgument.Name.Value);
var argument = objectFieldOptions.Arguments.TryGetArgument(argumentNode.Name.Value);

if (argument is null)
{
Expand All @@ -28,8 +28,11 @@ public static FieldDelegate Use(FieldDelegate next)

var argumentOptions = argument.ContextData.GetArgumentOptions();

if (await argumentOptions.SkipValidation!.Invoke(
new SkipValidationContext(middlewareContext, argument)).ConfigureAwait(false))
var shouldSkipValidation = await argumentOptions.SkipValidation!
.Invoke(new SkipValidationContext(middlewareContext, argument))
.ConfigureAwait(false);

if (shouldSkipValidation)
{
continue;
}
Expand All @@ -45,11 +48,12 @@ public static FieldDelegate Use(FieldDelegate next)
{
var inputValidatorProvider = argumentOptions.InputValidatorProviders[providerIndex];

var inputValidator = inputValidatorProvider.Invoke(
new InputValidatorProviderContext(middlewareContext, argument));
var inputValidator = inputValidatorProvider
.Invoke(new InputValidatorProviderContext(middlewareContext, argument));

var validationResult = await inputValidator.Invoke(
argumentValue, middlewareContext.RequestAborted).ConfigureAwait(false);
var validationResult = await inputValidator
.Invoke(new InputValidatorContext(argumentValue, middlewareContext.RequestAborted))
.ConfigureAwait(false);

if (validationResult?.IsValid is null or true)
{
Expand All @@ -62,11 +66,9 @@ public static FieldDelegate Use(FieldDelegate next)

var errorBuilder = ErrorBuilder.New();

argumentOptions.ErrorMapper!.Invoke(errorBuilder, new ErrorMappingContext(
middlewareContext,
argument,
validationResult,
validationFailure));
argumentOptions.ErrorMapper!.Invoke(
errorBuilder,
new ErrorMappingContext(middlewareContext, argument, validationResult, validationFailure));

middlewareContext.ReportError(errorBuilder.Build());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.12.1" />
<PackageReference Include="BenchmarkDotNet.Diagnostics.Windows" Version="0.12.1" />
<PackageReference Include="FairyBread" Version="4.0.0" />
<PackageReference Include="FluentChoco" Version="2.0.0" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using FairyBread;
using FluentValidation;

namespace AppAny.HotChocolate.FluentValidation.Benchmarks
{
public class TestInputValidator : AbstractValidator<TestInput>
public class TestInputValidator : AbstractValidator<TestInput>, IRequiresOwnScopeValidator
{
public TestInputValidator()
{
Expand Down
Loading

0 comments on commit 04fc4d2

Please sign in to comment.