JsonSchemaValidator.fromJson constructor

JsonSchemaValidator.fromJson(
  1. String json
)

Parses json as a JSON Schema string and compiles it into a validator.

Throws FormatException if json is not valid JSON or if the decoded value is not a JSON object (e.g. an array or a scalar).

Example:

final validator = JsonSchemaValidator.fromJson(
  '{"required": ["name"], "properties": {"name": {"type": "string"}}}',
);

Implementation

factory JsonSchemaValidator.fromJson(String json) {
  final Object? decoded;
  try {
    decoded = jsonDecode(json);
  } on FormatException {
    rethrow;
  }
  if (decoded is! Map<String, dynamic>) {
    throw FormatException(
      'JSON Schema must be a JSON object, '
      'got ${decoded.runtimeType}',
      json,
    );
  }
  return JsonSchemaValidator.fromMap(decoded);
}