AJV 架构验证失败
AJV schema validation fails
我正在使用 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 addschema时出现的,我是不是做错了什么?
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');
要继续使用 draft-04 架构 添加 meta: false
以防止添加 draft-06 meta-schema 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 允许 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 的作者。
正如 documentation, JSON Schema generation is an experimental feature. So it would be not surprizing that it fails. You are welcome to file issues 中指出的那样。
我正在使用 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 addschema时出现的,我是不是做错了什么?
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');
要继续使用 draft-04 架构 添加 meta: false
以防止添加 draft-06 meta-schema 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 允许 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 的作者。
正如 documentation, JSON Schema generation is an experimental feature. So it would be not surprizing that it fails. You are welcome to file issues 中指出的那样。