Skip to content

Commit a2b0242

Browse files
committed
Refactor validation framework for consistency and extensibility
Validation rule API and error reporting improvements - RuleSet API refactored to use helper methods for standardized problem creation and message formatting, centralizing error reporting logic. - Property name and display name handling unified with new helper methods for consistent error messages. - Rules class expanded with constants for rule names and metadata keys, enabling structured annotation of validation problems. - Nested collection validation methods renamed and enhanced to include item index in property chains for better error traceability. - Problem chaining in nested validations now includes item index for improved diagnostics. - Resource and localization files updated: renamed "DoesNotContainMessageTemplate" to "NotContainMessageTemplate", added new templates for rules, and regenerated designer code. - Predicate method renamed from DoesNotContain to NotContain for naming consistency. - Validation method signatures updated to use [NotNullWhen(true)] for improved nullability and static analysis. - Removed redundant code in favor of new helper methods, streamlining validation logic.
1 parent bece593 commit a2b0242

8 files changed

Lines changed: 457 additions & 417 deletions

File tree

RoyalCode.SmartValidations.Tests/NestedCollectionTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public void AllNested_NullCollection_NoProblems()
116116
public bool HasProblems([NotNullWhen(true)] out Problems? problems)
117117
{
118118
return Rules.Set<OrderColl>()
119-
.AllNested(Addresses, address => Rules.Set<AddressColl>()
119+
.Nested(Addresses, address => Rules.Set<AddressColl>()
120120
.WithPropertyPrefix("address")
121121
.NotEmpty(address.Street)
122122
.NotEmpty(address.City)
@@ -154,8 +154,8 @@ public bool HasProblems([NotNullWhen(true)] out Problems? problems)
154154
{
155155
return Rules.Set<FooColl>()
156156
.NotEmpty(Value)
157-
.AllNested(Bars)
158-
.AllNested(Bazes, b => b.HasProblems)
157+
.Nested(Bars)
158+
.Nested(Bazes, b => b.HasProblems)
159159
.HasProblems(out problems);
160160
}
161161
}

RoyalCode.SmartValidations.Tests/ValidateTests.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using RoyalCode.SmartProblems;
2+
using System.Diagnostics.CodeAnalysis;
23

34
namespace RoyalCode.SmartValidations.Tests;
45

@@ -159,7 +160,7 @@ public void Validate_StructCollection_Null_NoProblems()
159160
public int Quantity { get; set; }
160161
public decimal Price { get; set; }
161162

162-
public bool HasProblems(out Problems? problems)
163+
public bool HasProblems([NotNullWhen(true)] out Problems? problems)
163164
{
164165
return Rules.Set<Product>()
165166
.NotEmpty(Name)
@@ -173,7 +174,7 @@ public bool HasProblems(out Problems? problems)
173174
{
174175
public string? Title { get; set; }
175176
public Product[]? Products { get; set; }
176-
public bool HasProblems(out Problems? problems)
177+
public bool HasProblems([NotNullWhen(true)] out Problems? problems)
177178
{
178179
return Rules.Set<Catalog>()
179180
.NotEmpty(Title)

RoyalCode.SmartValidations/BuildInPredicates.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public static bool Contains(string? value, string substring, StringComparison co
123123
/// Validates whether the specified string does not contain the given substring using the comparison option.
124124
/// </summary>
125125
[MethodImpl(MethodImplOptions.AggressiveInlining)]
126-
public static bool DoesNotContain(string? value, string substring, StringComparison comparison = StringComparison.Ordinal)
126+
public static bool NotContain(string? value, string substring, StringComparison comparison = StringComparison.Ordinal)
127127
=> value is null || !value.Contains(substring, comparison);
128128

129129
/// <summary>

RoyalCode.SmartValidations/R.Designer.cs

Lines changed: 10 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

RoyalCode.SmartValidations/R.pt-BR.resx

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@
129129
<data name="ContainsMessageTemplate" xml:space="preserve">
130130
<value>O campo "{0}" deve conter o valor "{1}"</value>
131131
</data>
132-
<data name="DoesNotContainMessageTemplate" xml:space="preserve">
132+
<data name="NotContainMessageTemplate" xml:space="preserve">
133133
<value>O campo "{0}" não deve conter o valor "{1}"</value>
134134
</data>
135135
<data name="EmailMessageTemplate" xml:space="preserve">
@@ -192,6 +192,9 @@
192192
<data name="NotNullOrEmptyMessageTemplate" xml:space="preserve">
193193
<value>O campo "{0}" deve ser preenchido</value>
194194
</data>
195+
<data name="NoWhiteSpaceMessageTemplate" xml:space="preserve">
196+
<value>O campo '{0}' não deve ter espaços em branco</value>
197+
</data>
195198
<data name="NullOrLengthMessageTemplate" xml:space="preserve">
196199
<value>O campo "{0}", quando preenchido, deve ter um comprimento entre "{1}" e "{2}" caracteres</value>
197200
</data>
@@ -213,22 +216,19 @@
213216
<data name="NullOrNotMessageTemplate" xml:space="preserve">
214217
<value>O campo "{0}", quando preenchido, deve conter um valor</value>
215218
</data>
216-
<data name="StartsWithMessageTemplate" xml:space="preserve">
217-
<value>O campo "{0}" deve começar com "{1}"</value>
218-
</data>
219-
<data name="UrlMessageTemplate" xml:space="preserve">
220-
<value>O campo "{0}" deve ser um URL válido</value>
219+
<data name="OnlyDigitsMessageTemplate" xml:space="preserve">
220+
<value>O campo '{0}' deve ter apenas dígitos (números)</value>
221221
</data>
222222
<data name="OnlyLettersMessageTemplate" xml:space="preserve">
223223
<value>O campo '{0}' deve ter apenas letras</value>
224224
</data>
225-
<data name="OnlyDigitsMessageTemplate" xml:space="preserve">
226-
<value>O campo '{0}' deve ter apenas dígitos (números)</value>
227-
</data>
228225
<data name="OnlyLettersOrDigitsMessageTemplate" xml:space="preserve">
229226
<value>O campo '{0}' deve ter apenas letras ou dígitos (números)</value>
230227
</data>
231-
<data name="NoWhiteSpaceMessageTemplate" xml:space="preserve">
232-
<value>O campo '{0}' não deve ter espaços em branco</value>
228+
<data name="StartsWithMessageTemplate" xml:space="preserve">
229+
<value>O campo "{0}" deve começar com "{1}"</value>
230+
</data>
231+
<data name="UrlMessageTemplate" xml:space="preserve">
232+
<value>O campo "{0}" deve ser um URL válido</value>
233233
</data>
234234
</root>

RoyalCode.SmartValidations/R.resx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@
129129
<data name="ContainsMessageTemplate" xml:space="preserve">
130130
<value>The '{0}' field must contains '{1}'</value>
131131
</data>
132-
<data name="DoesNotContainMessageTemplate" xml:space="preserve">
132+
<data name="NotContainMessageTemplate" xml:space="preserve">
133133
<value>The '{0}' field must not contains '{1}'</value>
134134
</data>
135135
<data name="EmailMessageTemplate" xml:space="preserve">

0 commit comments

Comments
 (0)