Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/41.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.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
Node.js 如何在JSON模式验证器中使用if-else条件_Node.js_Json_Jsonschema - Fatal编程技术网

Node.js 如何在JSON模式验证器中使用if-else条件

Node.js 如何在JSON模式验证器中使用if-else条件,node.js,json,jsonschema,Node.js,Json,Jsonschema,我想使用JSON模式验证器进行验证,而我使用的代码如下,我得到的错误是没有定义gCode 我试着像下面的代码一样 properties: { oemId: { 'type': ['integer'], 'minLength': 1, 'required': true }, gCode:{ 'type': ['string'], 'minLength'

我想使用JSON模式验证器进行验证,而我使用的代码如下,我得到的错误是没有定义gCode

我试着像下面的代码一样

properties: {
        oemId: {
          'type': ['integer'],
          'minLength': 1,
          'required': true
        },
        gCode:{
          'type': ['string'],
          'minLength': 1
        },
        problemCategoryId: {
          'type': ['integer'],
          'minLength': 1
        }
      },
      if :{
        properties:{
          oemId: 1
        }
      },
      then:{
        required:[gCode]
      },
      else:{
        required: [problemCategoryId]
      }

我预计当oemId=1时,gCode is required=true else problemCategoryId is required true

所讨论的JSON模式的
if-then-else
语句不正确。这是正确的一个:

{
  "type": "object",
  "properties": {
        oemId: {
          'type': ['integer'],
          'minLength': 1,
          'required': true
        },
        gCode:{
          'type': ['string'],
          'minLength': 1
        },
        problemCategoryId: {
          'type': ['integer'],
          'minLength': 1
        }
  },
  "if": {
    "properties": {
      "oemId": { "const": 1 }
    },
    "required": ["oemId"]
  },
  "then": { "required": ["gCode"] },
  "else": { "required": ["problemCategoryId"] }
}
请注意,
if-then-else
语法刚刚添加到中的JSON模式中,这里是它在JSON-Schema.org中的文档: