Skip to content

Commit 0e6f8c3

Browse files
Merge pull request #126 from martijn00/test-coverage
Add more test coverage
2 parents 995b030 + df2439e commit 0e6f8c3

File tree

118 files changed

+1234
-444
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

118 files changed

+1234
-444
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## 11.0.1
44

55
- Align between validator input types with other validators
6+
- Split up BaseValidator into TranslatedValidator so only that one needs to provide a translation
67

78
## 11.0.0
89

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -382,8 +382,8 @@ We welcome efforts to internationalize/localize the package by translating the d
382382
#### Add new validator
383383

384384
1. Add a new validator to one of the folders in the `src` folder.
385-
2. Implement it using the `BaseValidator` class. Override the `validateValue` method and let the base class handle the null check in the `validate` method.
386-
3. Override the `translatedErrorText` property and return the correct translation from `FormBuilderLocalizations.current.`.
385+
2. Implement it using the `BaseValidator` or `TranslatedValidator` class. Override the `validateValue` method and let the base class handle the null check in the `validate` method.
386+
3. When using a `TranslatedValidator, Override the `translatedErrorText` property and return the correct translation from `FormBuilderLocalizations.current.`.
387387
4. Make sure to pass `errorText` and `checkNullOrEmpty` to the base class.
388388
5. Add static method to `form_builder_validators.dart` that uses the new validator.
389389
6. Implement tests

analysis_options.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,6 @@ linter:
1010
always_use_package_imports: false
1111
always_specify_types: true
1212
omit_local_variable_types: false
13-
public_member_api_docs: true
13+
public_member_api_docs: true
14+
directives_ordering: true
15+

lib/form_builder_validators.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,5 @@ export 'src/identity/identity.dart';
4444
export 'src/network/network.dart';
4545
export 'src/numeric/numeric.dart';
4646
export 'src/string/string.dart';
47+
export 'src/translated_validator.dart';
4748
export 'src/usecase/usecase.dart';

lib/l10n/intl_en.arb

Lines changed: 206 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,89 @@
44
"dateStringErrorText": "This field requires a valid date string.",
55
"emailErrorText": "This field requires a valid email address.",
66
"equalErrorText": "This field value must be equal to {value}.",
7-
"equalLengthErrorText": "Value must have a length equal to {length}",
7+
"@equalErrorText": {
8+
"placeholders": {
9+
"value": {
10+
"type": "String",
11+
"description": "The value that the field must equal"
12+
}
13+
}
14+
},
15+
"equalLengthErrorText": "Value must have a length equal to {length}.",
16+
"@equalLengthErrorText": {
17+
"placeholders": {
18+
"length": {
19+
"type": "int",
20+
"description": "The exact length the value must have"
21+
}
22+
}
23+
},
824
"integerErrorText": "This field requires a valid integer.",
925
"ipErrorText": "This field requires a valid IP.",
1026
"matchErrorText": "Value does not match pattern.",
11-
"maxErrorText": "Value must be less than or equal to {max}",
12-
"maxLengthErrorText": "Value must have a length less than or equal to {maxLength}",
13-
"maxWordsCountErrorText": "Value must have a words count less than or equal to {maxWordsCount}",
14-
"minErrorText": "Value must be greater than or equal to {min}",
15-
"minLengthErrorText": "Value must have a length greater than or equal to {minLength}",
16-
"minWordsCountErrorText": "Value must have a words count greater than or equal to {minWordsCount}",
27+
"maxErrorText": "Value must be less than or equal to {max}.",
28+
"@maxErrorText": {
29+
"placeholders": {
30+
"max": {
31+
"type": "num",
32+
"description": "The maximum value allowed"
33+
}
34+
}
35+
},
36+
"maxLengthErrorText": "Value must have a length less than or equal to {maxLength}.",
37+
"@maxLengthErrorText": {
38+
"placeholders": {
39+
"maxLength": {
40+
"type": "int",
41+
"description": "The maximum length allowed"
42+
}
43+
}
44+
},
45+
"maxWordsCountErrorText": "Value must have a words count less than or equal to {maxWordsCount}.",
46+
"@maxWordsCountErrorText": {
47+
"placeholders": {
48+
"maxWordsCount": {
49+
"type": "int",
50+
"description": "The maximum word count allowed"
51+
}
52+
}
53+
},
54+
"minErrorText": "Value must be greater than or equal to {min}.",
55+
"@minErrorText": {
56+
"placeholders": {
57+
"min": {
58+
"type": "num",
59+
"description": "The minimum value allowed"
60+
}
61+
}
62+
},
63+
"minLengthErrorText": "Value must have a length greater than or equal to {minLength}.",
64+
"@minLengthErrorText": {
65+
"placeholders": {
66+
"minLength": {
67+
"type": "int",
68+
"description": "The minimum length required"
69+
}
70+
}
71+
},
72+
"minWordsCountErrorText": "Value must have a words count greater than or equal to {minWordsCount}.",
73+
"@minWordsCountErrorText": {
74+
"placeholders": {
75+
"minWordsCount": {
76+
"type": "int",
77+
"description": "The minimum word count required"
78+
}
79+
}
80+
},
1781
"notEqualErrorText": "This field value must not be equal to {value}.",
82+
"@notEqualErrorText": {
83+
"placeholders": {
84+
"value": {
85+
"type": "String",
86+
"description": "The value that the field must not equal"
87+
}
88+
}
89+
},
1890
"numericErrorText": "Value must be numeric.",
1991
"requiredErrorText": "This field cannot be empty.",
2092
"urlErrorText": "This field requires a valid URL address.",
@@ -23,17 +95,93 @@
2395
"creditCardExpiredErrorText": "This credit card has expired.",
2496
"creditCardCVCErrorText": "This field requires a valid CVC code.",
2597
"colorCodeErrorText": "Value should be a valid {colorCode} color code.",
98+
"@colorCodeErrorText": {
99+
"placeholders": {
100+
"colorCode": {
101+
"type": "String",
102+
"description": "The type of color code (e.g., HEX, RGB)"
103+
}
104+
}
105+
},
26106
"uppercaseErrorText": "Value must be uppercase.",
27107
"lowercaseErrorText": "Value must be lowercase.",
28-
"fileExtensionErrorText": "File extension must be {extensions}",
29-
"fileSizeErrorText": "File size must be less than {maxSize} while it is {fileSize}",
30-
"dateRangeErrorText": "Date must be in range {min} - {max}",
108+
"fileExtensionErrorText": "File extension must be {extensions}.",
109+
"@fileExtensionErrorText": {
110+
"placeholders": {
111+
"extensions": {
112+
"type": "String",
113+
"description": "Allowed file extensions"
114+
}
115+
}
116+
},
117+
"fileSizeErrorText": "File size must be less than {maxSize} while it is {fileSize}.",
118+
"@fileSizeErrorText": {
119+
"placeholders": {
120+
"maxSize": {
121+
"type": "String",
122+
"description": "The maximum file size allowed"
123+
},
124+
"fileSize": {
125+
"type": "String",
126+
"description": "The actual file size"
127+
}
128+
}
129+
},
130+
"dateRangeErrorText": "Date must be in range {min} - {max}.",
131+
"@dateRangeErrorText": {
132+
"placeholders": {
133+
"min": {
134+
"type": "DateTime",
135+
"format": "yMd",
136+
"example": "11/10/2021",
137+
"description": "The minimum date allowed"
138+
},
139+
"max": {
140+
"type": "DateTime",
141+
"format": "yMd",
142+
"example": "11/10/2021",
143+
"description": "The maximum date allowed"
144+
}
145+
}
146+
},
31147
"mustBeTrueErrorText": "This field must be true.",
32148
"mustBeFalseErrorText": "This field must be false.",
33149
"containsSpecialCharErrorText": "Value must contain at least {min} special characters.",
150+
"@containsSpecialCharErrorText": {
151+
"placeholders": {
152+
"min": {
153+
"type": "int",
154+
"description": "The minimum number of special characters required"
155+
}
156+
}
157+
},
34158
"containsUppercaseCharErrorText": "Value must contain at least {min} uppercase characters.",
159+
"@containsUppercaseCharErrorText": {
160+
"placeholders": {
161+
"min": {
162+
"type": "int",
163+
"description": "The minimum number of uppercase characters required"
164+
}
165+
}
166+
},
35167
"containsLowercaseCharErrorText": "Value must contain at least {min} lowercase characters.",
168+
"@containsLowercaseCharErrorText": {
169+
"placeholders": {
170+
"min": {
171+
"type": "int",
172+
"description": "The minimum number of lowercase characters required"
173+
}
174+
}
175+
},
36176
"containsNumberErrorText": "Value must contain at least {min} numbers.",
177+
"@containsNumberErrorText": {
178+
"placeholders": {
179+
"min": {
180+
"type": "int",
181+
"description": "The minimum number of numerical characters required"
182+
}
183+
}
184+
},
37185
"alphabeticalErrorText": "Value must be alphabetical.",
38186
"uuidErrorText": "Value must be a valid UUID.",
39187
"jsonErrorText": "Value must be valid JSON.",
@@ -44,11 +192,59 @@
44192
"oddNumberErrorText": "Value must be an odd number.",
45193
"evenNumberErrorText": "Value must be an even number.",
46194
"portNumberErrorText": "Value must be a valid port number between {min} and {max}.",
195+
"@portNumberErrorText": {
196+
"placeholders": {
197+
"min": {
198+
"type": "int",
199+
"description": "The minimum port number allowed"
200+
},
201+
"max": {
202+
"type": "int",
203+
"description": "The maximum port number allowed"
204+
}
205+
}
206+
},
47207
"macAddressErrorText": "Value must be a valid MAC address.",
48208
"startsWithErrorText": "Value must start with {value}.",
209+
"@startsWithErrorText": {
210+
"placeholders": {
211+
"value": {
212+
"type": "String",
213+
"description": "The value that the field must start with"
214+
}
215+
}
216+
},
49217
"endsWithErrorText": "Value must end with {value}.",
218+
"@endsWithErrorText": {
219+
"placeholders": {
220+
"value": {
221+
"type": "String",
222+
"description": "The value that the field must end with"
223+
}
224+
}
225+
},
50226
"containsErrorText": "Value must contain {value}.",
227+
"@containsErrorText": {
228+
"placeholders": {
229+
"value": {
230+
"type": "String",
231+
"description": "The value that the field must contain"
232+
}
233+
}
234+
},
51235
"betweenErrorText": "Value must be between {min} and {max}.",
236+
"@betweenErrorText": {
237+
"placeholders": {
238+
"min": {
239+
"type": "num",
240+
"description": "The minimum value allowed"
241+
},
242+
"max": {
243+
"type": "num",
244+
"description": "The maximum value allowed"
245+
}
246+
}
247+
},
52248
"containsElementErrorText": "Value must be in list.",
53249
"ibanErrorText": "Value must be a valid IBAN.",
54250
"uniqueErrorText": "Value must be unique.",

lib/src/base_validator.dart

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,7 @@ abstract class BaseValidator<T> {
1010
/// {@template base_validator_error_text}
1111
/// The error message returned if the value is invalid.
1212
/// {@endtemplate}
13-
String get errorText => _errorText ?? translatedErrorText;
14-
15-
/// The translated error message returned if the value is invalid.
16-
String get translatedErrorText;
13+
String? get errorText => _errorText;
1714

1815
/// {@template base_validator_null_check}
1916
/// Whether to check if the value is null or empty.

lib/src/bool/has_lowercase_chars_validator.dart

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import '../../localization/l10n.dart';
2-
import '../base_validator.dart';
1+
import '../../form_builder_validators.dart';
32

43
/// {@template has_lowercase_chars_template}
5-
/// [HasLowercaseCharsValidator] extends [BaseValidator] to validate if a string
4+
/// [HasLowercaseCharsValidator] extends [TranslatedValidator] to validate if a string
65
/// contains a specified minimum number of lowercase characters.
76
///
87
/// ## Parameters:
@@ -13,7 +12,7 @@ import '../base_validator.dart';
1312
///
1413
/// {@macro lower_case_template}
1514
/// {@endtemplate}
16-
class HasLowercaseCharsValidator extends BaseValidator<String> {
15+
class HasLowercaseCharsValidator extends TranslatedValidator<String> {
1716
/// Constructor for the lowercase characters validator.
1817
HasLowercaseCharsValidator({
1918
this.atLeast = 1,

lib/src/bool/has_numeric_chars_validator.dart

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import '../../localization/l10n.dart';
2-
import '../base_validator.dart';
1+
import '../../form_builder_validators.dart';
32

43
/// {@template has_numeric_chars_template}
5-
/// [HasNumericCharsValidator] extends [BaseValidator] to validate if a string
4+
/// [HasNumericCharsValidator] extends [TranslatedValidator] to validate if a string
65
/// contains a specified minimum number of numeric characters (digits).
76
///
87
/// ## Parameters:
@@ -13,7 +12,7 @@ import '../base_validator.dart';
1312
///
1413
/// {@macro numeric_chars_template}
1514
/// {@endtemplate}
16-
class HasNumericCharsValidator extends BaseValidator<String> {
15+
class HasNumericCharsValidator extends TranslatedValidator<String> {
1716
/// Constructor for the numeric characters validator.
1817
HasNumericCharsValidator({
1918
this.atLeast = 1,

lib/src/bool/has_special_chars_validator.dart

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import '../../localization/l10n.dart';
2-
import '../base_validator.dart';
1+
import '../../form_builder_validators.dart';
32

43
/// {@template has_special_chars_template}
5-
/// [HasSpecialCharsValidator] extends [BaseValidator] to validate if a string
4+
/// [HasSpecialCharsValidator] extends [TranslatedValidator] to validate if a string
65
/// contains a specified minimum number of special characters.
76
///
87
/// ## Parameters:
@@ -13,7 +12,7 @@ import '../base_validator.dart';
1312
///
1413
/// {@macro special_chars_template}
1514
/// {@endtemplate}
16-
class HasSpecialCharsValidator extends BaseValidator<String> {
15+
class HasSpecialCharsValidator extends TranslatedValidator<String> {
1716
/// Constructor for the special characters validator.
1817
HasSpecialCharsValidator({
1918
this.atLeast = 1,

lib/src/bool/has_uppercase_chars_validator.dart

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import '../../localization/l10n.dart';
2-
import '../base_validator.dart';
1+
import '../../form_builder_validators.dart';
32

43
/// {@template has_uppercase_chars_template}
5-
/// [HasUppercaseCharsValidator] extends [BaseValidator] to validate if a string
4+
/// [HasUppercaseCharsValidator] extends [TranslatedValidator] to validate if a string
65
/// contains a specified minimum number of uppercase characters.
76
///
87
/// ## Parameters:
@@ -13,7 +12,7 @@ import '../base_validator.dart';
1312
///
1413
/// {@macro upper_case_template}
1514
/// {@endtemplate}
16-
class HasUppercaseCharsValidator extends BaseValidator<String> {
15+
class HasUppercaseCharsValidator extends TranslatedValidator<String> {
1716
/// Constructor for the uppercase characters validator.
1817
HasUppercaseCharsValidator({
1918
this.atLeast = 1,

0 commit comments

Comments
 (0)