Reject Extra Fields With JSON Schema additionalProperties: false
The properties keyword checks the values of named fields. To catch a typo like timeoutSeconds written as timeOutSeconds, add additionalProperties: false to the object that owns that key.
Worked example
Close delivery without closing its parent
{
"type": "object",
"properties": {
"delivery": {
"type": "object",
"properties": {"timeoutSeconds": {"type": "integer"}},
"additionalProperties": false
}
},
"required": ["delivery"]
}Steps
- Choose the exact object you want to keep closed. This example closes delivery while leaving the top level open for unrelated metadata.
- Validate {"delivery":{"timeoutSeconds":15},"source":"test"}; both the known delivery key and the unrelated top-level source should be accepted.
- Validate {"delivery":{"timeOutSeconds":15}} and check for an additional property error at /delivery. Fix the spelling rather than deleting the rule.
What to check
- An empty delivery object passes in this example. Add timeoutSeconds to delivery.required only if every caller must supply it.
- With allOf composition, additionalProperties only recognizes properties declared alongside it. Check the dialect before switching to unevaluatedProperties for composed schemas.