@@ -49,3 +49,96 @@ extension RangeValidation on SimpleValidationBuilder<num> {
49
49
);
50
50
}
51
51
}
52
+
53
+ extension RangeNullableValidation on SimpleValidationBuilder <num ?> {
54
+ /// Adds a validation rule that checks if the [num?] is within the range of [min] and [max] .
55
+ ///
56
+ /// [min] and [max] define the acceptable range for the number.
57
+ /// [message] is the error message returned if the validation fails. Defaults to "Must be between $min and $max".
58
+ /// [code] is an optional error code for translation purposes.
59
+ ///
60
+ /// Returns the [LucidValidationBuilder] to allow for method chaining.
61
+ ///
62
+ /// Example:
63
+ /// ```dart
64
+ /// ...
65
+ /// ruleFor((user) => user.age, key: 'age') // user.age is nullable
66
+ /// .range(18, 65);
67
+ /// ```
68
+ ///
69
+ /// String format args:
70
+ /// - **{PropertyName}**: The name of the property.
71
+ /// - **{From}**: The minimum value of the range.
72
+ /// - **{To}**: The maximum value of the range.
73
+ /// - **{PropertyValue}**: The value of the property.
74
+ ///
75
+ SimpleValidationBuilder <num ?> range (num min, num max,
76
+ {String ? message, String ? code}) {
77
+ return use (
78
+ (value, entity) {
79
+ if (value != null && value >= min && value <= max) return null ;
80
+
81
+ final currentCode = code ?? Language .code.range;
82
+ final currentMessage = LucidValidation .global.languageManager.translate (
83
+ currentCode,
84
+ parameters: {
85
+ 'PropertyName' : label.isNotEmpty ? label : key,
86
+ 'From' : '$min ' ,
87
+ 'To' : '$max ' ,
88
+ 'PropertyValue' : '$value ' ,
89
+ },
90
+ defaultMessage: message,
91
+ );
92
+
93
+ return ValidationException (message: currentMessage, code: currentCode);
94
+ },
95
+ );
96
+ }
97
+ }
98
+
99
+ extension RangeOrNullableValidation on SimpleValidationBuilder <num ?> {
100
+ /// Adds a validation rule that checks if the [num?] is within the range of [min] and [max] .
101
+ ///
102
+ /// [min] and [max] define the acceptable range for the number.
103
+ /// [message] is the error message returned if the validation fails. Defaults to "Must be between $min and $max".
104
+ /// [code] is an optional error code for translation purposes.
105
+ ///
106
+ /// Returns the [LucidValidationBuilder] to allow for method chaining.
107
+ ///
108
+ /// Example:
109
+ /// ```dart
110
+ /// ...
111
+ /// ruleFor((user) => user.age, key: 'age') // user.age is nullable
112
+ /// .range(18, 65);
113
+ /// ```
114
+ ///
115
+ /// String format args:
116
+ /// - **{PropertyName}**: The name of the property.
117
+ /// - **{From}**: The minimum value of the range.
118
+ /// - **{To}**: The maximum value of the range.
119
+ /// - **{PropertyValue}**: The value of the property.
120
+ ///
121
+ SimpleValidationBuilder <num ?> rangeOrNull (num min, num max,
122
+ {String ? message, String ? code}) {
123
+ return use (
124
+ (value, entity) {
125
+ if (value == null ) return null ;
126
+ if (value >= min && value <= max) return null ;
127
+
128
+ final currentCode = code ?? Language .code.range;
129
+ final currentMessage = LucidValidation .global.languageManager.translate (
130
+ currentCode,
131
+ parameters: {
132
+ 'PropertyName' : label.isNotEmpty ? label : key,
133
+ 'From' : '$min ' ,
134
+ 'To' : '$max ' ,
135
+ 'PropertyValue' : '$value ' ,
136
+ },
137
+ defaultMessage: message,
138
+ );
139
+
140
+ return ValidationException (message: currentMessage, code: currentCode);
141
+ },
142
+ );
143
+ }
144
+ }
0 commit comments