-
-
Notifications
You must be signed in to change notification settings - Fork 307
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
✨ Proposal: Add a Global defaultAdditionalProperties
Toggle at the Root Level
#1587
Comments
I see where you're coming from, but this violates a core principle of how JSON Schema works. Specifically, a schema can only "see" that which is inside of it; it can't see anything in the JSON structure above it. So any subschema would be unaware that your new Let's work through your example: {
"$schema": "http://json-schema.org/draft-07/schema#",
"defaultAdditionalProperties": false,
"type": "object",
"properties": {
"name": { "type": "string" },
"details": {
"type": "object",
"properties": {
"age": { "type": "integer" }
}
}
}
} Your intent is for the subschema at {
"type": "object",
"properties": {
"age": { "type": "integer" }
}
} It has no way of knowing there is a keyword at the root defining more constraints. What happens with a {
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "https://example.test/root",
"defaultAdditionalProperties": false,
"type": "object",
"properties": {
"name": { "type": "string" },
"details": { "$ref": "./details" }
}
}
{
"$id": "https://example.test/details",
"type": "object",
"properties": {
"age": { "type": "integer" }
}
} The schema {
"$id": "https://example.test/foo",
"type": "object",
"properties": {
"bar": { "$ref": "./bar" },
"baz": { "$ref": "./baz" }
}
}
{
"$id": "https://example.test/bar",
"type": "object",
"defaultAdditionalProperties": false,
"properties": {
"person": { "$ref": "./person" }
}
}
{
"$id": "https://example.test/baz",
"type": "object",
"properties": {
"person": { "$ref": "./person" }
}
}
{
"$id": "https://example.test/person",
"type": "object",
"properties": {
"name": { "type": "string" },
"age": { "type": "integer" }
}
} So evaluating data looks like this: {
"foo": {
"name": "Steve",
"age": 13,
"location": "Auckland" // invalid
},
"bar": {
"name": "Steve",
"age": 13,
"location": "Auckland" // valid
}
} Both |
I agree, I have seen the usage of this at every object level than global. |
Describe the inspiration for your proposal
Currently, JSON Schema requires
additionalProperties: false
to be set explicitly on every object where I want to disallow extra properties. This gets repetitive when I want all objects in my schema (root and nested) to enforce this rule consistently. For example, I have a schema like this:I have to repeat
additionalProperties: false
for both the root object and the details object. In a larger schema with many nested objects, this repetition grows tedious and increases the chance of forgetting it somewhere. There’s no way to set this once globally and have it cascade to all objects, so I’m stuck either duplicating it or using$ref
to a shared definition, which still requires manual application each time. A global setting would make this cleaner and align with the DRY.Describe the proposal
Add a new root-level keyword, like
defaultAdditionalProperties
, that sets the default value for additionalProperties across all objects in the schema unless overridden. For example:Benefits
Describe alternatives you've considered
$defs
and$ref
works but still requires explicit references per object, not a true global solution.Additional context
No response
The text was updated successfully, but these errors were encountered: