-
-
Notifications
You must be signed in to change notification settings - Fork 270
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
How to do inheritance? #348
Comments
JSON Schema doesn't support inheritance. You can combine schemas using boolean operations in ways that can look like inheritance, but if you think about it that way, you will confuse what is actually happening. You will have to get out of that mindset and think about defining your data a little differently. Your example is a fairly easy one to express except for one thing. I'll start with the schemas and then explain what it's doing and what the limitations are. {
"id": "http://example.com/schemas/person",
"type": "object",
"properties": {
"firstName": { "type": "string" },
"lastName": { "type": "string" }
},
"required": ["firstName", "lastName"]
} {
"id": "http://example.com/schema/employee",
"allOf": [{ "$ref": "http://example.com/schemas/person" }],
"properties": {
"company": { "type": "string" }
},
"required": ["company"]
} In the Employee schema, we use {
"type": "object",
"allOf": [
{
"properties": {
"foo": { "type": "integer", "minimum": 2 }
}
}
{
"properties": {
"foo": { "type": "integer", "multipleOf": 2 }
}
}
]
} In this example the property "foo" has a minimum of 2 and excludes odd numbers. The limitation you have with this approach is that you can't use {
"id": "http://example.com/schema/final-employee",
"allOf": [
{ "$ref": "http://example.com/schemas/person" },
{ "$ref": "http://example.com/schemas/employee" }
],
"properties": {
"firstName": {},
"lastName": {},
"company": {}
},
"additionalProperties": false
} The problem, of course, is that any time you add a property to either of these schemas, you need update FinalEmployee as well. That's why I advise that you always ignore additional properties rather than explicitly forbid them. It has some value, but not nearly enough to be worth the pain. So, I hope that helped. Schemas are not describing objects, they are a collection of constraints. So, thinking in terms of inheritance doesn't work. |
@kiloquad see also json-schema-org/json-schema-org.github.io#77 for a more verbose but also more flexible approach using the draft-06 keyword |
I took the re-use/addlProps label off because I'm using that to manage an informal vote on the various proposed solutions, and this is a question rather than solution. If you think the answer is found somewhere among the existing solution proposals, please vote per the VOTE-A-RAMA comment and close this. |
It's been nearly two weeks since this was answered, with no further questions. I've filed an issue on the web site repo to document this better, so I'm going to close this out. Please continue any related conversation at json-schema-org/json-schema-org.github.io#148 |
Sorry, I can't find any better place to ask and I can't find any examples. It looks like $merge would do it but according to issue #15, this wont be included.
My use case, I have an
Person
object in a schema file with afirstName
andlastName
requiresfirstName
.I need an
Employee
object in a separate schema. I want it to inheritPerson
and add the propertycompany
and require thecompany
property.A valid
Person
:An invalid
Person
:A invalid
Person
:A valid
Employee
:An invalid
Employee
:A invalid
Employee
:I'd like to end up with two schema files,
Person.schema.json
andEmployee.schema.json
. Where aPerson
object or andEmployee
object would passPerson.schema.json
validation, aPerson
object would not necessarily passEmployee.schema.json
validation.The text was updated successfully, but these errors were encountered: