0

Let's say I have a simple JSON like:

"values": {
    "A": 0,
    "B": 0,
    "C": 0,
    "D": 0,
    "E": 0,
}

A, B, C are part of the basic schema T1 while D,E are part of an extending schema T2. Therefore the schema T1 has 3 properties while T2 has 5 properties. I would like to avoid write A,B,C twice, in both schemas.

Is there anyway in JSON Schema to extend the list of properties?

4
  • Have you read the documentation on how to use references? json-schema.org/understanding-json-schema/structuring.html Are you looking for other details not mentioned there? =] Commented May 27, 2019 at 9:03
  • Yes, I have. And they are great for sub part that are similar. If I had A and B that had the same sub objects. But I need a schema T1 with properties A,B,C and a second schema T2 with properties D,E. And then I need a way to make a schema allowing all the properties from T1 and T2: A,B,C,D,E. I need the union of two schemas. I am not sure what I say makes sense. Commented May 27, 2019 at 10:32
  • In which case, you need to use allOf. Have you seen that keyword? If not, I'll share details in an answer. Commented May 27, 2019 at 10:33
  • Yes, I have seen. But reading from it seems that I won't work for expanding a schema with another since both schemas needs to pass. If both schemas have additionalProperties: false, it is not possible for them to both be valid on the same input. No? Commented May 27, 2019 at 11:46

1 Answer 1

1

If you want to split out your schemas in a way which reflects extending objects, where some of your schemas have additionalProperties: false, you need to hoist up the additionalProperties: false.

You can use a definition in each schema to define the object without additionalProperties: false, then the root schema allOf reference the definition with additionalProperties: true. Then in the other schemas, you can reference the previous schema's definition.

Unfortunately, it's a limitation with draft-7. In draft-8, it is possible to do what you want with a new keyword, but it's not published yet.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks :) In the meantime, if we need, we may go around the issue with preprocessing the schemas for including sub-parts.
In case you're still looking to make this better, as of JSON Schema 2019-09, you can use the unevaluatedProperties keyword, which can "see through" applicator keywords.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.