toBoolStrict function
Strict bool coercion.
Only accepts:
- Actual
boolvalues - Strings
"true"/"false"(case-insensitive)
Rejects numeric 1/0, yes/no, on/off, t/f, etc. Use toBool for a middle ground (adds int 1/0) or toBoolLenient for broad textual support.
See also:
- toBool – standard (bool + 1/0 + 'true'/'false')
- toBoolLenient – very permissive variants
Implementation
IValidator toBoolStrict(IValidator child, {String? message}) {
final validator = (($isBool | toLowerCase(isString() & isOneOf(['true', 'false'])))) &
core.transform((v) {
return switch (v) {
final bool b => b,
final String s => s.toLowerCase().trim() == 'true',
_ => null,
};
}, child);
return handleReturnPreserveValue(validator, message);
}