-
Notifications
You must be signed in to change notification settings - Fork 1
7.x #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
7.x #1
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -370,6 +370,20 @@ public void ShouldHaveChildValidator_should_work_with_DependentRules() { | |||
| validator.ShouldHaveChildValidator(x => x.Children, typeof(InlineValidator<Person>)); | ||||
| } | ||||
|
|
||||
| [Fact] | ||||
| public void Allows_only_one_failure_to_match() { | ||||
| var validator = new InlineValidator<Person> { | ||||
| v => v.RuleFor(x => x.Surname).Equal("a").WithErrorCode("nota"), | ||||
| v => v.RuleFor(x => x.Surname).Equal("b").WithErrorCode("notb") | ||||
| }; | ||||
|
|
||||
| var person = new Person() { Surname = "c" }; | ||||
| var result = validator.TestValidate(person); | ||||
|
|
||||
|
||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| #region License | ||
| // Copyright (c) Jeremy Skinner (http://www.jeremyskinner.co.uk) | ||
| // | ||
| // 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/JeremySkinner/FluentValidation | ||
| #endregion | ||
|
|
||
| namespace FluentValidation.Resources { | ||
| using Validators; | ||
|
|
||
| internal class ChineseTraditionalLanguage : Language { | ||
| public override string Name => "zh-TW"; | ||
|
|
||
| public ChineseTraditionalLanguage() { | ||
| Translate<EmailValidator>("'{PropertyName}' 不是有效的電子郵件地址。"); | ||
| Translate<GreaterThanOrEqualValidator>("'{PropertyName}' 必須大於或等於 '{ComparisonValue}'。"); | ||
| Translate<GreaterThanValidator>("'{PropertyName}' 必須大於 '{ComparisonValue}'。"); | ||
| Translate<LengthValidator>("'{PropertyName}' 的長度必須在 {MinLength} 到 {MaxLength} 字符,您輸入了 {TotalLength} 字符。"); | ||
| Translate<MinimumLengthValidator>("'{PropertyName}' 必須大於或等於{MinLength}個字符。您輸入了{TotalLength}個字符。"); | ||
| Translate<MaximumLengthValidator>("'{PropertyName}' 必須小於或等於{MaxLength}個字符。您輸入了{TotalLength}個字符。"); | ||
| Translate<LessThanOrEqualValidator>("'{PropertyName}' 必須小於或等於 '{ComparisonValue}'。"); | ||
| Translate<LessThanValidator>("'{PropertyName}' 必須小於 '{ComparisonValue}'。"); | ||
| Translate<NotEmptyValidator>("'{PropertyName}' 不能為空。"); | ||
| Translate<NotEqualValidator>("'{PropertyName}' 不能和 '{ComparisonValue}' 相等。"); | ||
| Translate<NotNullValidator>("'{PropertyName}' 不能為Null。"); | ||
| Translate<PredicateValidator>("'{PropertyName}' 不符合指定的條件。"); | ||
| Translate<AsyncPredicateValidator>("'{PropertyName}' 不符合指定的條件。"); | ||
| Translate<RegularExpressionValidator>("'{PropertyName}' 的格式不正確。"); | ||
| Translate<EqualValidator>("'{PropertyName}' 應該和 '{ComparisonValue}' 相等。"); | ||
| Translate<ExactLengthValidator>("'{PropertyName}' 必須是 {MaxLength} 個字符,您輸入了 {TotalLength} 字符。"); | ||
| Translate<InclusiveBetweenValidator>("'{PropertyName}' 必須在 {From} (包含)和 {To} (包含)之間, 您輸入了 {Value}。"); | ||
| Translate<ExclusiveBetweenValidator>("'{PropertyName}' 必須在 {From} (不包含)和 {To} (不包含)之間, 您輸入了 {Value}。"); | ||
| Translate<CreditCardValidator>("'{PropertyName}' 不是有效的信用卡號碼。"); | ||
| Translate<ScalePrecisionValidator>("'{PropertyName}' 總位數不能超過 {expectedPrecision} 位,其中小數部份 {expectedScale} 位。您共計輸入了 {digits} 位數字,其中小數部份{actualScale} 位。"); | ||
| Translate<EmptyValidator>("'{PropertyName}' 必須為空。"); | ||
| Translate<NullValidator>("'{PropertyName}' 必須為Null。"); | ||
| Translate<EnumValidator>("'{PropertyName}' 的數值範圍不包含 '{PropertyValue}'。"); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -121,20 +121,22 @@ public static void ShouldNotHaveError<T, TValue>(this TestValidationResult<T, TV | |
| } | ||
|
|
||
| public static IEnumerable<ValidationFailure> When(this IEnumerable<ValidationFailure> failures, Func<ValidationFailure, bool> failurePredicate, string exceptionMessage = null) { | ||
| foreach (var failure in failures) { | ||
| if (!failurePredicate(failure)) { | ||
| string message = "Expected validation error was not found"; | ||
| bool anyMatched = failures.Any(failurePredicate); | ||
|
|
||
| if (exceptionMessage != null) { | ||
| message = exceptionMessage.Replace("{Code}", failure.ErrorCode) | ||
| .Replace("{Message}", failure.ErrorMessage) | ||
| .Replace("{State}", failure.CustomState?.ToString() ?? ""); | ||
| } | ||
| if (!anyMatched) { | ||
| var failure = failures.First(); | ||
|
Comment on lines
+124
to
+127
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. issue (bug_risk): Handling empty failure sequences will throw an unexpected InvalidOperationException instead of the intended ValidationTestException. When |
||
|
|
||
| string message = "Expected validation error was not found"; | ||
|
|
||
| throw new ValidationTestException(message); | ||
| if (exceptionMessage != null) { | ||
| message = exceptionMessage.Replace("{Code}", failure.ErrorCode) | ||
| .Replace("{Message}", failure.ErrorMessage) | ||
| .Replace("{State}", failure.CustomState?.ToString() ?? ""); | ||
| } | ||
| } | ||
|
|
||
| throw new ValidationTestException(message); | ||
| } | ||
|
|
||
| return failures; | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Local scope variable 'validator' shadows ValidatorTesterTester.validator.