toDateTime function

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

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, {String? message}) {
  final base = core.transform((value) {
    return switch (value) {
      final DateTime d => d,
      final String s => DateTime.tryParse(s.trim()),
      _ => null,
    };
  }, child);

  return handleReturnPreserveValue(base, message);
}