assertSet<T> method

void assertSet<T>(
  1. String field,
  2. T value
)

Asserts that the field can be set with the given value, and throws List<ValidationException> if any validators fail. Throws ArgumentError if the field does not exist or if the value is of the wrong type.

Implementation

void assertSet<T>(String field, T value) {
  final meta = metadata[field];
  if (meta == null) throw ArgumentError.value(field, 'field');
  if (value == null || value.runtimeType != meta.type) {
    throw TypeError();
  }

  final errors = <ValidationException>[];
  for (final v in meta.validators) {
    final err = v.validate(value);
    if (err != null) errors.add(err);
  }
  if (errors.isNotEmpty) throw errors;
}