generated from Avanade/avanade-template
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathValidator.cs
118 lines (108 loc) · 8.56 KB
/
Validator.cs
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Copyright (c) Avanade. Licensed under the MIT License. See https://github.com/Avanade/CoreEx
using CoreEx.Validation.Rules;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections;
using System.Collections.Generic;
namespace CoreEx.Validation
{
/// <summary>
/// Provides access to the validator capabilities.
/// </summary>
public static class Validator
{
/// <summary>
/// Create a <see cref="Validator{TEntity}"/>.
/// </summary>
/// <typeparam name="TEntity">The entity <see cref="Type"/>.</typeparam>
/// <returns>A <see cref="Validator{TEntity}"/>.</returns>
public static Validator<TEntity> Create<TEntity>() where TEntity : class => new();
/// <summary>
/// Create (or get) an instance of the pre-registered validator.
/// </summary>
/// <typeparam name="TValidator">The validator <see cref="Type"/>.</typeparam>
/// <param name="serviceProvider">The <see cref="IServiceProvider"/>; defaults to <see cref="ExecutionContext.ServiceProvider"/> where not specified.</param>
/// <returns>The <typeparamref name="TValidator"/> instance.</returns>
public static TValidator Create<TValidator>(IServiceProvider? serviceProvider = null) where TValidator : IValidatorEx
=> (serviceProvider == null ? ExecutionContext.GetService<TValidator>() : serviceProvider.GetService<TValidator>())
?? throw new InvalidOperationException($"Attempted to get service '{typeof(TValidator).FullName}' but null was returned; this would indicate that the service has not been configured correctly.");
/// <summary>
/// Create <typeparamref name="T"/> value validator (see <see cref="CommonValidator{T}"/>).
/// </summary>
/// <typeparam name="T">The value <see cref="Type"/>.</typeparam>
/// <param name="validator">An action with the <see cref="CommonValidator{T}"/> to enable further configuration.</param>
/// <returns>The <see cref="CommonValidator{T}"/>.</returns>
/// <remarks>This is a synonym for the <see cref="CommonValidator.Create{T}(Action{CommonValidator{T}})"/>.</remarks>
public static CommonValidator<T> CreateFor<T>(Action<CommonValidator<T>>? validator = null) => CommonValidator.Create(validator);
/// <summary>
/// Create a collection-based <see cref="CommonValidator{T}"/> where the <see cref="ICollectionRuleItem"/> can be specified.
/// </summary>
/// <typeparam name="TColl">The collection <see cref="Type"/>.</typeparam>
/// <param name="minCount">The minimum count.</param>
/// <param name="maxCount">The maximum count.</param>
/// <param name="item">The item <see cref="ICollectionRuleItem"/> configuration.</param>
/// <param name="allowNullItems">Indicates whether the underlying collection item must not be null.</param>
/// <returns>The <see cref="CommonValidator{T}"/> for the collection.</returns>
public static CommonValidator<TColl> CreateForCollection<TColl>(int minCount = 0, int? maxCount = null, ICollectionRuleItem? item = null, bool allowNullItems = false) where TColl : class, IEnumerable?
=> CreateFor<TColl>(v => v.Collection(minCount, maxCount, item, allowNullItems));
/// <summary>
/// Create a collection-based <see cref="CommonValidator{T}"/> for the specified <paramref name="itemValidator"/>.
/// </summary>
/// <typeparam name="TColl">The collection <see cref="Type"/>.</typeparam>
/// <typeparam name="TItem">The item <see cref="Type"/>.</typeparam>
/// <param name="itemValidator">The item <see cref="IValidatorEx{T}"/>.</param>
/// <param name="minCount">The minimum count.</param>
/// <param name="maxCount">The maximum count.</param>
/// <param name="allowNullItems">Indicates whether the underlying collection item must not be null.</param>
/// <returns>The <see cref="CommonValidator{T}"/> for the collection.</returns>
public static CommonValidator<TColl> CreateForCollection<TColl, TItem>(IValidatorEx<TItem> itemValidator, int minCount = 0, int? maxCount = null, bool allowNullItems = false) where TColl : class, IEnumerable<TItem>?
=> CreateFor<TColl>(v => v.Collection(itemValidator, minCount, maxCount, allowNullItems));
/// <summary>
/// Create a dictionary-based <see cref="CommonValidator{T}"/> where the <see cref="IDictionaryRuleItem"/> can be specified.
/// </summary>
/// <typeparam name="TDict">The dictionary <see cref="Type"/>.</typeparam>
/// <param name="minCount">The minimum count.</param>
/// <param name="maxCount">The maximum count.</param>
/// <param name="item">The item <see cref="IDictionaryRuleItem"/> configuration.</param>
/// <param name="allowNullKeys">Indicates whether the underlying dictionary key can be null.</param>
/// <param name="allowNullValues">Indicates whether the underlying dictionary value can be null.</param>
/// <returns>The <see cref="CommonValidator{T}"/> for the dictionary.</returns>
public static CommonValidator<TDict> CreateForDictionary<TDict>(int minCount = 0, int? maxCount = null, IDictionaryRuleItem? item = null, bool allowNullKeys = false, bool allowNullValues = false) where TDict : class, IDictionary
=> CreateFor<TDict>(v => v.Dictionary(minCount, maxCount, item, allowNullKeys, allowNullValues));
/// <summary>
/// Create a dictionary-based <see cref="CommonValidator{T}"/> for the specified <paramref name="keyValidator"/> and <paramref name="valueValidator"/>.
/// </summary>
/// <typeparam name="TDict">The dictionary <see cref="Type"/>.</typeparam>
/// <typeparam name="TKey">The key <see cref="Type"/>.</typeparam>
/// <typeparam name="TValue">The value <see cref="Type"/>.</typeparam>
/// <param name="keyValidator">The key <see cref="IValidatorEx{T}"/>.</param>
/// <param name="valueValidator">The value <see cref="IValidatorEx{T}"/>.</param>
/// <param name="minCount">The minimum count.</param>
/// <param name="maxCount">The maximum count.</param>
/// <param name="allowNullKeys">Indicates whether the underlying dictionary key can be null.</param>
/// <param name="allowNullValues">Indicates whether the underlying dictionary value can be null.</param>
/// <returns>The <see cref="CommonValidator{T}"/> for the dictionary.</returns>
public static CommonValidator<TDict> CreateForDictionary<TDict, TKey, TValue>(IValidatorEx<TKey>? keyValidator, IValidatorEx<TValue>? valueValidator, int minCount = 0, int? maxCount = null, bool allowNullKeys = false, bool allowNullValues = false) where TDict : Dictionary<TKey, TValue>? where TKey : notnull
=> CreateFor<TDict>(v => v.Dictionary(keyValidator, valueValidator, minCount, maxCount, allowNullKeys, allowNullValues));
/// <summary>
/// Create a dictionary-based <see cref="CommonValidator{T}"/> for the specified <paramref name="valueValidator"/>.
/// </summary>
/// <typeparam name="TDict">The dictionary <see cref="Type"/>.</typeparam>
/// <typeparam name="TKey">The key <see cref="Type"/>.</typeparam>
/// <typeparam name="TValue">The value <see cref="Type"/>.</typeparam>
/// <param name="valueValidator">The value <see cref="IValidatorEx{T}"/>.</param>
/// <param name="minCount">The minimum count.</param>
/// <param name="maxCount">The maximum count.</param>
/// <param name="allowNullKeys">Indicates whether the underlying dictionary key can be null.</param>
/// <param name="allowNullValues">Indicates whether the underlying dictionary value can be null.</param>
/// <returns>The <see cref="CommonValidator{T}"/> for the dictionary.</returns>
public static CommonValidator<TDict> CreateForDictionary<TDict, TKey, TValue>(IValidatorEx<TValue>? valueValidator, int minCount = 0, int? maxCount = null, bool allowNullKeys = false, bool allowNullValues = false) where TDict : Dictionary<TKey, TValue>? where TKey : notnull
=> CreateFor<TDict>(v => v.Dictionary((IValidatorEx<TKey>?)null, valueValidator, minCount, maxCount, allowNullKeys, allowNullValues));
/// <summary>
/// Creates a <c>null</c> <see cref="IValidatorEx{T}"/>.
/// </summary>
/// <typeparam name="T">The <see cref="Type"/>.</typeparam>
/// <returns>A <c>null</c> <see cref="IValidatorEx{T}"/>; i.e. simply <c>null</c>.</returns>
public static IValidatorEx<T>? Null<T>() => null;
}
}