brandur / json_schema

A JSON Schema V4 and Hyperschema V4 parser and validator.
MIT License
230 stars 45 forks source link

Recursive references not expanded #58

Closed esiegel closed 8 years ago

esiegel commented 8 years ago

Below is a schema for parsing arrays of objects where each value in the object is either a string or another object. I'm finding the situation where things that should not validate are in fact passing validation.

#examples

# good
[{a: "a", b: "b", c: "c", "nested": {d: "a", e: "b"}}]

# bad where any of the values are not a,b,c or another object
[{a: "BAD", b: {c: "BAD2 }}]
    schema = JsonSchema.parse!(
      {
        "$schema" => "http://json-schema.org/draft-04/schema#",
        type: "array", items: { "$ref" => "#/definitions/obj_abc" },
        definitions: {
          abc: { type: "string", enum: ["a", "b", "c"] },
          obj_abc: { 
            type: "object",
            additionalProperties: {
              oneOf: [
                { "$ref" => "#/definitions/abc" },
                { "$ref" => "#/definitions/obj_abc" }
              ]
            }
          }
        }
      }.with_indifferent_access
    )

    schema.expand_references!

    data = [{
      a: "a",
      b: "b",
      c: "c",
      a_obj: { a: "a"},
      bad_obj: { bad: "BAD"},
    }]

    success, errors = schema.validate(data)

I tried debugging this and found myself in the additionProperties validation where the get_extra_keys was returning nil. I'm assuming that the reference expander is doing something funky, or maybe I am myself.

Thanks for any and all help.

esiegel commented 8 years ago

Ok, this seems to be a problem with additionalProperties not returning a boolean on success. Creating a pull request now.