getField function

IValidator getField(
  1. String key,
  2. IValidator inner
)

Extracts and validates a field from a map.

Retrieves the value associated with the key from a map and passes it to the inner validator. Fails if the input is not a map or if the key is not present.

If you need to validate more than one field, consider using eskema.

Implementation

IValidator getField(String key, IValidator inner) =>
    isMap() &
    containsKey(key) &
    Validator((value) {
      final r = inner.validate(value[key]);
      if (r.isValid) return r;

      return Result.invalid(
        value,
        expectations: r.expectations
            .map((e) => Expectation(
                  message: e.message,
                  value: e.value,
                  path: '$key${e.path != null ? '.${e.path}' : ''}',
                ))
            .toList(),
      );
    });