checkKeys method

void checkKeys(
  1. Iterable<String> allowed, {
  2. Iterable<String>? suggestions,
})

Rejects keys outside allowed, suggesting the closest key from suggestions, which defaults to allowed.

Implementation

void checkKeys(Iterable<String> allowed, {Iterable<String>? suggestions}) {
  final allowedSet = allowed.toSet();
  for (final key in map.keys) {
    if (key is! String) {
      throw error('Keys must be strings.', key: key);
    }
    if (allowedSet.contains(key)) {
      continue;
    }
    final suggestion = closestMatch(key, suggestions ?? allowedSet);
    final where = path.isEmpty ? 'at the top level' : 'in ${path.join('.')}';
    throw error(
      'Unknown key "$key" $where.'
      '${suggestion == null ? '' : ' Did you mean "$suggestion"?'}',
      key: key,
    );
  }
}