Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript JSON模式是否可以使用引用外部属性的if/then/else_Javascript_Json_Object_Ajv - Fatal编程技术网

Javascript JSON模式是否可以使用引用外部属性的if/then/else

Javascript JSON模式是否可以使用引用外部属性的if/then/else,javascript,json,object,ajv,Javascript,Json,Object,Ajv,我想根据其他属性的值有条件地添加required。 仅当“isInexperienced”时,才需要“companyName”和“companyAddress” 值为false 模式 数据 我不完全理解您原始模式的意图,但是这个如何 { "$schema": "http://json-schema.org/draft-07/schema#", "type": "object", "properties": { "previous_employment_se

我想根据其他属性的值有条件地添加required。 仅当“isInexperienced”时,才需要“companyName”和“companyAddress” 值为false

模式

数据


我不完全理解您原始模式的意图,但是这个如何

{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "type": "object",
    "properties": {
        "previous_employment_section": {
            "type": "array",
            "items": {
                "type": "object",
                "properties": {
                    "companyAddress": {
                        "type": "string"
                    },
                    "companyName": {
                        "type": "string"
                    }
                }
            }
        },
        "isInexperienced": {
            "type": "boolean"
        }
    },
    "if": {
        "properties": {
            "isInexperienced": {
                "const": false
            }
        }
    },
    "then": {
        "properties": {
            "previous_employment_section": {
                "items": {
                    "required": [
                        "companyName",
                        "companyAddress"
                    ]
                },
                "minItems": 1
            }
        }
    }
}

这是不可能的。我们需要在更高的层次上使用“if”,可以嵌套“properties”。可以使用Leadpony的方法

{
  "previous_employment_section": [],
  "isInexperienced": true
}
{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "type": "object",
    "properties": {
        "previous_employment_section": {
            "type": "array",
            "items": {
                "type": "object",
                "properties": {
                    "companyAddress": {
                        "type": "string"
                    },
                    "companyName": {
                        "type": "string"
                    }
                }
            }
        },
        "isInexperienced": {
            "type": "boolean"
        }
    },
    "if": {
        "properties": {
            "isInexperienced": {
                "const": false
            }
        }
    },
    "then": {
        "properties": {
            "previous_employment_section": {
                "items": {
                    "required": [
                        "companyName",
                        "companyAddress"
                    ]
                },
                "minItems": 1
            }
        }
    }
}