Skip to content

Commit

Permalink
Fix bug when validating schemas that specify an explicit metaschema
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Nate Heydt committed Nov 17, 2021
1 parent a1191fc commit bf460c7
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/ex_json_schema/schema.ex
Original file line number Diff line number Diff line change
Expand Up @@ -373,13 +373,13 @@ defmodule ExJsonSchema.Schema do
end)
end

defp meta04?(%{"$schema" => @draft4_schema_url <> _}), do: true
defp meta04?(%{"$id" => @draft4_schema_url <> _}), do: true
defp meta04?(_), do: false

defp meta06?(%{"$schema" => @draft6_schema_url <> _}), do: true
defp meta06?(%{"$id" => @draft6_schema_url <> _}), do: true
defp meta06?(_), do: false

defp meta07?(%{"$schema" => @draft7_schema_url <> _}), do: true
defp meta07?(%{"$id" => @draft7_schema_url <> _}), do: true
defp meta07?(_), do: false

defp do_get_fragment(nil, _, _ref), do: {:error, :invalid_reference}
Expand Down
8 changes: 8 additions & 0 deletions test/ex_json_schema/schema_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,18 @@ defmodule ExJsonSchema.SchemaTest do

test "schema is validated against its meta-schema" do
schema = %{"properties" => "foo"}
schema_meta_draft7 = %{
"$schema" => "http://json-schema.org/draft-07/schema#",
"properties" => "foo"
}

assert_raise ExJsonSchema.Schema.InvalidSchemaError,
~s(schema did not pass validation against its meta-schema: [%ExJsonSchema.Validator.Error{error: %ExJsonSchema.Validator.Error.Type{actual: "string", expected: ["object"]}, path: "#/properties"}]),
fn -> resolve(schema) end

assert_raise ExJsonSchema.Schema.InvalidSchemaError,
~s(schema did not pass validation against its meta-schema: [%ExJsonSchema.Validator.Error{error: %ExJsonSchema.Validator.Error.Type{actual: "string", expected: ["object"]}, path: "#/properties"}]),
fn -> resolve(schema_meta_draft7) end
end

test "resolving an absolute reference in a scoped schema" do
Expand Down

0 comments on commit bf460c7

Please sign in to comment.