validateOrThrow<T extends Exception> method

void validateOrThrow<T extends Exception>([
  1. T exception(
    1. Iterable<String> errors
    )?
])

Validates the object and throws an exception if there are any errors.

This method calls the validate method to retrieve a list of errors. If the list is not empty, it throws an exception with the errors joined by a newline character.

Implementation

void validateOrThrow<T extends Exception>([T Function(Iterable<String> errors)? exception]) {
  exception ??= (errors) => Exception(errors.join('\n')) as T;
  var errors = validate();
  if (errors.isNotEmpty) {
    throw exception(errors);
  }
}