Skip to content

Commit 95a686f

Browse files
Fix null-check to use localization properly (#130)
1 parent 41429cb commit 95a686f

File tree

38 files changed

+650
-95
lines changed

38 files changed

+650
-95
lines changed

src/Immediate.Validations.Generators/Models.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public sealed record ValidationTargetProperty
2525
public required string TypeFullName { get; init; }
2626
public required bool IsReferenceType { get; init; }
2727
public required bool IsNullable { get; init; }
28-
public required bool ValidateNotNull { get; init; }
28+
public required PropertyValidation? ValidateNotNull { get; init; }
2929
public required bool IsValidationProperty { get; init; }
3030
public required string? ValidationTypeFullName { get; init; }
3131
public required ValidationTargetProperty? CollectionPropertyDetails { get; init; }

src/Immediate.Validations.Generators/Templates/Validations.sbntxt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,12 @@ partial {{ class.type }} {{ class.name }}
122122
{{~ if p.validate_not_null ~}}
123123
errors.Add(
124124
$"{{ get_prop_name(p.property_name, depth) }}",
125-
$"'{{ get_prop_name(p.name, depth) }}' must not be null."
125+
{{ if string.empty p.validate_not_null.message; p.validate_not_null.validator_name + ".DefaultMessage"; else; p.validate_not_null.message; end }},
126+
new()
127+
{
128+
["PropertyName"] = $"{{ get_prop_name(p.name, depth) }}",
129+
["PropertyValue"] = null,
130+
}
126131
);
127132
{{~ end ~}}
128133

src/Immediate.Validations.Generators/ValidateTargetTransformer.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,12 @@ ImmutableArray<AttributeData> attributes
191191
: propertyType.IsNullableType();
192192

193193
var validateNotNull = !isNullable || attributes.Any(a => a.AttributeClass.IsNotNullAttribute());
194+
var validateMessage = attributes
195+
.FirstOrDefault(a => a.AttributeClass.IsNotNullAttribute())
196+
?.NamedArguments
197+
.FirstOrDefault(a => a.Key == "Message") is { Value: { Value: { } } value }
198+
? value.ToCSharpString()
199+
: null;
194200

195201
var baseType = propertyType.IsNullableType()
196202
? ((INamedTypeSymbol)propertyType).TypeArguments[0]
@@ -280,7 +286,17 @@ ImmutableArray<AttributeData> attributes
280286
TypeFullName = propertyType.ToDisplayString(s_fullyQualifiedPlusNullable),
281287
IsReferenceType = isReferenceType,
282288
IsNullable = isNullable,
283-
ValidateNotNull = validateNotNull,
289+
290+
ValidateNotNull = validateNotNull
291+
? new()
292+
{
293+
Message = validateMessage,
294+
IsNullable = true,
295+
Arguments = [],
296+
IsGenericMethod = false,
297+
ValidatorName = "global::Immediate.Validations.Shared.NotNullAttribute",
298+
}
299+
: null,
284300

285301
IsValidationProperty = isValidationProperty,
286302
ValidationTypeFullName = isValidationProperty

tests/Immediate.Validations.FunctionalTests/IntegrationTests/CustomLocalizerTests.cs

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ public sealed partial record ValidateRecord : IValidationTarget<ValidateRecord>
1414
public required int Id { get; init; }
1515
}
1616

