Error: 'schema should be object or boolean' in ajv.addSchema
Error: 'schema should be object or boolean' in ajv.addSchema
我正在尝试重构一些 JSONSchema 检查以使用 $ref
include 将一个模式包含在另一个模式中。
由于我使用 AJV 验证模式,我一直在尝试使用 addSchema
函数加载两个模式,但我一直收到错误 schema should be object or boolean
但到目前为止据我所知,我已经正确定义了架构。
我的 JS 代码如下:
var Ajv = require('ajv');
var ajv = new Ajv({ allErrors: 'true', verbose: 'true' });
ajv.addMetaSchema(require('ajv/lib/refs/json-schema-draft-06.json'));
ajv.addSchema('../../schema/sport_schema/tennis_sport_schema.json', 'tennis_sport_schema.json');
ajv.addSchema('../../schema/sport_schema/tennis_event_schema.json', 'tennis_event_schema.json');
架构 tennis_sport_schema.json
如下所示:
{
"$schema": "http://json-schema.org/draft-06/schema#",
"title": "Tennis Sport endpoint schema",
"description": "The Tennis Sport endpoint for Sport API",
"required": ["offset", "limit", "hasNext", "hasPrevious", "items"],
"type": "object",
"properties": {
"offset": {
"type": "number",
"const": 0
},
"limit": {
"type": "number",
"const": 20
},
"hasNext": {
"type": "boolean",
"const": true
},
"hasPrevious": {
"type": "boolean",
"const": false
},
"items": {
"type": "array",
"minItems": 1,
"maxItems": 20,
"items": {
"$ref": "tennis_event_schema.json"
}
}
}
}
错误输出为:
mocha "test/sport_tests/tennis_schema.js"
/Users/framps01/Workspace/sport-store-test-framework/node_modules/ajv/lib/ajv.js:300
throw new Error('schema should be object or boolean');
^
Error: schema should be object or boolean
at Ajv._addSchema (/Users/framps01/Workspace/sport-store-test-framework/node_modules/ajv/lib/ajv.js:300:15)
at Ajv.addSchema (/Users/framps01/Workspace/sport-store-test-framework/node_modules/ajv/lib/ajv.js:136:31)
at Object.<anonymous> (/Users/framps01/Workspace/sport-store-test-framework/test/sport_tests/tennis_schema.js:4:5)
at Module._compile (module.js:635:30)
at Object.Module._extensions..js (module.js:646:10)
at Module.load (module.js:554:32)
at tryModuleLoad (module.js:497:12)
at Function.Module._load (module.js:489:3)
at Module.require (module.js:579:17)
at require (internal/module.js:11:18)
at /Users/framps01/Workspace/sport-store-test-framework/node_modules/mocha/lib/mocha.js:250:27
at Array.forEach (<anonymous>)
at Mocha.loadFiles (/Users/framps01/Workspace/sport-store-test-framework/node_modules/mocha/lib/mocha.js:247:14)
at Mocha.run (/Users/framps01/Workspace/sport-store-test-framework/node_modules/mocha/lib/mocha.js:576:10)
at Object.<anonymous> (/Users/framps01/Workspace/sport-store-test-framework/node_modules/mocha/bin/_mocha:637:18)
at Module._compile (module.js:635:30)
at Object.Module._extensions..js (module.js:646:10)
at Module.load (module.js:554:32)
at tryModuleLoad (module.js:497:12)
at Function.Module._load (module.js:489:3)
at Function.Module.runMain (module.js:676:10)
at startup (bootstrap_node.js:187:16)
at bootstrap_node.js:608:3
npm ERR! Test failed. See above for more details.
谁能指出我哪里出错了? tennis_sport_schema.json
被定义为 'object' 所以不确定为什么会抛出错误以表明它不是。
addSchema
的第一个参数需要是一个对象,而不是 json 文件的路径。
.addSchema(Array<Object>|Object schema [, String key]) -> Ajv
所以会是:
let obj = {"$id": "mySuperSchema"}; //inc the rest of your json schema object.
ajv.addSchema(obj, 'mySuperSchema');
或者,它接受一个模式对象数组,假设在其中正确设置了 $id(然后忽略第二个参数)。
我正在尝试重构一些 JSONSchema 检查以使用 $ref
include 将一个模式包含在另一个模式中。
由于我使用 AJV 验证模式,我一直在尝试使用 addSchema
函数加载两个模式,但我一直收到错误 schema should be object or boolean
但到目前为止据我所知,我已经正确定义了架构。
我的 JS 代码如下:
var Ajv = require('ajv');
var ajv = new Ajv({ allErrors: 'true', verbose: 'true' });
ajv.addMetaSchema(require('ajv/lib/refs/json-schema-draft-06.json'));
ajv.addSchema('../../schema/sport_schema/tennis_sport_schema.json', 'tennis_sport_schema.json');
ajv.addSchema('../../schema/sport_schema/tennis_event_schema.json', 'tennis_event_schema.json');
架构 tennis_sport_schema.json
如下所示:
{
"$schema": "http://json-schema.org/draft-06/schema#",
"title": "Tennis Sport endpoint schema",
"description": "The Tennis Sport endpoint for Sport API",
"required": ["offset", "limit", "hasNext", "hasPrevious", "items"],
"type": "object",
"properties": {
"offset": {
"type": "number",
"const": 0
},
"limit": {
"type": "number",
"const": 20
},
"hasNext": {
"type": "boolean",
"const": true
},
"hasPrevious": {
"type": "boolean",
"const": false
},
"items": {
"type": "array",
"minItems": 1,
"maxItems": 20,
"items": {
"$ref": "tennis_event_schema.json"
}
}
}
}
错误输出为:
mocha "test/sport_tests/tennis_schema.js"
/Users/framps01/Workspace/sport-store-test-framework/node_modules/ajv/lib/ajv.js:300
throw new Error('schema should be object or boolean');
^
Error: schema should be object or boolean
at Ajv._addSchema (/Users/framps01/Workspace/sport-store-test-framework/node_modules/ajv/lib/ajv.js:300:15)
at Ajv.addSchema (/Users/framps01/Workspace/sport-store-test-framework/node_modules/ajv/lib/ajv.js:136:31)
at Object.<anonymous> (/Users/framps01/Workspace/sport-store-test-framework/test/sport_tests/tennis_schema.js:4:5)
at Module._compile (module.js:635:30)
at Object.Module._extensions..js (module.js:646:10)
at Module.load (module.js:554:32)
at tryModuleLoad (module.js:497:12)
at Function.Module._load (module.js:489:3)
at Module.require (module.js:579:17)
at require (internal/module.js:11:18)
at /Users/framps01/Workspace/sport-store-test-framework/node_modules/mocha/lib/mocha.js:250:27
at Array.forEach (<anonymous>)
at Mocha.loadFiles (/Users/framps01/Workspace/sport-store-test-framework/node_modules/mocha/lib/mocha.js:247:14)
at Mocha.run (/Users/framps01/Workspace/sport-store-test-framework/node_modules/mocha/lib/mocha.js:576:10)
at Object.<anonymous> (/Users/framps01/Workspace/sport-store-test-framework/node_modules/mocha/bin/_mocha:637:18)
at Module._compile (module.js:635:30)
at Object.Module._extensions..js (module.js:646:10)
at Module.load (module.js:554:32)
at tryModuleLoad (module.js:497:12)
at Function.Module._load (module.js:489:3)
at Function.Module.runMain (module.js:676:10)
at startup (bootstrap_node.js:187:16)
at bootstrap_node.js:608:3
npm ERR! Test failed. See above for more details.
谁能指出我哪里出错了? tennis_sport_schema.json
被定义为 'object' 所以不确定为什么会抛出错误以表明它不是。
addSchema
的第一个参数需要是一个对象,而不是 json 文件的路径。
.addSchema(Array<Object>|Object schema [, String key]) -> Ajv
所以会是:
let obj = {"$id": "mySuperSchema"}; //inc the rest of your json schema object.
ajv.addSchema(obj, 'mySuperSchema');
或者,它接受一个模式对象数组,假设在其中正确设置了 $id(然后忽略第二个参数)。