3

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?

J1617
  • 91
  • 1
  • 10
  • According to JSON Scheme, `exclusiveMaximum` must be a number. But in the `www.jsonix.org/jsonschemas/w3c/2001/XMLSchema.jsonschema` the `exclusiveMaximum` is false and ajv is throwing. – Faizuddin Mohammed Feb 20 '18 at 07:37
  • https://github.com/json-schema-org/json-schema-spec/blob/master/schema.json and you can see `"exclusiveMaximum": { "type": "number" },` – Faizuddin Mohammed Feb 20 '18 at 07:38

3 Answers3

4

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');
J1617
  • 91
  • 1
  • 10
0

The change of exclusiveMaximum from boolean to number happened with Draft-06/07 of JSON Schema.

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

Use this and then 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');
Faizuddin Mohammed
  • 4,118
  • 5
  • 27
  • 51
0

Author of Jsonix here.

As it is pointed out in the documentation, JSON Schema generation is an experimental feature. So it would be not surprizing that it fails. You are welcome to file issues.

lexicore
  • 42,748
  • 17
  • 132
  • 221