This repository was archived by the owner on Sep 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 231
/
Copy pathvalidators.dart
115 lines (105 loc) · 3.82 KB
/
validators.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import 'package:angular/angular.dart';
import 'directives/validators.dart' show ValidatorFn;
import 'model.dart' as model_module;
/// Providers for validators to be used for [Control]s in a form.
///
/// Provide this using `ExistingProvider.forToken` to add validators.
const NG_VALIDATORS = MultiToken<Object>('NgValidators');
/// Provides a set of validators used by form controls.
///
/// A validator is a function that processes a [Control] or collection of
/// controls and returns a map of errors. A null map means that validation has
/// passed.
///
/// ### Example
///
/// ```dart
/// Control loginControl = new Control("", Validators.required)
/// ```
class Validators {
/// Validator that requires controls to have a non-empty value.
static Map<String, bool>? required(model_module.AbstractControl control) {
return control.value == null || control.value == ''
? {'required': true}
: null;
}
/// Validator that requires controls to have a value of a minimum length.
static ValidatorFn minLength(num minLength) {
return /* Map < String , dynamic >? */ (model_module.AbstractControl
control) {
if (Validators.required(control) != null) return null;
final v = control.value as String;
return v.length < minLength
? {
'minlength': {
'requiredLength': minLength,
'actualLength': v.length
}
}
: null;
};
}
/// Validator that requires controls to have a value of a maximum length.
static ValidatorFn maxLength(num maxLength) {
return /* Map < String , dynamic >? */ (model_module.AbstractControl
control) {
if (Validators.required(control) != null) return null;
final v = control.value as String;
return v.length > maxLength
? {
'maxlength': {
'requiredLength': maxLength,
'actualLength': v.length
}
}
: null;
};
}
/// Validator that requires a control to match a regex to its value.
static ValidatorFn pattern(String pattern) {
return /* Map < String , dynamic >? */ (model_module.AbstractControl
control) {
if (Validators.required(control) != null) return null;
var regex = RegExp('^$pattern\$');
final v = control.value as String;
return regex.hasMatch(v)
? null
: {
'pattern': {'requiredPattern': '^$pattern\$', 'actualValue': v}
};
};
}
/// No-op validator.
static Map<String, bool>? nullValidator(model_module.AbstractControl c) =>
null;
/// Compose multiple validators into a single function that returns the union
/// of the individual error maps.
static ValidatorFn? compose(List<ValidatorFn?>? validators) {
if (validators == null) return null;
final presentValidators = _removeNullValidators(validators);
if (presentValidators.isEmpty) return null;
return (model_module.AbstractControl control) {
return _executeValidators(control, presentValidators);
};
}
// TODO(tsander): Remove the need to filter the validation. The list of
// validators should not contain null values.
static List<T> _removeNullValidators<T>(List<T?> validators) {
final result = <T>[];
for (var i = 0, len = validators.length; i < len; i++) {
var validator = validators[i];
if (validator != null) result.add(validator);
}
return result;
}
}
Map<String, dynamic>? _executeValidators(
model_module.AbstractControl control, List<ValidatorFn> validators) {
var result = <String, dynamic>{};
for (var i = 0, len = validators.length; i < len; i++) {
final validator = validators[i];
final localResult = validator(control);
if (localResult != null) result.addAll(localResult);
}
return result.isEmpty ? null : result;
}