1+ var fs = require ( 'fs' ) ;
2+ var Ajv = require ( 'ajv' ) ;
13var Jsonix = require ( 'jsonix' ) . Jsonix ;
24var 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} ;
0 commit comments