错误:ajv.addSchema中的“模式应为对象或布尔值”

时间:2018-06-28 12:11:51

标签: jsonschema ajv

我正在尝试重构一些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被定义为“对象”,因此不确定为什么会引发错误以表明它不是错误。

1 个答案:

答案 0 :(得分:0)

addSchema的第一个参数必须是一个对象,而不是json文件的路径。

.addSchema(Array<Object>|Object schema [, String key]) -> Ajv

source

所以应该是:

let obj = {"$id": "mySuperSchema"}; //inc the rest of your json schema object.
ajv.addSchema(obj, 'mySuperSchema');

或者,它接受一个架构对象数组,假设在其中正确设置了$ id(然后忽略第二个参数)。