@@ -10,107 +10,113 @@ var request = require("request")
10
10
var schema = JSON . parse ( fs . readFileSync ( "./schemas/v2.0/schema.json" , 'utf8' ) ) ;
11
11
var validators = ( process . env . VALIDATORS || "tv4,zschema" ) . split ( "," )
12
12
13
- var setupTV4 = function ( ) {
14
- var deferred = Q . defer ( ) ;
15
- request ( {
16
- url : "http://json-schema.org/draft-04/schema" ,
17
- json : true
18
- } , function ( error , response , body ) {
19
- if ( ! error && response . statusCode === 200 ) {
20
- tv4 . addSchema ( "http://json-schema.org/draft-04/schema" , body ) ;
21
- deferred . resolve ( ) ;
22
- } else {
23
- deferred . reject ( new Error ( "Request failed" ) ) ;
13
+ var validationMethods = {
14
+ tv4 : {
15
+ setup : function ( ) {
16
+ var deferred = Q . defer ( ) ;
17
+ request ( {
18
+ url : "http://json-schema.org/draft-04/schema" ,
19
+ json : true
20
+ } , function ( error , response , body ) {
21
+ if ( ! error && response . statusCode === 200 ) {
22
+ tv4 . addSchema ( "http://json-schema.org/draft-04/schema" , body ) ;
23
+ deferred . resolve ( ) ;
24
+ } else {
25
+ deferred . reject ( new Error ( "Request failed" ) ) ;
26
+ }
27
+ } )
28
+ return deferred . promise ;
29
+ } ,
30
+ validate : function ( schema , data ) {
31
+ var result = tv4 . validateMultiple ( data , schema , true , true ) ;
32
+ assert ( result . missing . length == 0 , "Missing schemas: " + result . missing )
33
+ if ( result . errors . length > 0 ) {
34
+ for ( i in result . errors ) {
35
+ // Remove stack trace so results are readable
36
+ delete result . errors [ i ] . stack ;
37
+ }
38
+ }
39
+ assert ( result . valid == true , "Validation failed: " + JSON . stringify ( result , null , "\t" ) ) ;
40
+ } ,
41
+ } ,
42
+ zschema : {
43
+ setup : function ( ) {
44
+ var deferred = Q . defer ( )
45
+ request ( {
46
+ url : "http://json-schema.org/draft-04/schema"
47
+ } , function ( error , response , body ) {
48
+ if ( ! error && response . statusCode === 200 ) {
49
+ ZSchema . setRemoteReference ( "http://json-schema.org/draft-04/schema" , body ) ;
50
+ deferred . resolve ( ) ;
51
+ } else {
52
+ deferred . reject ( new Error ( "Request failed" ) ) ;
53
+ }
54
+ } )
55
+ return deferred . promise ;
56
+ } ,
57
+ validate : function ( schema , data ) {
58
+ var validator = new ZSchema ( { sync : true } ) ;
59
+ var valid = validator . validate ( data , schema ) ;
60
+ if ( ! valid ) {
61
+ var error = validator . getLastError ( ) ;
62
+ throw new Error ( "ZSchema failed: " + JSON . stringify ( error , null , "\t" ) ) ;
63
+ }
64
+ assert ( valid == true )
24
65
}
66
+ }
67
+ }
68
+
69
+ var setupValidators = function ( done ) {
70
+ var setupPromises = [ ]
71
+ validators . forEach ( function ( validator ) {
72
+ setupPromises . push ( validationMethods [ validator ] . setup ( ) )
73
+ } ) ;
74
+ return Q . all ( setupPromises ) . then ( function ( ) {
75
+ done ( ) ;
25
76
} )
26
- return deferred . promise ;
27
77
}
28
78
29
- var setupZSchema = function ( ) {
30
- var deferred = Q . defer ( )
31
- request ( {
32
- url : "http://json-schema.org/draft-04/schema"
33
- } , function ( error , response , body ) {
34
- if ( ! error && response . statusCode === 200 ) {
35
- ZSchema . setRemoteReference ( "http://json-schema.org/draft-04/schema" , body ) ;
36
- deferred . resolve ( ) ;
37
- } else {
38
- deferred . reject ( new Error ( "Request failed" ) ) ;
39
- }
79
+ var createYAMLTest = function ( file , validator ) {
80
+ if ( validators . indexOf ( validator ) == - 1 )
81
+ return ;
82
+
83
+ it ( "should validate " + file + " with " + validator , function ( ) {
84
+ var data = yaml . safeLoad ( fs . readFileSync ( file , 'utf8' ) ) ;
85
+ validationMethods [ validator ] . validate ( schema , data ) ;
86
+ } )
87
+ }
88
+
89
+ var createJSONTest = function ( file , validator ) {
90
+ if ( validators . indexOf ( validator ) == - 1 )
91
+ return ;
92
+
93
+ it ( "should validate " + file + " with " + validator , function ( ) {
94
+ var data = JSON . parse ( fs . readFileSync ( file , 'utf8' ) ) ;
95
+ validationMethods [ validator ] . validate ( schema , data ) ;
40
96
} )
41
- return deferred . promise ;
42
97
}
43
98
44
99
describe ( 'JSON Samples' , function ( ) {
45
100
before ( function ( done ) {
46
- return Q . all ( [ setupTV4 ( ) , setupZSchema ( ) ] ) . then ( function ( ) {
47
- done ( ) ;
48
- } )
101
+ setupValidators ( done ) ;
49
102
} )
103
+
50
104
files = glob . sync ( "./examples/**/*.json" )
51
- for ( i in files ) {
52
- var file = files [ i ] ;
53
- var data = JSON . parse ( fs . readFileSync ( file , 'utf8' ) ) ;
54
- if ( validators . indexOf ( "tv4" ) != - 1 ) {
55
- it ( "should validate " + file + " with tv4" , function ( ) {
56
- validateWithTV4 ( schema , data )
57
- } )
58
- }
59
- if ( validators . indexOf ( "zschema" ) != - 1 ) {
60
- it ( "should validate " + file + " with zschema" , function ( ) {
61
- validateWithZSchema ( schema , data ) ;
62
- } )
63
- }
64
- }
105
+ validators . forEach ( function ( validator ) {
106
+ files . forEach ( function ( file ) {
107
+ createJSONTest ( file , validator ) ;
108
+ } )
109
+ } )
65
110
} )
66
111
67
112
describe ( 'YAML Samples' , function ( ) {
113
+ before ( function ( done ) {
114
+ setupValidators ( done ) ;
115
+ } )
68
116
files = glob . sync ( "./examples/**/*.yaml" )
69
- for ( i in files ) {
70
- var file = files [ i ] ;
71
- var data = yaml . safeLoad ( fs . readFileSync ( file , 'utf8' ) ) ;
72
- if ( validators . indexOf ( "tv4" ) != - 1 ) {
73
- it ( "should validate " + file + " with tv4" , function ( ) {
74
- validateWithTV4 ( schema , data )
75
- } )
76
- }
77
- if ( validators . indexOf ( "zschema" ) != - 1 ) {
78
- it ( "should validate " + file + " with zschema" , function ( ) {
79
- validateWithZSchema ( schema , data ) ;
80
- } )
81
- }
82
- }
117
+ validators . forEach ( function ( validator ) {
118
+ files . forEach ( function ( file ) {
119
+ createYAMLTest ( file , validator ) ;
120
+ } )
121
+ } )
83
122
} )
84
-
85
- var validateWithTV4 = function ( schema , data ) {
86
- var result = tv4 . validateMultiple ( data , schema , true , true ) ;
87
- assert ( result . missing . length == 0 , "Missing schemas: " + result . missing )
88
- if ( result . errors . length > 0 ) {
89
- for ( i in result . errors ) {
90
- // Remove stack trace so results are readable
91
- delete result . errors [ i ] . stack ;
92
- }
93
- }
94
- assert ( result . valid == true , "Validation failed: " + JSON . stringify ( result , null , "\t" ) ) ;
95
- }
96
-
97
- var validateWithZSchema = function ( schema , data ) {
98
- var validator = new ZSchema ( { sync : true } ) ;
99
- var valid = validator . validate ( data , schema ) ;
100
- if ( ! valid ) {
101
- var error = validator . getLastError ( ) ;
102
- throw new Error ( "ZSchema failed: " + JSON . stringify ( error , null , "\t" ) ) ;
103
- }
104
- assert ( valid == true )
105
-
106
- // Async version
107
- // ZSchema.validate(data, schema)
108
- // .then(function(report){
109
- // expect(report.warnings).to.eql([]);
110
- // expect(report.successful).to.be.true;
111
- // })
112
- // .catch(function(err){
113
- // expect(err.errors).to.eql([]);
114
- // throw "Failed";
115
- // }).done();
116
- }
0 commit comments