pickKeys function

IValidator pickKeys(
  1. Iterable<String> keys,
  2. IValidator child, {
  3. String? message,
})

Picks a subset of keys from a Map, producing a new Map with only those keys present (if they existed). Fails if input is not a Map.

Implementation

IValidator pickKeys(Iterable<String> keys, IValidator child, {String? message}) {
  return core.pivotValue(
    (value) {
      if (value is! Map) return null;

      final out = <dynamic, dynamic>{};

      for (final k in keys) {
        if (value.containsKey(k)) out[k] = value[k];
      }

      return out;
    },
    child: child,
    errorMessage: message ?? 'a Map containing keys: ${keys.join(', ')}',
  );
}