validate method

Result validate(
  1. dynamic value, {
  2. bool exists = true,
})

Main validation method. Use this method if you want to validate that a dynamic value is valid, and get an error message if not.

You can also call isValid if you just want to check if the value is valid.

If you want to to throw an error use validateOrThrow

exists If set to true, the value is consider to "exist", this is most useful for optional fields in maps.

Will throw an error if used with async validators

Implementation

Result validate(dynamic value, {bool exists = true}) {
  if ((value == null && isNullable && exists) || (!exists && isOptional)) {
    return Result.valid(value);
  }
  final r = validator(value);
  if (r is Future<Result>) {
    throw AsyncValidatorException();
  }
  return r;
}