17+
[Validate]
18+
public sealed partial record NotNullRecord : IValidationTarget<NotNullRecord>
19+
{
20+
public required string Value { get; init; }
21+
}
22+
1723
[Test]
1824
public void EnLocalizedMessage()
1925
{
@@ -28,7 +34,7 @@ public void EnLocalizedMessage()
2834
new()
2935
{
3036
PropertyName = "Id",
31-
ErrorMessage = "'Id' must not be empty.",
37+
ErrorMessage = "'Id' must be greater than '0'. - resx",
3238
},
3339
],
3440
errors
@@ -49,7 +55,49 @@ public void FrLocalizedMessage()
4955
new()
5056
{
5157
PropertyName = "Id",
52-
ErrorMessage = "'Id' ne doit pas être vide.",
58+
ErrorMessage = "'Id' doit être supérieur à '0'. - resx",
59+
},
60+
],
61+
errors
62+
);
63+
}
64+
65+
[Test]
66+
public void EnLocalizedNotNullMessage()
67+
{
68+
using var scope = new LocalizerScope("en-US");
69+
70+
var record = new NotNullRecord { Value = null! };
71+
72+
var errors = NotNullRecord.Validate(record);
73+
74+
Assert.Equal(
75+
[
76+
new()
77+
{
78+
PropertyName = "Value",
79+
ErrorMessage = "'Value' must not be null. - resx",
80+
},
81+
],
82+
errors
83+
);
84+
}
85+
86+
[Test]
87+
public void FrLocalizedNotNullMessage()
88+
{
89+
using var scope = new LocalizerScope("fr-CA");
90+
91+
var record = new NotNullRecord { Value = null! };
92+
93+
var errors = NotNullRecord.Validate(record);
94+
95+
Assert.Equal(
96+
[
97+
new()
98+
{
99+
PropertyName = "Value",
100+
ErrorMessage = "'Value' ne doit pas être nul. - resx",
53101
},
54102
],
55103
errors

tests/Immediate.Validations.FunctionalTests/IntegrationTests/DefaultLocalizerTests.cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ public sealed partial record ValidateRecord : IValidationTarget<ValidateRecord>
1313
public required int Id { get; init; }
1414
}
1515

16+
[Validate]
17+
public sealed partial record NotNullRecord : IValidationTarget<NotNullRecord>
18+
{
19+
public required string Value { get; init; }
20+
}
21+
1622
[Test]
1723
public void EnLocalizedMessage()
1824
{
@@ -54,6 +60,48 @@ public void FrLocalizedMessage()
5460
errors
5561
);
5662
}
63+
64+
[Test]
65+
public void EnLocalizedNotNullMessage()
66+
{
67+
using var scope = new CultureScope("en-US");
68+
69+
var record = new NotNullRecord { Value = null! };
70+
71+
var errors = NotNullRecord.Validate(record);
72+
73+
Assert.Equal(
74+
[
75+
new()
76+
{
77+
PropertyName = "Value",
78+
ErrorMessage = "'Value' must not be null.",
79+
},
80+
],
81+
errors
82+
);
83+
}
84+
85+
[Test]
86+
public void FrLocalizedNotNullMessage()
87+
{
88+
using var scope = new CultureScope("fr-CA");
89+
90+
var record = new NotNullRecord { Value = null! };
91+
92+
var errors = NotNullRecord.Validate(record);
93+
94+
Assert.Equal(
95+
[
96+
new()
97+
{
98+
PropertyName = "Value",
99+
ErrorMessage = "'Value' ne doit pas être nul.",
100+
},
101+
],
102+
errors
103+
);
104+
}
57105
}
58106

59107
public sealed class CultureScope : IDisposable

tests/Immediate.Validations.FunctionalTests/Resources/Validators.Designer.cs

