How to use JSON schema oneOf for arrays with fixed

2019-07-25 15:25发布

问题:

I want to specify multiple values for oneOf and have defined the below schema that validates successfully (http://json-schema-validator.herokuapp.com/). Note there is deliberately only one value under oneOf in this example.

{
    "id": "test-schema",
    "$schema": "http://json-schema.org/draft-04/schema#",
    "description": "test schema",

    "type": "object",
    "properties": {

        "alpha": {

            "type": "object",
            "properties": {

                "beta": {

                    "oneOf": [
                        {
                            "type": "object",
                            "properties": {

                                "ObjA": {

                                    "type": "object",
                                    "properties": {

                                        "a": {

                                            "type": "array",
                                            "items": {

                                                "type": "number",
                                                "default": [90, 95],
                                                "additionalProperties": false
                                            },
                                            "additionalProperties": false
                                        },

                                        "b": {

                                            "type": "array",
                                            "items": {

                                                "type": "number",
                                                "default": [4, 8],
                                                "additionalProperties": false
                                            },
                                            "additionalProperties": false
                                        },

                                        "c": {

                                            "type": "array",
                                            "items": {

                                                "type": "number",
                                                "default": [0.2, 0.6],
                                                "additionalProperties": false
                                            },
                                            "additionalProperties": false
                                        }
                                    },                                    
                                    "additionalProperties": false
                                }
                            }, 
                            "additionalProperties": false
                        }
                    ]
                }
            },         
            "additionalProperties": false
        }
    },
    "additionalProperties": false
}    

Q1: I am using default to specify the initial array value but really want these to be fixed and disallow any other values. How can I achieve this?

Answered here How to specify which oneOf item a JSON object should take? .

Q2: I want to specify that beta be assigned ObjA in a JSON file that validates against this schema, how do I specify this?

Answered here How to specify which oneOf item a JSON object should take? .

回答1:

Answered here How to specify which oneOf item a JSON object should take?

This link was my first attempt at this question but was posed in a more complicated way, here I tried to simplify it. It is answered now so I refer to it.