toDateTime function

IValidator toDateTime(
  1. IValidator child
)

Coerces a value to a DateTime.

Handles existing DateTime objects and strings that can be parsed as a DateTime. Passes the resulting DateTime to the child validator.

Implementation

IValidator toDateTime(IValidator child) =>
    transform((value) {
      return switch (value) {
        final DateTime d => d,
        final String s => DateTime.tryParse(s.trim()),
        _ => null,
      };
    }, child) >
    Expectation(message: 'a valid DateTime formatted String');