AJV 架构验证失败



我正在使用Jsonix,我使用了下面提到的命令来生成jsonix映射和jsonix模式,

java -jar jsonix-schema-compiler-full.jar -generateJsonSchema -d mappings books.xsd

它正在正确生成映射和模式,我想使用AJV和生成的 JSON 模式验证 JSON,所以我尝试过这个,

var fs = require('fs');
var Ajv = require('ajv');
var XMLSchemaJsonSchema = JSON.parse(fs.readFileSync('../node_modules/jsonix/jsonschemas/w3c/2001/XMLSchema.jsonschema').toString());
var JsonixJsonSchema = JSON.parse(fs.readFileSync('../node_modules/jsonix/jsonschemas/jsonix/Jsonix.jsonschema').toString());
var booksJsonSchema = JSON.parse(fs.readFileSync('./books.jsonschema').toString());
var ajv = new Ajv();
ajv.addSchema(XMLSchemaJsonSchema, 'http://www.jsonix.org/jsonschemas/w3c/2001/XMLSchema.jsonschema');
ajv.addSchema(JsonixJsonSchema, 'http://www.jsonix.org/jsonschemas/jsonix/Jsonix.jsonschema');
var validate = ajv.compile(booksJsonSchema);
var data ={
"book": [
{
"@id": "bk001",
"author": "Writer",
"title": "The First Book",
"genre": "Fiction",
"price": "44.95",
"pub_date":2000-10-01,
"review": "An amazing story of nothing."
},
{
"@id": "bk002",
"author": "Poet",
"title": "The Poet's First Poem",
"genre": "Poem",
"price": "24.95",
"pub_date":2000-10-02,
"review": "Least poetic poems."
}
]
};
var valid = validate(data);
if (!valid) {
console.log('Validation failed errors:');
console.log(validate.errors);
}else{
console.log("successfully done validation");
}

但它是抛出错误

/Users/qliktag/Desktop/QAGG/qagUI2/Scripts/QLIKTAG-2602/node_modules/ajv/lib/ajv.js:183
else throw new Error(message);
^
Error: schema is invalid: data.definitions['nonPositiveInteger'].anyOf[0].exclusiveMaximum should be number
at Ajv.validateSchema (/Users/qliktag/Desktop/QAGG/qagUI2/testScripts/node_modules/ajv/lib/ajv.js:185:16)
at Ajv._addSchema (/Users/qliktag/Desktop/QAGG/qagUI2/Scripts/QLIKTAG-2602/node_modules/ajv/lib/ajv.js:316:10)
at Ajv.addSchema (/Users/qliktag/Desktop/QAGG/qagUI2/Scripts/QLIKTAG-2602/node_modules/ajv/lib/ajv.js:136:29)
at Object.<anonymous> (/Users/qliktag/Desktop/QAGG/qagUI2/Scripts/QLIKTAG-2602/mappings/ajvSample.js:248:5)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)

实际上错误是在ajv添加架构时出现的,我做错了什么吗?

继续使用 draft-04 架构,添加meta: false以防止添加 draft-06 元架构https://github.com/epoberezkin/ajv/releases/tag/5.0.0

var ajv = new Ajv({
schemaId: 'id',
meta: false, 
});
var metaSchema = require('../node_modules/ajv/lib/refs/json-schema-draft-04.json');
ajv.addMetaSchema(metaSchema);
ajv._opts.defaultMeta = metaSchema.id;
ajv._refs['http://json-schema.org/schema'] = 'http://json-schema.org/draft-04/schema';

添加此内容后,请使用addSchema允许布尔值用于独占最大值

ajv.addSchema(XMLSchemaJsonSchema, 'http://www.jsonix.org/jsonschemas/w3c/2001/XMLSchema.jsonschema');
ajv.addSchema(JsonixJsonSchema, 'http://www.jsonix.org/jsonschemas/jsonix/Jsonix.jsonschema');
exclusiveMaximum

从布尔值到数字的变化发生在 JSON 模式的 Draft-06/07 中。

// var ajv = new Ajv({schemaId: 'id'});
// If you want to use both draft-04 and draft-06/07 schemas:
var ajv = new Ajv({schemaId: 'auto'});
ajv.addMetaSchema(require('ajv/lib/refs/json-schema-draft-04.json'));

使用此选项,然后addSchema允许布尔值用于exclusiveMaximum

ajv.addSchema(XMLSchemaJsonSchema, 'http://www.jsonix.org/jsonschemas/w3c/2001/XMLSchema.jsonschema');
ajv.addSchema(JsonixJsonSchema, 'http://www.jsonix.org/jsonschemas/jsonix/Jsonix.jsonschema');

Jsonix的作者在这里。

正如文档中指出的那样,JSON 模式生成是一项实验性功能。因此,它失败也就不足为奇了。欢迎您提出问题。

相关内容

  • 没有找到相关文章

最新更新