Closed panSarin closed 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.
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 ?
@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.
Ha! That makes sense! Thanks man and sorry for being such pain on the ... ;]
No worries! I'm going to close this one out for now, but let me know if you have any other trouble.
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?