0

Now I have a large json schema like

{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "title": "A example schema",
    "definitions": {
        "stringArray": {
            "type": "array",
            "items": { "type": "string" },
            "uniqueItems": true,
            "default": []
        }
    },
    "properties": {
        "time": {"type": "string", "minLength": 5},
        "id": {"type": "integer"},
        "members": {
             "type": "array",
             "items": {
                 "properties": {
                     "id": {"type": "string"},
                     "email": {"type": "string"}
                 }
             }
        }
    }
}

and a new schema using it

{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "title": "A example schema",
    "$ref": "example 1"
}

I want to modify the second schema to let some filed support null type.

I am thinking to split the first schema into two parts, one for those don't support null, another for those need to support null. For those need to support null, I need to keep two copy, one support null type, one not, because they are all needed in different case.

I wonder if there's a easier way to achieve?

1 Answer 1

2

First off, importantly, draft 7 ignores any keywords that are siblings of $ref, so you'll want to move that $ref into a aggregation keyword (we call them "applicators" in the spec). Typically allOf is used:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "A example schema",
  "allOf": [
    { "$ref": "example 1" }
  ]
}

But for your application, you'll want to use oneOf because you want to include another subschema that allows for null values as an option.

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "A example schema",
  "oneOf": [
    { "$ref": "example 1" },
    { "type": "null" }
  ]
}
Sign up to request clarification or add additional context in comments.

1 Comment

I don't want this ref support to be null type. What I want is let those filed in ref support null type, for example "time"

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.