validate static method

Map<String, String> validate(
  1. Map<Object?, Object?> metadata, [
  2. String name = 'metadata'
])

Returns an unmodifiable copy of metadata, or throws ArgumentError for a non-string key or value, a reserved key, or a map over the limits.

Implementation

static Map<String, String> validate(
  Map<Object?, Object?> metadata, [
  String name = 'metadata',
]) {
  if (metadata.length > maxEntries) {
    throw ArgumentError.value(
      metadata.length,
      name,
      'must have at most $maxEntries entries',
    );
  }
  final validated = <String, String>{};
  for (final MapEntry(:key, :value) in metadata.entries) {
    if (key is! String || value is! String) {
      throw ArgumentError.value(key, name, 'keys and values must be strings');
    }
    if (reservedKeys.contains(key)) {
      throw ArgumentError.value(key, name, 'is a reserved metadata key');
    }
    if (key.runes.length > maxKeyLength) {
      throw ArgumentError.value(
        key,
        name,
        'keys must have at most $maxKeyLength characters',
      );
    }
    if (value.runes.length > maxValueLength) {
      throw ArgumentError.value(
        key,
        name,
        'values must have at most $maxValueLength characters',
      );
    }
    validated[key] = value;
  }
  if (utf8.encode(jsonEncode(validated)).length > maxEncodedBytes) {
    throw ArgumentError.value(
      metadata.length,
      name,
      'must encode to at most $maxEncodedBytes bytes of JSON',
    );
  }
  return Map.unmodifiable(validated);
}