@@ -4,7 +4,7 @@ import 'package:vine/src/contracts/schema.dart';
44import 'package:vine/src/contracts/vine.dart' ;
55import 'package:vine/src/helper.dart' ;
66
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}$' );
88
99final class VineStringRule implements VineRule {
1010 final String ? message;
@@ -93,13 +93,15 @@ final class VineEmailRule implements VineRule {
9393}
9494
9595final class VinePhoneRule implements VineRule {
96+ final RegExp ? regex;
9697 final String ? message;
9798
98- const VinePhoneRule (this .message);
99+ const VinePhoneRule (this .regex, this . message);
99100
100101 @override
101102 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)) {
103105 final error = ctx.errorReporter.format ('phone' , field, message, {});
104106 ctx.errorReporter.report ('phone' , [...field.customKeys, field.name], error);
105107 }
@@ -161,16 +163,18 @@ final class VineUrlRule implements VineRule {
161163 final bool allowUnderscores;
162164 final String ? message;
163165
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);
165168
166169 @override
167170 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+ })) {
174178 final error = ctx.errorReporter.format ('url' , field, message, {});
175179 ctx.errorReporter.report ('url' , [...field.customKeys, field.name], error);
176180 }
@@ -214,7 +218,8 @@ final class VineStartWithRule implements VineRule {
214218 @override
215219 void handle (VineValidationContext ctx, FieldContext field) {
216220 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});
218223 ctx.errorReporter.report ('startWith' , [...field.customKeys, field.name], error);
219224 }
220225 }
@@ -248,13 +253,15 @@ final class VineConfirmedRule implements VineRule {
248253 final hasKey = ctx.data.containsKey (confirmedKey);
249254
250255 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});
252258 ctx.errorReporter.report ('missingProperty' , [...field.customKeys, field.name], error);
253259 }
254260
255261 final currentValue = ctx.data[confirmedKey];
256262 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});
258265 ctx.errorReporter.report ('confirmed' , [...field.customKeys, field.name], error);
259266 }
260267
0 commit comments