toInt function

IValidator toInt(
  1. IValidator child
)

Coerces a value to an integer.

Handles existing integers, doubles (by truncating), and strings that can be parsed as an integer. Passes the resulting integer to the child validator.

Implementation

IValidator toInt(IValidator child) =>
    ($isInt | $isNumber | $isIntString) &
    transform((v) {
      return switch (v) {
        final int n => n,
        final double n => n.toInt(),
        final String s => int.tryParse(s.trim()),
        _ => null,
      };
    }, child);