-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathFluentValidationExtensions.cs
More file actions
91 lines (81 loc) · 2.89 KB
/
FluentValidationExtensions.cs
File metadata and controls
91 lines (81 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
using FluentValidation;
using GraphQL.DI;
using GraphQL.FluentValidation;
using GraphQL.Instrumentation;
using GraphQL.Types;
namespace GraphQL;
/// <summary>
/// Extensions to GraphQL to enable FluentValidation.
/// </summary>
public static partial class FluentValidationExtensions
{
/// <summary>
/// Adds a FieldMiddleware to the GraphQL pipeline that converts a <see cref="ValidationException"/> to <see cref="ExecutionError"/>s./>
/// </summary>
public static ExecutionOptions UseFluentValidation(this ExecutionOptions executionOptions, IValidatorCache validatorTypeCache)
{
validatorTypeCache.Freeze();
executionOptions.SetCache(validatorTypeCache);
return executionOptions;
}
/// <summary>
/// Adds a FieldMiddleware to the GraphQL pipeline that converts a <see cref="ValidationException"/> to <see cref="ExecutionError"/>s./>
/// </summary>
public static void UseFluentValidation(this ISchema schema)
{
ValidationMiddleware validationMiddleware = new();
schema.FieldMiddleware.Use(validationMiddleware);
}
/// <summary>
/// Configures GraphQL to use FluentValidation, using a custom <see cref="IValidatorCache"/>.
/// </summary>
/// <param name="builder">
/// The GraphQL builder.
/// </param>
/// <param name="validatorCache">
/// The cache used to resolve validator types.
/// </param>
/// <returns>
/// The <paramref name="builder"/> instance;
/// </returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="builder"/> is null.
/// -or-
/// <paramref name="validatorCache"/> is null.
/// </exception>
public static IGraphQLBuilder UseFluentValidation(this IGraphQLBuilder builder, IValidatorCache validatorCache)
{
if (builder is null)
{
throw new ArgumentNullException(nameof(builder));
}
if (validatorCache is null)
{
throw new ArgumentNullException(nameof(validatorCache));
}
builder.UseMiddleware<ValidationMiddleware>();
validatorCache.Freeze();
builder.ConfigureExecutionOptions(eo => eo.SetCache(validatorCache));
return builder;
}
/// <summary>
/// Configures GraphQL to use FluentValidation, with validators resolved using dependency injection.
/// </summary>
/// <param name="builder">
/// The GraphQL builder.
/// </param>
/// <returns>
/// The <paramref name="builder"/> instance;
/// </returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="builder"/> is null.
/// </exception>
public static IGraphQLBuilder UseFluentValidation(this IGraphQLBuilder builder)
{
if (builder is null)
{
throw new ArgumentNullException(nameof(builder));
}
return builder.UseFluentValidation(new ValidatorServiceProviderCache());
}
}