transformers library

Transformers

Value–coercion helpers that run BEFORE the provided child validator. They:

  • Accept a broader set of input types (e.g. int / num / String for toInt)
  • Produce a (possibly) transformed value
  • Pass that value to the child validator

Failure model:

  • The “pre‑check” part (e.g. $isInt | $isNumber | $isIntString) ensures only plausible values reach the mapper. If that OR validator fails, the chain stops there (no transform executed).
  • The transform(...) function itself NEVER throws; it may return null. If it returns null, the child validator receives null (and will typically fail unless it is nullable()).

Composition patterns:

  • Coerce then constrain: final age = toInt(isGte(0) & isLte(130));
  • Add null support after coercion: final maybeDate = toDateTime(isType<DateTime>()).nullable();
  • Field extraction + transform: final userAge = getField('age', toInt(isGte(18)));

Expectations:

  • Each helper appends (via > 'a valid DateTime formatted String' etc.) a human readable expectation for clearer error messages.

NOTE: These are “inline transformers” — they do not mutate external data, only the value flowing through the validator pipeline.

Functions

defaultTo(dynamic defaultValue, IValidator child) IValidator
Provides a default value if the input is null.
getField(String key, IValidator inner) IValidator
Extracts and validates a field from a map.
split(String separator, IValidator child) IValidator
Splits a string into a list of substrings.
toBool(IValidator child) IValidator
Coerces a value to a boolean.
toDateTime(IValidator child) IValidator
Coerces a value to a DateTime.
toDouble(IValidator child) IValidator
Coerces a value to a double.
toInt(IValidator child) IValidator
Coerces a value to an integer.
toLowerCase(IValidator child) IValidator
Transforms a string to lowercase.
toNum(IValidator child) IValidator
Coerces a value to a number (num).
toUpperCase(IValidator child) IValidator
Transforms a string to uppercase.
transform<T>(T fn(dynamic), IValidator child) IValidator
Transforms a value using a provided function.
trim(IValidator child) IValidator
Trims leading and trailing whitespace from a string.