ex_json_schema icon indicating copy to clipboard operation
ex_json_schema copied to clipboard

Fix bug when validating schemas that specify an explicit metaschema

Open heydtn opened this issue 4 years ago • 0 comments

Schema.assert_valid_schema/1 was checking whether an input schema was one of the JSON Schema draft metaschemas, and if so, automatically marking it as valid. The problem, though, is that it was checking the $schema field instead of the $id field. This meant that any schema which specified that it's supposed to be checked against a JSON Schema draft would automatically pass even if it wasn't valid.

This can be reproduced with the following

iex(1)> ExJsonSchema.Schema.resolve(%{"properties" => "foo"})
** (ExJsonSchema.Schema.InvalidSchemaError) schema did not pass validation against its meta-schema: [%ExJsonSchema.Validator.Error{error: %ExJsonSchema.Validator.Error.Type{actual: "string", expected: ["object"]}, path: "#/properties"}]
    (ex_json_schema 0.8.1) lib/ex_json_schema/schema.ex:145: ExJsonSchema.Schema.resolve_root/1
iex(1)> ExJsonSchema.Schema.resolve(%{"$schema" => "http://json-schema.org/draft-07/schema#", "properties" => "foo"})
%ExJsonSchema.Schema.Root{
  custom_format_validator: nil,
  definitions: %{},
  location: :root,
  refs: %{},
  schema: %{
    "$schema" => "http://json-schema.org/draft-07/schema#",
    "properties" => "foo"
  },
  version: 7
}

heydtn avatar Nov 17 '21 01:11 heydtn