2017-06-09 22 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

相關問題