【问题标题】:Schema <[object Object]> already exists with different definitionSchema <[object Object]> 已经存在,但定义不同
【发布时间】:2017-06-09 12:22:57
【问题描述】:

在尝试将 2 个架构合二为一时出现 Schema &lt;[object Object]&gt; already exists with different definition 错误。

如果我做错了,请纠正我:

Coupons Schema 在 coupons.js 中

const COUPONS_SCHEMA = {
  "id": "/Coupons",
  "items": {
    "id": "/items",
    "properties": {
      "Description": {
        "type": "string"
      },
      "Ean": {
        "type": "string"
      },
      "ExpiryDate": {
        "type": "string"
      },
      "Id": {
        "type": "string"
      },
      "Name": {
        "type": "string"
      },
      "StartDate": {
        "type": "string"
      },
      "Type": {
        "type": "string"
      },
      "VoucherValue": {
        "type": "string"
      }
    },
    "type": "object"
  },
  "type": "array"
};

export default COUPONS_SCHEMA;

rewards Schema 在rewards.js

const REWARDS_SCHEMA = {
    "id": "/Rewards",
    "items": {
        "id": "/items",
        "properties": {
            "PromotionId": {

                "type": "string"
            },
            "Reward Amount": {

                "type": "string"
            },
            "RewardType": {

                "type": "string"
            }
        },
        "type": "object"
    },
    "type": "array"
};

export default REWARDS_SCHEMA;

我在 Discounts Schema

中引用了上述定义的模式

import { Validator } from 'jsonschema';
import Coupons from './coupons';
import Rewards from './rewards';
let validator = new Validator();


const DISCOUNTS_SCHEMA = {
  "id": "/Discounts",
  "properties": {
    "Coupons": {
    "$ref": "/Coupons"
    },
    "PromotionalClubCardPoints": {
      "type": "string"
    },
    "Rewards": {
      "$ref": "/Rewards"
    },
    "StaffDiscount": {
      "type": "string"
    },
    "StandardClubCardPoints": {
      "type": "string"
    },
    "TotalClubCardPoints": {
      "type": "string"
    },
    "TotalCoupons": {
      "type": "string"
    },
    "TotalGiftCards": {
      "type": "string"
    },
    "TotalGreenClubCardPoints": {
      "type": "string"
    },
    "TotalSavings": {
      "type": "string"
    },
    "TotalVouchers": {
      "type": "string"
    }
  },
  "type": "object"
};

validator.addSchema(Coupons,'/Discounts');
validator.addSchema(Rewards,'/Discounts');


export default DISCOUNTS_SCHEMA;

and getting the below error 

 throw new Error('Schema <'+schema+'> already exists with different definition');
        ^

Error: Schema <[object Object]> already exists with different definition
    at Validator.addSubSchema (/Users/repo/node_modules/jsonschema/lib/validator.js:72:15)
    at Validator.addSubSchemaArray (/Users/repo/node_modules/jsonschema/lib/validator.js:99:10)
    at Validator.addSubSchema (/Users/repo/node_modules/jsonschema/lib/validator.js:80:8)
    at Validator.addSchema (/Users/repo/node_modules/jsonschema/lib/validator.js:48:8)
    at Object.<anonymous> (/Users/repo/src/schema/discounts.js:47:11)
    at Module._compile (module.js:570:32)
    at loader (/Users/repo/node_modules/babel-register/lib/node.js:144:5)
    at Object.require.extensions.(anonymous function) [as .js] (/Users/repo/node_modules/babel-register/lib/node.js:154:7)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)

如果在定义模式时做错了,请纠正我。

【问题讨论】:

  • 我们可以看看验证器类/函数吗?
  • 验证器函数是从 'jsonschema' npm 包中导入的

标签: javascript node.js npm jsonschema json-schema-validator


【解决方案1】:

问题可能是您在coupons.js 和rewards.js 中都使用了id“/items”。 ids 必须是普遍唯一的。这就是为什么它们应该是绝对 URI。

【讨论】:

  • id '/items' 用于将其定义为数组。我们可以将其作为我们的规则,因为它的父 ID 不同。
  • 你误解了我的意思。 ids 不会按照您期望的方式嵌套。它们需要是唯一的,而不考虑它们的定义位置。您可以改用“/Coupon/items”和“/Rewards/items”,这将是您所期望的,但无论父ids如何,您都不能使用“/items”两次。这不是 JSON Schema 中存在的概念。
【解决方案2】:

就我而言(其他任何人登陆这里以找到类似问题), 我们有一个这种格式的模式文件:

{
  "keyA": {
    "type": "string",
    "example": "0319739002",
    "id": "/keyA",
    "minLength": 1,
    "maxLength": 100
  },
  "keyB": {
    "keyA": {
      "type": "string",
      "example": "0186013001",
      "id": "/keyA",
      "minLength": 1,
      "maxLength": 100
    }
  }
}

其中 keyA 是 keyB 的一部分,但 id 重复了。删除(/重命名)“id”字段对我们有用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-05
    • 1970-01-01
    • 2019-05-08
    • 2015-10-15
    • 2014-01-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多