繁体   English   中英

Json Schema oneOf ,未按预期工作

[英]Json Schema oneOf , not working as expected

我正在创建一个 json 模式,目的是生成单个产品代码数组或产品代码数组数组。 简而言之,我希望能够返回 productCodes: [12224, 444444] 或 productCodesArray: [[12222,55555],[11111,99999]] 结果。 我尝试了一种解决方案,该解决方案部分有效,但两者都可以。 被)允许。 我可能误读了规范。 我怎样才能做到这一点? 我尝试使用 oneOf,这是我的解决方案:

             {
                 "$schema": "http://json-schema.org/draft-07/schema#",
                  "$id":"https://examples.com/schemas/my-array-schema",
                  "type":"object",
                  "required": ["productCodesResult"],
                  "properties": {
                  "additionalItems": false,
                  "productCodesResult": {
                  "type": "object",
                  "properties": {
                  "oneOf": {
                       "reasons": {
                       "$ref": "#/definitions/productCodesDef"
                   },
                      "reasonsArray": {
                      "$ref": "#/definitions/productCodesArrayDef"
                  }
                }
               }
              }
             },
                 "definitions": {
                 "productCodesDef": {
                 "type": "object",
                  "required": ["productCodes"],
                  "properties":{
                  "productsIds": {
                  "type": "array",
                  "items": {
                  "type": "integer",
                  "minItems": 1
                          }
                    }
                  }.   
                },
                    "productCodesArrayDef": {
                    "type": "object",
                    "required": ["reasonsArray"],
                    "properties":{
                    "productsCodesArray": {
                    "type": "array",
                    "items": {
                    "type": "array",
                    "items":{
                    "type": "integer",
                    "minItems": 1
                   }
                 }
               }
              }
            }
           }
         }

Ajv测试了这个。 productCodes属性级别指定oneOf 添加两个选项,一个接受字符串数组( string[] ),另一个接受字符串数组( string[][] )。

const schema = {
  type: 'object',
  required: ['productCodes'],
  properties: {
    productCodes: {
      type: 'array',
      oneOf: [
        {
          type: 'array',
          minItems: 1,
          items: {
            type: 'string',
          },
        },
        {
          type: 'array',
          minItems: 1,
          items: {
            type: 'array',
            minItems: 1,
            items: {
              type: 'string',
            },
          },
        },
      ],
    },
  },
};

无需为属性赋予不同的名称,只需将其productCodes 在上面的示例中,我将其设为必需,因此传递一个空对象将无效。

import Ajv from 'ajv';

const data = {
  // productCodes: ['123', '345'],
  productCodes: [
    ['123', '345'],
    ['123', '345'],
  ],
};

const validate = this.ajv.compile(schema);
const valid = validate(data);
console.log(valid);

如果出于任何要求,属性必须具有不同的名称,则将它们定义为 2 个不同的属性。 在根级别上使用oneOf可以使它们互斥。 productCodesproductCodesArray必须存在。

const schema = {
  type: 'object',
  required: [],
  properties: {
    productCodes: {
      type: 'array',
      minItems: 1,
      items: {
        type: 'string',
      },
    },
    productCodesArray: {
      type: 'array',
      minItems: 1,
      items: {
        type: 'array',
        minItems: 1,
        items: {
          type: 'string',
        },
      },
    },
  },
  oneOf: [
    { type: 'object', required: ['productCodes'] },
    { type: 'object', required: ['productCodesArray'] },
  ],
};

作为替代方案,您也可以使用allOfnot来执行此操作。

const schema = {
  type: 'object',
  required: [],
  properties: {
    productCodes: { ... }
    productCodesArray: { ... }
  },
  allOf: [
    {
      not: {
        type: 'object',
        required: ['productCodes', 'productCodesArray'],
      },
    },
  ],
};

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM