brandur / json_schema

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

Throwin warning about additional property #60

Closed panSarin closed 8 years ago

panSarin commented 8 years ago

Is there any option that json_schema validator would throw a warning if in response i have some fields that are not specified in JSON Schema file?

brandur commented 8 years ago

@panSarin Hey! So what you can do here is use the non-exception version of #validate:

valid, errors = schema.validate(data)

And then look through the errors for errors of type :invalid_keys and react appropriately.

panSarin commented 8 years ago

Not sure if I got u wrong, or you got my question wrong:

Let me use an example:

lets say that is object that i am validating

department_in_index": {
      "type": "object",
      "required": ["id", "name", "facility"],
      "properties": {
        "id": { "type": "string", "example": "567bc35c5605f67f41000025" },
        "name": { "type": "string", "example": "Department AB"},
        "facility": { "type": "string", "example": "1A" }
      }
    },

and in my data that i validate with i have 1 more additional field ( "city" f.e. )

So i would like to have

valid, errors = schema.validate(data)

errors filled with invalid_keys: :city, but it doesnt seems to care about additional attributes in data. Am i doing something wrong or I misunderstood you ?

brandur commented 8 years ago

@panSarin Ah, I think I see! In the case above, try adding "additionalProperties": false. This will make any keys that are not in the properties list invalid.

panSarin commented 8 years ago

Ha! That makes sense! Thanks man and sorry for being such pain on the ... ;]

brandur commented 8 years ago

No worries! I'm going to close this one out for now, but let me know if you have any other trouble.