Skip to content

Commit cbfef4f

Browse files
author
Nate Heydt
committed
Fix bug when validating schemas that specify an explicit metaschema
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.
1 parent a1191fc commit cbfef4f

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

lib/ex_json_schema/schema.ex

+3-3
Original file line numberDiff line numberDiff line change
@@ -373,13 +373,13 @@ defmodule ExJsonSchema.Schema do
373373
end)
374374
end
375375

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

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

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

385385
defp do_get_fragment(nil, _, _ref), do: {:error, :invalid_reference}

test/ex_json_schema/schema_test.exs

+9
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,18 @@ defmodule ExJsonSchema.SchemaTest do
3434
test "schema is validated against its meta-schema" do
3535
schema = %{"properties" => "foo"}
3636

37+
schema_meta_draft7 = %{
38+
"$schema" => "http://json-schema.org/draft-07/schema#",
39+
"properties" => "foo"
40+
}
41+
3742
assert_raise ExJsonSchema.Schema.InvalidSchemaError,
3843
~s(schema did not pass validation against its meta-schema: [%ExJsonSchema.Validator.Error{error: %ExJsonSchema.Validator.Error.Type{actual: "string", expected: ["object"]}, path: "#/properties"}]),
3944
fn -> resolve(schema) end
45+
46+
assert_raise ExJsonSchema.Schema.InvalidSchemaError,
47+
~s(schema did not pass validation against its meta-schema: [%ExJsonSchema.Validator.Error{error: %ExJsonSchema.Validator.Error.Type{actual: "string", expected: ["object"]}, path: "#/properties"}]),
48+
fn -> resolve(schema_meta_draft7) end
4049
end
4150

4251
test "resolving an absolute reference in a scoped schema" do

0 commit comments

Comments
 (0)