closedEnum function

String closedEnum(
  1. Object? value,
  2. Set<String> allowed,
  3. String path, {
  4. String? ifAbsent,
})

Reads a closed-set string, where path already names the key.

An unrecognised value throws: a closed enum is not an extension point, so falling back would place a control the client cannot honour (a sidebar action read as row lands on every record). Absent yields ifAbsent, or throws when the contract gives no default.

Implementation

String closedEnum(
  Object? value,
  Set<String> allowed,
  String path, {
  String? ifAbsent,
}) {
  if (value == null && ifAbsent != null) return ifAbsent;
  if (value is String && allowed.contains(value)) return value;
  throw SchemaFormatException(
    path,
    'expected one of ${allowed.join(', ')}; got '
    '${value is String ? '"$value"' : value.runtimeType}',
  );
}