JsonSchema.parse constructor

JsonSchema.parse(
  1. String text, {
  2. Uri? id,
  3. JsonSchemaRegistry? registry,
})

The schema written as JSON text; true and false are the schemas that accept everything and nothing. Throws a FormatException for text that is not JSON or not a schema.

Implementation

factory JsonSchema.parse(
  String text, {
  Uri? id,
  JsonSchemaRegistry? registry,
}) {
  final decoded = jsonDecode(text);
  return switch (decoded) {
    final Map<String, Object?> map => JsonSchema(
      map,
      id: id,
      registry: registry,
    ),
    true => JsonSchema(const {}, id: id, registry: registry),
    false => JsonSchema._rejecting(id: id, registry: registry),
    _ => throw const FormatException(
      'a JSON Schema is an object, true or false',
    ),
  };
}