validateField<Type> method
Method to help validate a FormField<Type> providing its value represented by its Maybe<Type>
, and
a List<Validator<Type>>
, returning a Result<String>
with possible error message.
Implementation
Result<String> validateField<Type>({
required Maybe<Type> field,
required List<Validator<Type>> validators,
}) {
List<String> errorMsgs = [];
for (Function validator in validators) {
String? error = validator(field.when(
nothing: () => '',
just: (val) => val != null ? val.toString() : '',
));
if (error != null) errorMsgs.add(error);
}
return errorMsgs.isEmpty
? const Success('')
: Failure(
FormError(
msg: errorMsgs.first,
),
);
}