fromDynamic static method
Processes the list of ValueValidator objects from the given map
which
must be an actual Map or a Map-like object that supports the []
operator. Any object that is not Map-like will result in an error.
When attempting to build a validator, this will first check the list of custom validator builders and then check the list of internal validator builders. This allows applications the ability to override even the built in default types.
This expects a JSON object that looks like:
{
"validators": [
{
"type": "<type>"
},
...
]
}
See also:
- CurrencyValidator, ensures the value represents a currency.
- EmailValidator, ensures the value is a valid email address format.
- MaxLengthValidator, ensures the value has at most N characters.
- MaxNumberValidator, ensures the value is a number no larger than N.
- MinLengthValidator, ensures the value has at least N characters.
- MinNumberValidator, ensures the value is a number no smaller than N.
- NumberValidator, ensures the value is a valid number.
- PhoneNumberValidator, ensures the value is formatted as a phone number.
- RequiredValidator, ensures the value is not empty.
Note: All validators except for RequiredValidator will pass on an empty value.
Implementation
static Validator fromDynamic(dynamic map) {
late Validator result;
if (map == null) {
throw Exception('[Validator.fromDynamic]: map is null');
} else {
final list = map['validators'];
final validators = <ValueValidator>[];
if (list?.isNotEmpty == true) {
for (var map in list) {
final type = map['type'];
final builder =
_customValidatorBuilders[type] ?? _validatorBuilders[type];
if (builder != null) {
final validator = builder(map);
validators.add(validator);
} else {
throw Exception('Unknown validator type: "$type');
}
}
if (validators.isNotEmpty == true) {
result = Validator(validators: validators);
}
}
}
return result;
}