length function
Checks whether the given value has a length property and the length matches the validators
Implementation
IValidator length(List<IValidator> validators, {String? message}) {
return Validator((value) {
if (hasLengthProperty(value)) {
final len = (value as dynamic).length;
final result = all(validators).validate(len);
if (result.isValid) return Result.valid(value);
// Re-wrap child expectations into a single consolidated expectation for clarity.
// Child messages (e.g. 'equal to 2') are wrapped in square brackets like prior behavior.
final joined = result.expectations.map((e) => e.message).join(' & ');
return Expectation(
message: message ?? 'length [$joined]',
value: value,
code: ExpectationCodes.valueLengthOutOfRange,
data: {'length': len},
).toInvalidResult();
}
return Expectation(
message: message ?? '${value.runtimeType} does not have a length property',
value: value,
code: ExpectationCodes.logicPredicateFailed,
).toInvalidResult();
});
}