jonasschmidt / ex_json_schema

An Elixir JSON Schema validator
MIT License
366 stars 98 forks source link

Validation reporting errors for properties that have arrays of objects unexpectedly #21

Closed technomage closed 7 years ago

technomage commented 7 years ago

Of the following tests, the first passes, and the remaining 2 do not. They look valid to me, but appear to indicate the validator does not handle properties that are arrays of objects properly.

test "array ref test 2" do schema = %{ "type" => "array", "items" => %{"$ref" => "#/definitions/item"}, "definitions" => %{ "item" => %{ "type" => "object", "properties" => %{ "uid" => %{"type" => "string"} }, "required" => ["uid"], "additionalProperties" => false } } } |> ExJsonSchema.Schema.resolve json = [%{uid: "test", junk: 1}] {:error, [{msg, var}]} = ExJsonSchema.Validator.validate(schema, string_keys(json)) assert Regex.run(~r/junk/,var) != nil assert Regex.run(~r/not allow additional/,msg) != nil end

test "array ref test 3" do schema = %{ "type" => "object", "properties" => %{ "tests" => %{ "type" => "array", "items" => %{"$ref" => "#/definitions/item"} }, }, "definitions" => %{ "item" => %{ "type" => "object", "properties" => %{ "uid" => %{ "type" => "string" } }, "required" => ["uid"], "additionalProperties" => false } } } |> ExJsonSchema.Schema.resolve json = %{tests: [%{uid: "test"}]} :ok = ExJsonSchema.Validator.validate(schema, string_keys(json)) end

test "array nesting test 4" do schema = %{ "type" => "object", "properties" => %{ "tests" => %{ "type" => "array", "items" => %{ "type" => "object", "properties" => %{ "uid" => %{ "type" => "string" } }, "required" => ["uid"], "additionalProperties" => false } }, }, } |> ExJsonSchema.Schema.resolve json = %{tests: [%{uid: "test"}]} :ok = ExJsonSchema.Validator.validate(schema, string_keys(json)) end

Test output:

technomage commented 7 years ago

json_schema_test.exs.zip