Lines changed: 10 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 124 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,126 @@
1-
<?xml version="1.0" encoding="utf-8"?>
2-
1+
<?xml version="1.0" encoding="utf-8"?>
32
<root>
4-
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
5-
<xsd:element name="root" msdata:IsDataSet="true">
6-
7-
</xsd:element>
8-
</xsd:schema>
9-
<resheader name="resmimetype">
10-
<value>text/microsoft-resx</value>
11-
</resheader>
12-
<resheader name="version">
13-
<value>1.3</value>
14-
</resheader>
15-
<resheader name="reader">
16-
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
17-
</resheader>
18-
<resheader name="writer">
19-
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
20-
</resheader>
21-
<data name="GreaterThanAttribute" xml:space="preserve">
22-
<value>'{PropertyName}' ne doit pas être vide.</value>
23-
</data>
3+
<!--
4+
Microsoft ResX Schema
5+
6+
Version 2.0
7+
8+
The primary goals of this format is to allow a simple XML format
9+
that is mostly human readable. The generation and parsing of the
10+
various data types are done through the TypeConverter classes
11+
associated with the data types.
12+
13+
Example:
14+
15+
... ado.net/XML headers & schema ...
16+
<resheader name="resmimetype">text/microsoft-resx</resheader>
17+
<resheader name="version">2.0</resheader>
18+
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
19+
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
20+
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
21+
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
22+
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
23+
<value>[base64 mime encoded serialized .NET Framework object]</value>
24+
</data>
25+
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
26+
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
27+
<comment>This is a comment</comment>
28+
</data>
29+
30+
There are any number of "resheader" rows that contain simple
31+
name/value pairs.
32+
33+
Each data row contains a name, and value. The row also contains a
34+
type or mimetype. Type corresponds to a .NET class that support
35+
text/value conversion through the TypeConverter architecture.
36+
Classes that don't support this are serialized and stored with the
37+
mimetype set.
38+
39+
The mimetype is used for serialized objects, and tells the
40+
ResXResourceReader how to depersist the object. This is currently not
41+
extensible. For a given mimetype the value must be set accordingly:
42+
43+
Note - application/x-microsoft.net.object.binary.base64 is the format
44+
that the ResXResourceWriter will generate, however the reader can
45+
read any of the formats listed below.
46+
47+
mimetype: application/x-microsoft.net.object.binary.base64
48+
value : The object must be serialized with
49+
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
50+
: and then encoded with base64 encoding.
51+
52+
mimetype: application/x-microsoft.net.object.soap.base64
53+
value : The object must be serialized with
54+
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
55+
: and then encoded with base64 encoding.
56+
57+
mimetype: application/x-microsoft.net.object.bytearray.base64
58+
value : The object must be serialized into a byte array
59+
: using a System.ComponentModel.TypeConverter
60+
: and then encoded with base64 encoding.
61+
-->
62+
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
63+
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
64+
<xsd:element name="root" msdata:IsDataSet="true">
65+
<xsd:complexType>
66+
<xsd:choice maxOccurs="unbounded">
67+
<xsd:element name="metadata">
68+
<xsd:complexType>
69+
<xsd:sequence>
70+
<xsd:element name="value" type="xsd:string" minOccurs="0" />
71+
</xsd:sequence>
72+
<xsd:attribute name="name" use="required" type="xsd:string" />
73+
<xsd:attribute name="type" type="xsd:string" />
74+
<xsd:attribute name="mimetype" type="xsd:string" />
75+
<xsd:attribute ref="xml:space" />
76+
</xsd:complexType>
77+
</xsd:element>
78+
<xsd:element name="assembly">
79+
<xsd:complexType>
80+
<xsd:attribute name="alias" type="xsd:string" />
81+
<xsd:attribute name="name" type="xsd:string" />
82+
</xsd:complexType>
83+
</xsd:element>
84+
<xsd:element name="data">
85+
<xsd:complexType>
86+
<xsd:sequence>
87+
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
88+
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
89+
</xsd:sequence>
90+
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
91+
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
92+
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
93+
<xsd:attribute ref="xml:space" />
94+
</xsd:complexType>
95+
</xsd:element>
96+
<xsd:element name="resheader">
97+
<xsd:complexType>
98+
<xsd:sequence>
99+
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
100+
</xsd:sequence>
101+
<xsd:attribute name="name" type="xsd:string" use="required" />
102+
</xsd:complexType>
103+
</xsd:element>
104+
</xsd:choice>
105+
</xsd:complexType>
106+
</xsd:element>
107+
</xsd:schema>
108+
<resheader name="resmimetype">
109+
<value>text/microsoft-resx</value>
110+
</resheader>
111+
<resheader name="version">
112+
<value>2.0</value>
113+
</resheader>
114+
<resheader name="reader">
115+
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
116+
</resheader>
117+
<resheader name="writer">
118+
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
119+
</resheader>
120+
<data name="GreaterThanAttribute" xml:space="preserve">
121+
<value>'{PropertyName}' doit être supérieur à '{ComparisonValue}'. - resx</value>
122+
</data>
123+
<data name="NotNullAttribute" xml:space="preserve">
124+
<value>'{PropertyName}' ne doit pas être nul. - resx</value>
125+
</data>
24126
</root>

0 commit comments

Comments
 (0)