-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
CustomValidatorTester.cs
208 lines (173 loc) · 6.34 KB
/
CustomValidatorTester.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#region License
// Copyright (c) .NET Foundation and contributors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// The latest version of this file can be found at https://github.com/FluentValidation/FluentValidation
#endregion
namespace FluentValidation.Tests {
using System.Linq;
using System.Threading.Tasks;
using Xunit;
public class CustomValidatorTester {
private TestValidator validator;
public CustomValidatorTester() {
validator = new TestValidator();
}
[Fact]
public void New_Custom_Returns_single_failure() {
validator
.RuleFor(x => x)
.Custom((x, context) => {
context.AddFailure("Surname", "Fail");
});
var result = validator.Validate(new Person());
result.IsValid.ShouldBeFalse();
result.Errors[0].ErrorMessage.ShouldEqual("Fail");
result.Errors[0].PropertyName.ShouldEqual("Surname");
}
[Fact]
public async Task New_Custom_Returns_single_failure_async() {
validator
.RuleFor(x => x)
.CustomAsync((x, context, cancel) => {
context.AddFailure("Surname", "Fail");
return Task.CompletedTask;
});
var result = await validator.ValidateAsync(new Person());
result.IsValid.ShouldBeFalse();
result.Errors[0].ErrorMessage.ShouldEqual("Fail");
result.Errors[0].PropertyName.ShouldEqual("Surname");
}
[Fact]
public void Perserves_property_chain_using_custom() {
validator.RuleForEach(x => x.Orders).SetValidator(new NestedOrderValidator());
var person = new Person();
person.Orders.Add(new Order());
var result = validator.Validate(person);
result.Errors.Single().PropertyName.ShouldEqual("Orders[0].Amount");
}
[Fact]
public void New_Custom_within_ruleset() {
var validator = new InlineValidator<Person>();
validator.RuleSet("foo", () => {
validator.RuleFor(x => x).Custom((x, ctx) => {
ctx.AddFailure("x", "y");
});
});
validator.RuleSet("bar", () => {
validator.RuleFor(x=>x).Custom((x,ctx) => {
ctx.AddFailure("x", "y");
});
});
var result = validator.Validate(new Person(), v => v.IncludeRuleSets("foo"));
result.Errors.Count.ShouldEqual(1);
}
[Fact]
public async Task New_CustomAsync_within_ruleset() {
var validator = new InlineValidator<Person>();
validator.RuleSet("foo", () => {
validator.RuleFor(x => x).CustomAsync((x, ctx,cancel) => {
ctx.AddFailure("x", "y");
return Task.CompletedTask;
});
});
validator.RuleSet("bar", () => {
validator.RuleFor(x => x).CustomAsync((x, ctx,cancel) => {
ctx.AddFailure("x", "y");
return Task.CompletedTask;
});
});
var result = await validator.ValidateAsync(new Person(), v => v.IncludeRuleSets("foo"));
result.Errors.Count.ShouldEqual(1);
}
[Fact]
public void New_Custom_When_property_name_omitted_infers_property_name() {
validator.RuleFor(x => x.Surname)
.Custom((x, context) => {
context.AddFailure("Error");
});
var result = validator.Validate(new Person());
result.Errors.Single().PropertyName.ShouldEqual("Surname");
}
[Fact]
public void New_Custom_When_property_name_omitted_infers_property_name_nested() {
var addressValidator = new InlineValidator<Address>();
addressValidator.RuleFor(x => x.Line1).Custom((x, ctx) => {
ctx.AddFailure("Error");
});
validator.RuleFor(x => x.Address)
.SetValidator(addressValidator);
var result = validator.Validate(new Person { Address = new Address() });
result.Errors.Single().PropertyName.ShouldEqual("Address.Line1");
}
[Fact]
public void New_custom_uses_empty_property_name_for_model_level_rule() {
validator.RuleFor(x => x).Custom((x, ctx) => ctx.AddFailure("Foo"));
var result = validator.Validate(new Person());
result.Errors.Single().PropertyName.ShouldEqual(string.Empty);
}
[Fact]
public void Throws_when_async_rule_invoked_synchronously() {
validator.RuleFor(x => x.Forename).CustomAsync((x, context, cancel) => {
context.AddFailure("foo");
return Task.CompletedTask;
});
Assert.Throws<AsyncValidatorInvokedSynchronouslyException>(() =>
validator.Validate(new Person()));
}
[Fact]
public async void Runs_sync_rule_asynchronously_when_validator_invoked_asynchronously() {
validator.RuleFor(x => x.Forename).Custom((x, context) => context.AddFailure("foo"));
var result = await validator.ValidateAsync(new Person());
result.Errors.Count.ShouldEqual(1);
}
[Fact]
public void Allows_placeholders() {
validator.RuleFor(x => x.Forename).Custom((name, context) => {
context.MessageFormatter.AppendArgument("Foo", "1");
context.AddFailure("{Foo}");
});
var result = validator.Validate(new Person());
result.Errors.Single().ErrorMessage.ShouldEqual("1");
}
[Fact]
public void Allows_conditions() {
validator.RuleFor(x => x.Forename).Custom((name, ctx) => {
ctx.AddFailure("foo");
}).When(x => x.Age < 18);
var result = validator.Validate(new Person() {Age = 17});
result.IsValid.ShouldBeFalse();
result = validator.Validate(new Person() {Age = 18});
result.IsValid.ShouldBeTrue();
}
[Fact]
public async Task Allows_conditions_async() {
validator.RuleFor(x => x.Forename).CustomAsync((name, ctx, ct) => {
ctx.AddFailure("foo");
return Task.CompletedTask;
}).WhenAsync((x, ct) => Task.FromResult(x.Age < 18));
var result = await validator.ValidateAsync(new Person() {Age = 17});
result.IsValid.ShouldBeFalse();
result = await validator.ValidateAsync(new Person() {Age = 18});
result.IsValid.ShouldBeTrue();
}
private class NestedOrderValidator : AbstractValidator<Order> {
public NestedOrderValidator() {
RuleFor(x=>x).Custom((x, ctx) => {
ctx.AddFailure("Amount", "bar");
});
}
}
}
}