toBoolStrict function

IValidator toBoolStrict(
  1. IValidator child, {
  2. String? message,
})

Strict bool coercion.

Only accepts:

  • Actual bool values
  • 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:

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);
}