首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >与“anyOf”匹配的Json模式

与“anyOf”匹配的Json模式
EN

Stack Overflow用户
提问于 2020-10-01 13:04:08
回答 1查看 841关注 0票数 1

我希望能够管理“objects”的json数组,其中每个对象都有一个类型和属性,如果对象中缺少强制属性,则会出现架构错误。

我试图这样做(不包括数组部分),声明两种对象类型,并表示json中的对象可以是这两种类型之一:

代码语言:javascript
复制
{
  'definitions': 
  {
    'typeone': 
    {
      'type': 'object',
      'properties': 
      {
        'xtype': {'type':'string', 'const':'typeone'},
        'num' :  {'type':'number'}
      },
      'required':['xtype', 'num'],
      'additionalProperties':false
    },
    'typetwo': 
    {
      'type': 'object',
      'properties': 
      {
        'xtype': {'type':'string', 'const':'typetwo'},
        'str' :  {'type':'string'}
      },
      'required':['xtype', 'str'],
      'additionalProperties':false
    }
  },
  'anyOf':
  [
     { '$ref': '#/definitions/typeone' },
     { '$ref': '#/definitions/typetwo' },
  ]
}

但是,如果我向它提供json,它会失败,因为这样的对象缺少了一个强制属性:

代码语言:javascript
复制
{
  'xtype': 'typeone'
}

...it JSON does not match any schemas from 'anyOf'.错误--我可以看到原因是它不知道如何在xtype上进行匹配,相反,它只是认为xtype‘xtype’是无效的,并且看上去是其他类型的。

是否有更好的方法来执行anyOf,它将基于一个属性值(如“开关”)进行硬匹配,然后给出关于该对象类型缺少其他强制属性的错误?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-10-01 14:48:19

它更冗长,但是您可以使用if/then来切换基于"xtype“属性的验证。

代码语言:javascript
复制
{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "allOf": [
    {
      "if": {
        "type": "object",
        "properties": {
          "xtype": { "const": "typeone" }
        },
        "required": ["xtype"]
      },
      "then": { "$ref": "#/definitions/typeone" }
    },
    {
      "if": {
        "type": "object",
        "properties": {
          "xtype": { "const": "typetwo" }
        },
        "required": ["xtype"]
      },
      "then": { "$ref": "#/definitions/typetwo" }
    }
  ],
  "definitions": {
    "typeone": {
      "type": "object",
      "properties": {
        "xtype": {},
        "num": { "type": "number" }
      },
      "required": ["num"],
      "additionalProperties": false
    },
    "typetwo": {
      "type": "object",
      "properties": {
        "xtype": {},
        "str": { "type": "string" }
      },
      "required": ["str"],
      "additionalProperties": false
    }
  }
}

只要对模型做一个小小的更改,您就可以使用dependencies来获得一个更简单、更简洁的模式。您可以拥有一个与类型名称相对应的属性,而不是具有"xtytpe“属性。例如,{ "typeone": true }

代码语言:javascript
复制
{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "dependencies": {
    "typeone": {
      "type": "object",
      "properties": {
        "typeone": {},
        "num": { "type": "number" }
      },
      "required": ["num"],
      "additionalProperties": false
    },
    "typetwo": {
      "type": "object",
      "properties": {
        "typetwo": {},
        "str": { "type": "string" }
      },
      "required": ["str"],
      "additionalProperties": false
    }
  }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64155999

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档