node.jsnode-modulesajvjsonix

AJV schema validation fails


I am using Jsonix, I have used below mentioned command to generate jsonix mapping and jsonix schema as,

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

it is properly generating mapping and schema, I want to validate JSON using AJV and the generated JSON Schema, so I have tried this,

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");
}

But it is throwing error

/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)

Actually the error comes while ajv addschema, Is I did anything wrong?


Solution

  • To continue using draft-04 schemas added meta: false to prevent adding 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';
    

    After adding this use addSchema to allow booleans for exclusiveMaximum

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