Skip to content

Commit a1cbb8a

Browse files
committed
Merge pull request #87 from maxlinc/validate_tests
Fix and refactor the mocha tests
2 parents 9f80241 + 0815dc9 commit a1cbb8a

File tree

2 files changed

+96
-90
lines changed

2 files changed

+96
-90
lines changed

.travis.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ env:
1010
- VALIDATORS=tv4
1111
- VALIDATORS=zschema
1212
matrix:
13-
allow_failrues:
14-
-env: VALIDATORS=tv4
13+
allow_failures:
14+
- env: VALIDATORS=tv4

test/validate.js

+94-88
Original file line numberDiff line numberDiff line change
@@ -10,107 +10,113 @@ var request = require("request")
1010
var schema = JSON.parse(fs.readFileSync("./schemas/v2.0/schema.json", 'utf8'));
1111
var validators = (process.env.VALIDATORS || "tv4,zschema").split(",")
1212

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)
2465
}
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();
2576
})
26-
return deferred.promise;
2777
}
2878

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);
4096
})
41-
return deferred.promise;
4297
}
4398

4499
describe('JSON Samples', function() {
45100
before(function(done) {
46-
return Q.all([setupTV4(), setupZSchema()]).then(function() {
47-
done();
48-
})
101+
setupValidators(done);
49102
})
103+
50104
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+
})
65110
})
66111

67112
describe('YAML Samples', function() {
113+
before(function(done) {
114+
setupValidators(done);
115+
})
68116
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+
})
83122
})
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

Comments
 (0)