Skip to content

Commit 06fb038

Browse files
committed
JSON Schema Validation.
1 parent c209915 commit 06fb038

File tree

6 files changed

+101
-17
lines changed

6 files changed

+101
-17
lines changed

nodejs/scripts/jsonschemas/jsonix/Jsonix.jsonschema

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
"type" : "object",
77
"properties" : {
88
"year" : {
9-
"$ref" : "#/definitions/integer"
9+
"$ref" : "http://www.jsonix.org/jsonschemas/w3c/2001/XMLSchema.jsonschema#/definitions/integer"
1010
},
1111
"month" : {
1212
"allOf" : [ {
13-
"$ref" : "#/definitions/unsignedByte"
13+
"$ref" : "http://www.jsonix.org/jsonschemas/w3c/2001/XMLSchema.jsonschema#/definitions/unsignedByte"
1414
}, {
1515
"minimum" : 1,
1616
"exclusiveMinimum" : false,
@@ -20,7 +20,7 @@
2020
},
2121
"day" : {
2222
"allOf" : [ {
23-
"$ref" : "#/definitions/unsignedByte"
23+
"$ref" : "http://www.jsonix.org/jsonschemas/w3c/2001/XMLSchema.jsonschema#/definitions/unsignedByte"
2424
}, {
2525
"minimum" : 1,
2626
"exclusiveMinimum" : false,
@@ -30,7 +30,7 @@
3030
},
3131
"hour" : {
3232
"allOf" : [ {
33-
"$ref" : "#/definitions/unsignedByte"
33+
"$ref" : "http://www.jsonix.org/jsonschemas/w3c/2001/XMLSchema.jsonschema#/definitions/unsignedByte"
3434
}, {
3535
"minimum" : 0,
3636
"exclusiveMinimum" : false,
@@ -40,7 +40,7 @@
4040
},
4141
"minute" : {
4242
"allOf" : [ {
43-
"$ref" : "#/definitions/unsignedByte"
43+
"$ref" : "http://www.jsonix.org/jsonschemas/w3c/2001/XMLSchema.jsonschema#/definitions/unsignedByte"
4444
}, {
4545
"minimum" : 0,
4646
"exclusiveMinimum" : false,
@@ -50,7 +50,7 @@
5050
},
5151
"second" : {
5252
"allOf" : [ {
53-
"$ref" : "#/definitions/unsignedByte"
53+
"$ref" : "http://www.jsonix.org/jsonschemas/w3c/2001/XMLSchema.jsonschema#/definitions/unsignedByte"
5454
}, {
5555
"minimum" : 0,
5656
"exclusiveMinimum" : false,
@@ -60,7 +60,7 @@
6060
},
6161
"fractionalSecond" : {
6262
"allOf" : [ {
63-
"$ref" : "#/definitions/decimal"
63+
"$ref" : "http://www.jsonix.org/jsonschemas/w3c/2001/XMLSchema.jsonschema#/definitions/decimal"
6464
}, {
6565
"minimum" : 0,
6666
"exclusiveMinimum" : false,
@@ -70,7 +70,7 @@
7070
},
7171
"timezone" : {
7272
"allOf" : [ {
73-
"$ref" : "#/definitions/integer"
73+
"$ref" : "http://www.jsonix.org/jsonschemas/w3c/2001/XMLSchema.jsonschema#/definitions/integer"
7474
}, {
7575
"minimum" : -1440,
7676
"exclusiveMinimum" : false,

nodejs/scripts/jsonschemas/w3c/2001/XMLSchema.jsonschema

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -321,11 +321,11 @@
321321
},
322322
"prefix" : {
323323
"anyOf" : [ {
324-
"$ref" : "#/definitions/NCName"
325-
}, {
326-
"type" : "string",
327-
"enum" : [ "" ]
328-
}
324+
"$ref" : "#/definitions/NCName"
325+
}, {
326+
"type" : "string",
327+
"enum" : [ "" ]
328+
} ]
329329
}
330330
},
331331
"required" : [ "localPart" ]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
/PO.js
2+
/PO.jsonschema

nodejs/tests/po/package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@
66
],
77
"main": "jsonix-tests-po.js",
88
"dependencies": {
9-
"jsonix": "2.x.x",
10-
"jsonix-schema-compiler": "2.x.x"
9+
"jsonix": ">=2.3.0",
10+
"jsonix-schema-compiler": ">=2.3.6",
11+
"ajv": ">=1.2.1"
1112
},
1213
"devDependencies" : {
1314
"nodeunit" : "~0.x.x"
1415
},
1516
"scripts": {
16-
"prepublish" : "java -jar node_modules/jsonix-schema-compiler/lib/jsonix-schema-compiler-full.jar -compact -logLevel TRACE -d mappings purchaseorder.xsd -b bindings.xjb",
17+
"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",
1718
"test": "nodeunit tests/tests.js"
1819
}
1920
}

nodejs/tests/po/tests/po-tests.js

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
var fs = require('fs');
2+
var Ajv = require('ajv');
13
var Jsonix = require('jsonix').Jsonix;
24
var PO = require('../mappings/PO').PO;
35

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

1720
var po = poElement.value;
1821

@@ -30,5 +33,33 @@ module.exports = {
3033
test.equal('US', po.billTo.country);
3134
test.done();
3235
});
33-
}
36+
},
37+
"ValidateJson": function (test) {
38+
// Load JSON Schemas
39+
var XMLSchemaJsonSchema = JSON.parse(fs.readFileSync('./node_modules/jsonix/jsonschemas/w3c/2001/XMLSchema.jsonschema').toString());
40+
var JsonixJsonSchema = JSON.parse(fs.readFileSync('./node_modules/jsonix/jsonschemas/Jsonix/Jsonix.jsonschema').toString());
41+
var POJsonSchema = JSON.parse(fs.readFileSync('./mappings/PO.jsonschema').toString());
42+
43+
var ajv = new Ajv();
44+
ajv.addSchema(XMLSchemaJsonSchema, 'http://www.jsonix.org/jsonschemas/w3c/2001/XMLSchema.jsonschema');
45+
ajv.addSchema(JsonixJsonSchema, 'http://www.jsonix.org/jsonschemas/jsonix/Jsonix.jsonschema');
46+
var validate = ajv.compile(POJsonSchema);
47+
48+
var po = JSON.parse(fs.readFileSync("tests/po.json").toString());
49+
50+
console.log('Validating.');
51+
var valid = validate(po);
52+
if (!valid) {
53+
console.log('Validation failed.');
54+
console.log('Validation errors:');
55+
console.log(validate.errors);
56+
}
57+
test.ok(valid, 'Validation failed.');
58+
var context = new Jsonix.Context([ PO ]);
59+
var marshaller = context.createMarshaller();
60+
var marshalled = marshaller.marshalString(po);
61+
console.log('Marshalled XML:');
62+
console.log(marshalled);
63+
test.done();
64+
}
3465
};

nodejs/tests/po/tests/po.json

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
{
2+
"name": {
3+
"localPart": "purchaseOrder"
4+
},
5+
"value": {
6+
"orderDate": {
7+
"year": 1999,
8+
"month": 10,
9+
"day": 20
10+
},
11+
"shipTo": {
12+
"country": "US",
13+
"name": "Alice Smith",
14+
"street": "123 Maple Street",
15+
"city": "Mill Valley",
16+
"state": "CA",
17+
"zip": 90952
18+
},
19+
"billTo": {
20+
"country": "US",
21+
"name": "Robert Smith",
22+
"street": "8 Oak Avenue",
23+
"city": "Old Town",
24+
"state": "PA",
25+
"zip": 95819
26+
},
27+
"comment": "Hurry, my lawn is going wild!",
28+
"items": {
29+
"item": [
30+
{
31+
"partNum": "872-AA",
32+
"productName": "Lawnmower",
33+
"quantity": 1,
34+
"usPrice": 148.95,
35+
"comment": "Confirm this is electric"
36+
},
37+
{
38+
"partNum": "926-AA",
39+
"productName": "Baby Monitor",
40+
"quantity": 1,
41+
"usPrice": 39.98,
42+
"shipDate": {
43+
"year": 1999,
44+
"month": 5,
45+
"day": 21
46+
}
47+
}
48+
]
49+
}
50+
}
51+
}

0 commit comments

Comments
 (0)