Schema< [object Object]>已存在不同的定义

时间:2017-06-09 12:22:57

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

尝试在一个模式中引用2个模式时出现Schema <[object Object]> already exists with different definition错误。

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

  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.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;

折扣架构

中引用上面定义的架构

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)

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

2 个答案:

答案 0 :(得分:0)

问题可能是您使用id&#34; / items&#34;在coupons.js和rewards.js中。 id需要具有普遍的独特性。这就是为什么它们应该是绝对的URI。

答案 1 :(得分:0)

就我而言(其他人在这里找到类似问题), 我们有以下格式的架构文件:

Switch IDE boot JDK

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