isoDate function

LivrRule isoDate(
  1. List<Object?> args,
  2. RuleContext context
)

iso_date: the value must be an ISO 8601 date without time, like 2014-08-14.

With bounds, {iso_date: {'min': '2026-01-01', 'max': 'today'}}, the date must be in the range, both bounds included. A bound is a date or 'today', 'yesterday' or 'tomorrow': the current UTC date of the clock of the validator, one day before it or after it. The first argument, if any, must be an options object (LIVR 2.0 ignored any argument, so {iso_date: 'today'} checked nothing); arguments after it are ignored. {iso_date: null} is the plain iso_date, as in 2.0. Dates are in the proleptic Gregorian calendar: 1582-10-10 is valid, 1500-02-29 is not.

Implementation

LivrRule isoDate(List<Object?> args, RuleContext context) {
  if (args.isEmpty || (args.length == 1 && args[0] == null)) return _isoDate;

  final options = args[0];
  if (options is! Map<Object?, Object?>) {
    throw LivrSchemaException(
      'Rule [iso_date] expects no arguments or an object with "min" and '
      '"max", got [$options]',
    );
  }
  for (final key in options.keys) {
    if (key != 'min' && key != 'max') {
      throw LivrSchemaException(
        'Rule [iso_date] got an unknown option [$key]'
        '${key == 'format' ? ', use iso_datetime for datetimes' : ''}',
      );
    }
  }

  // A fixed bound is a key of a date (_dateKey), a relative one is the
  // number of days from the current date
  final min = options.containsKey('min')
      ? _parseBound(options['min'], 'min')
      : null;
  final max = options.containsKey('max')
      ? _parseBound(options['max'], 'max')
      : null;
  if (min == null && max == null) return _isoDate;

  final minKey = min?.key ?? -1;
  final maxKey = max?.key ?? _maxKey;
  final minDays = min?.days;
  final maxDays = max?.days;

  // No value could pass. A fixed and a relative bound are not compared: the
  // result depends on the day, and that can be intended
  if (min != null &&
      max != null &&
      (minDays == null
          ? maxDays == null && minKey > maxKey
          : maxDays != null && minDays > maxDays)) {
    throw LivrSchemaException(
      'Rule [iso_date] got min [${options['min']}] later than max '
      '[${options['max']}]',
    );
  }

  if (minDays == null && maxDays == null) {
    return (value, allValues) {
      if (value is! String) return _notString(value);
      if (value.isEmpty) return RuleResult.valid;

      final key = _dateKey(value);
      if (key < 0) return RuleResult.error('WRONG_DATE');
      if (key < minKey) return RuleResult.error('DATE_TOO_EARLY');
      if (key > maxKey) return RuleResult.error('DATE_TOO_LATE');
      return RuleResult.valid;
    };
  }

  final clock = context.clock;
  return (value, allValues) {
    if (value is! String) return _notString(value);
    if (value.isEmpty) return RuleResult.valid;

    final key = _dateKey(value);
    if (key < 0) return RuleResult.error('WRONG_DATE');

    // One clock read per value: the current date in UTC, never in the local
    // time zone
    final today = _dayNumber(clock());
    if (key < (minDays == null ? minKey : _relativeKey(today, minDays))) {
      return RuleResult.error('DATE_TOO_EARLY');
    }
    if (key > (maxDays == null ? maxKey : _relativeKey(today, maxDays))) {
      return RuleResult.error('DATE_TOO_LATE');
    }
    return RuleResult.valid;
  };
}