Skip to content

Commit

Permalink
JSON Schema Validation.
Browse files Browse the repository at this point in the history
  • Loading branch information
highsource committed Aug 30, 2015
1 parent c209915 commit 06fb038
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 17 deletions.
16 changes: 8 additions & 8 deletions nodejs/scripts/jsonschemas/jsonix/Jsonix.jsonschema
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
"type" : "object",
"properties" : {
"year" : {
"$ref" : "#/definitions/integer"
"$ref" : "http://www.jsonix.org/jsonschemas/w3c/2001/XMLSchema.jsonschema#/definitions/integer"
},
"month" : {
"allOf" : [ {
"$ref" : "#/definitions/unsignedByte"
"$ref" : "http://www.jsonix.org/jsonschemas/w3c/2001/XMLSchema.jsonschema#/definitions/unsignedByte"
}, {
"minimum" : 1,
"exclusiveMinimum" : false,
Expand All @@ -20,7 +20,7 @@
},
"day" : {
"allOf" : [ {
"$ref" : "#/definitions/unsignedByte"
"$ref" : "http://www.jsonix.org/jsonschemas/w3c/2001/XMLSchema.jsonschema#/definitions/unsignedByte"
}, {
"minimum" : 1,
"exclusiveMinimum" : false,
Expand All @@ -30,7 +30,7 @@
},
"hour" : {
"allOf" : [ {
"$ref" : "#/definitions/unsignedByte"
"$ref" : "http://www.jsonix.org/jsonschemas/w3c/2001/XMLSchema.jsonschema#/definitions/unsignedByte"
}, {
"minimum" : 0,
"exclusiveMinimum" : false,
Expand All @@ -40,7 +40,7 @@
},
"minute" : {
"allOf" : [ {
"$ref" : "#/definitions/unsignedByte"
"$ref" : "http://www.jsonix.org/jsonschemas/w3c/2001/XMLSchema.jsonschema#/definitions/unsignedByte"
}, {
"minimum" : 0,
"exclusiveMinimum" : false,
Expand All @@ -50,7 +50,7 @@
},
"second" : {
"allOf" : [ {
"$ref" : "#/definitions/unsignedByte"
"$ref" : "http://www.jsonix.org/jsonschemas/w3c/2001/XMLSchema.jsonschema#/definitions/unsignedByte"
}, {
"minimum" : 0,
"exclusiveMinimum" : false,
Expand All @@ -60,7 +60,7 @@
},
"fractionalSecond" : {
"allOf" : [ {
"$ref" : "#/definitions/decimal"
"$ref" : "http://www.jsonix.org/jsonschemas/w3c/2001/XMLSchema.jsonschema#/definitions/decimal"
}, {
"minimum" : 0,
"exclusiveMinimum" : false,
Expand All @@ -70,7 +70,7 @@
},
"timezone" : {
"allOf" : [ {
"$ref" : "#/definitions/integer"
"$ref" : "http://www.jsonix.org/jsonschemas/w3c/2001/XMLSchema.jsonschema#/definitions/integer"
}, {
"minimum" : -1440,
"exclusiveMinimum" : false,
Expand Down
10 changes: 5 additions & 5 deletions nodejs/scripts/jsonschemas/w3c/2001/XMLSchema.jsonschema
Original file line number Diff line number Diff line change
Expand Up @@ -321,11 +321,11 @@
},
"prefix" : {
"anyOf" : [ {
"$ref" : "#/definitions/NCName"
}, {
"type" : "string",
"enum" : [ "" ]
}
"$ref" : "#/definitions/NCName"
}, {
"type" : "string",
"enum" : [ "" ]
} ]
}
},
"required" : [ "localPart" ]
Expand Down
1 change: 1 addition & 0 deletions nodejs/tests/po/mappings/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/PO.js
/PO.jsonschema
7 changes: 4 additions & 3 deletions nodejs/tests/po/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
],
"main": "jsonix-tests-po.js",
"dependencies": {
"jsonix": "2.x.x",
"jsonix-schema-compiler": "2.x.x"
"jsonix": ">=2.3.0",
"jsonix-schema-compiler": ">=2.3.6",
"ajv": ">=1.2.1"
},
"devDependencies" : {
"nodeunit" : "~0.x.x"
},
"scripts": {
"prepublish" : "java -jar node_modules/jsonix-schema-compiler/lib/jsonix-schema-compiler-full.jar -compact -logLevel TRACE -d mappings purchaseorder.xsd -b bindings.xjb",
"prepublish" : "java -jar node_modules/jsonix-schema-compiler/lib/jsonix-schema-compiler-full.jar -generateJsonSchema -compact -logLevel TRACE -d mappings purchaseorder.xsd -b bindings.xjb",
"test": "nodeunit tests/tests.js"
}
}
33 changes: 32 additions & 1 deletion nodejs/tests/po/tests/po-tests.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
var fs = require('fs');
var Ajv = require('ajv');
var Jsonix = require('jsonix').Jsonix;
var PO = require('../mappings/PO').PO;

Expand All @@ -13,6 +15,7 @@ module.exports = {
// Unmarshal the XML file
unmarshaller.unmarshalFile( 'tests/po.xml',
function(poElement) {
console.log(JSON.stringify(poElement, null, 4));

var po = poElement.value;

Expand All @@ -30,5 +33,33 @@ module.exports = {
test.equal('US', po.billTo.country);
test.done();
});
}
},
"ValidateJson": function (test) {
// Load JSON Schemas
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 POJsonSchema = JSON.parse(fs.readFileSync('./mappings/PO.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(POJsonSchema);

var po = JSON.parse(fs.readFileSync("tests/po.json").toString());

console.log('Validating.');
var valid = validate(po);
if (!valid) {
console.log('Validation failed.');
console.log('Validation errors:');
console.log(validate.errors);
}
test.ok(valid, 'Validation failed.');
var context = new Jsonix.Context([ PO ]);
var marshaller = context.createMarshaller();
var marshalled = marshaller.marshalString(po);
console.log('Marshalled XML:');
console.log(marshalled);
test.done();
}
};
51 changes: 51 additions & 0 deletions nodejs/tests/po/tests/po.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{
"name": {
"localPart": "purchaseOrder"
},
"value": {
"orderDate": {
"year": 1999,
"month": 10,
"day": 20
},
"shipTo": {
"country": "US",
"name": "Alice Smith",
"street": "123 Maple Street",
"city": "Mill Valley",
"state": "CA",
"zip": 90952
},
"billTo": {
"country": "US",
"name": "Robert Smith",
"street": "8 Oak Avenue",
"city": "Old Town",
"state": "PA",
"zip": 95819
},
"comment": "Hurry, my lawn is going wild!",
"items": {
"item": [
{
"partNum": "872-AA",
"productName": "Lawnmower",
"quantity": 1,
"usPrice": 148.95,
"comment": "Confirm this is electric"
},
{
"partNum": "926-AA",
"productName": "Baby Monitor",
"quantity": 1,
"usPrice": 39.98,
"shipDate": {
"year": 1999,
"month": 5,
"day": 21
}
}
]
}
}
}

0 comments on commit 06fb038

Please sign in to comment.