0

I have a JSON request I'm trying to validate against a JSON schema. I'm wondering is it possible to require a field in a subschema base off the value of a field within that same subschema? I've tried anyOf, OneOf and IF, Then, Else to no avail. anyOf returns error "only 1 subschema matches out of 2" and OneOf return "2 subschemas matched instead of one".

{
"field1":"aaa",
"field2":"bbb"
"field3":{ 
   "isTrue":true,
   "inner1":"1",
   "inner2":"1"
    }
}

So for the above Json, can I only require fields inner1 and inner2 if field isTrue is true?

"field3": {
                                "type": "object",
                                "properties": {
                                    "isTrue": {
                                        "type": 
                                            "boolean"
                                    },
                                    "inner1": {
                                        "type": 
                                            "integer"
                                    },
                                    "inner2": {
                                        "type": 
                                            "string"
                                    }
                                },
                                "anyOf": [
                                    {
                                        "properties": {
                                            "isTrue": {
                                                "const": "true"
                                            }
                                        },
                                        "required": [
                                            "inner1",
                                            "inner2"
                                        ]
                                    },
                                    {
                                        "properties": {
                                            "isTrue": {
                                                "const": "false"
                                            }
                                        },
                                        "required": [
                                            "isTrue"
                                        ]
                                    }
                                ]

                            }

The above is my offending Json schema

1 Answer 1

1

Your schema should work if you remove the quotes around "true" and "false" -- they are being treated as strings, rather than booleans.

(Also, your data is using strings for both inner1 and inner2, but the schema wants the first of those to be an integer -- perhaps that was a transcription error.)

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

Comments

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.