@@ -4,7 +4,7 @@ import 'package:vine/src/contracts/schema.dart';
4
4
import 'package:vine/src/contracts/vine.dart' ;
5
5
import 'package:vine/src/helper.dart' ;
6
6
7
- final regex = RegExp (r'^\+?[0-9]{1,4}[-.\s]?[0-9]{1,4}[-.\s]?[0-9]{4,10}$' );
7
+ final europeanPhoneRegex = RegExp (r'^\+?[0-9]{1,4}[-.\s]?[0-9]{1,4}[-.\s]?[0-9]{4,10}$' );
8
8
9
9
final class VineStringRule implements VineRule {
10
10
final String ? message;
@@ -93,13 +93,15 @@ final class VineEmailRule implements VineRule {
93
93
}
94
94
95
95
final class VinePhoneRule implements VineRule {
96
+ final RegExp ? regex;
96
97
final String ? message;
97
98
98
- const VinePhoneRule (this .message);
99
+ const VinePhoneRule (this .regex, this . message);
99
100
100
101
@override
101
102
void handle (VineValidationContext ctx, FieldContext field) {
102
- if (field.value case String value when ! regex.hasMatch (value)) {
103
+ final currentRegexp = regex ?? europeanPhoneRegex;
104
+ if (field.value case String value when ! currentRegexp.hasMatch (value)) {
103
105
final error = ctx.errorReporter.format ('phone' , field, message, {});
104
106
ctx.errorReporter.report ('phone' , [...field.customKeys, field.name], error);
105
107
}
@@ -161,16 +163,18 @@ final class VineUrlRule implements VineRule {
161
163
final bool allowUnderscores;
162
164
final String ? message;
163
165
164
- const VineUrlRule (this .protocols, this .requireTld, this .requireProtocol, this .allowUnderscores, this .message);
166
+ const VineUrlRule (
167
+ this .protocols, this .requireTld, this .requireProtocol, this .allowUnderscores, this .message);
165
168
166
169
@override
167
170
void handle (VineValidationContext ctx, FieldContext field) {
168
- if (field.value case String value when ! value.isURL ({
169
- 'protocols' : protocols,
170
- 'requireTld' : requireTld,
171
- 'requireProtocol' : requireProtocol,
172
- 'allowUnderscores' : allowUnderscores,
173
- })) {
171
+ if (field.value case String value
172
+ when ! value.isURL ({
173
+ 'protocols' : protocols,
174
+ 'requireTld' : requireTld,
175
+ 'requireProtocol' : requireProtocol,
176
+ 'allowUnderscores' : allowUnderscores,
177
+ })) {
174
178
final error = ctx.errorReporter.format ('url' , field, message, {});
175
179
ctx.errorReporter.report ('url' , [...field.customKeys, field.name], error);
176
180
}
@@ -214,7 +218,8 @@ final class VineStartWithRule implements VineRule {
214
218
@override
215
219
void handle (VineValidationContext ctx, FieldContext field) {
216
220
if (field.value case String value when ! value.startsWith (attemptedValue)) {
217
- final error = ctx.errorReporter.format ('startWith' , field, message, {'value' : attemptedValue});
221
+ final error =
222
+ ctx.errorReporter.format ('startWith' , field, message, {'value' : attemptedValue});
218
223
ctx.errorReporter.report ('startWith' , [...field.customKeys, field.name], error);
219
224
}
220
225
}
@@ -248,13 +253,15 @@ final class VineConfirmedRule implements VineRule {
248
253
final hasKey = ctx.data.containsKey (confirmedKey);
249
254
250
255
if (! hasKey) {
251
- final error = ctx.errorReporter.format ('missingProperty' , field, message, {'field' : confirmedKey});
256
+ final error =
257
+ ctx.errorReporter.format ('missingProperty' , field, message, {'field' : confirmedKey});
252
258
ctx.errorReporter.report ('missingProperty' , [...field.customKeys, field.name], error);
253
259
}
254
260
255
261
final currentValue = ctx.data[confirmedKey];
256
262
if ((field.value as String ) != currentValue) {
257
- final error = ctx.errorReporter.format ('confirmed' , field, message, {'attemptedName' : confirmedKey});
263
+ final error =
264
+ ctx.errorReporter.format ('confirmed' , field, message, {'attemptedName' : confirmedKey});
258
265
ctx.errorReporter.report ('confirmed' , [...field.customKeys, field.name], error);
259
266
}
260
267
0 commit comments