0

I have a set of options for validation:

{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "title": "parsePosition Validator",
    "type": "object",
    "properties": {
        "option1": {
            "type": "array",
            "items": {
                "tpye": "number"
            },
            "minItems": 3
        },
        "option2": {
            "type": "array",
            "items": {
            "tpye": "number"
            },
            "minItems": 3
        },
        "option3": {
            "oneOf": [
            {
                "type": "array",
                "items": {
                    "tpye": "number"
                },
                "minItems": 3
            },
            {
                "x": { "type":"number" },
                "y": { "type":"number" },
                "z": { "type":"number" },
                "required": ["x","y","z"]
            }
        }
    },
    "oneOf": [
        { "required":["option1"] },
        { "required":["option2"] },
        { "required":["option3"] }
    ]
}

Essentially the input of the json can be any of the three options. My issue arises when trying to validate option3. Possible inputs could be:

{
    "option3": [1,2,3]
}

or

{
    "option3": {
        "x": 1,
        "y": 2,
        "z": 3
    }
}

However, when I use the array rather than objects, the validator does not throw an issue with an empty array or an array of incorrect size.

Am I missing something?

1 Answer 1

1

You are missing something very simple...

You haven't put type: object in your second subschema in the oneOf. for option 3.

The reason you need this is because JSON Schema is a constraints based language.

Specifying properties, doesn't mean the the instance location HAS to be an object; You also need to say "this location should be an object".

An array of any size is valid under your subschema oneOf[1], because properties and `required are only applicable to an object.

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

2 Comments

Also, x, y and z are specified as if they are keywords; they should be wrapped in a {"properties":{...} object.
See, it's EASY to miss basic things! =]

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.