2017-06-09 12 views
1

在尝试引用2个架构时出错Schema <[object Object]> already exists with different definition架构<[object object]>已存在不同定义

请纠正我,如果我做错了什么:在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)

请纠正我,如果我做错了什么定义模式。

+0

我们可以看看Validator类/函数吗? – Nevosis

+0

验证器功能从'jsonschema'npm包导入 –

回答

0

问题可能是您在coupons.js和rewards.js中都使用了id“/ items”。 id需要是普遍独特的。这就是为什么他们应该是绝对的URI。

+0

id'/ items'用于将其定义为数组。我们可以这样做,因为它的父母身份是不同的。 –

+0

你误解我告诉你的。 'id's不像你期待的那样嵌套。他们需要是独一无二的,无需考虑他们的定义。您可以使用“/ Coupon/items”和“/ Rewards/items”来代替它,它将成为您期望的内容,但不能使用“/ items”两次,而不考虑父'id's。这不是JSON模式中存在的概念。 – Jason

相关问题