fromDynamic static method

Validator fromDynamic(
  1. dynamic map
)

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:

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;
}