-
Notifications
You must be signed in to change notification settings - Fork 91
/
AutomaticSchemaLoadingSpec.js
91 lines (69 loc) · 2.44 KB
/
AutomaticSchemaLoadingSpec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
"use strict";
var isBrowser = typeof window !== "undefined";
var ZSchema = require("../../src/ZSchema");
if (!isBrowser) {
var request = require("https").request;
}
function validateWithAutomaticDownloads(validator, data, schema, callback) {
var lastResult;
function finish() {
callback(validator.getLastErrors(), lastResult);
}
function validate() {
lastResult = validator.validate(data, schema);
var missingReferences = validator.getMissingRemoteReferences();
if (missingReferences.length > 0) {
var finished = 0;
missingReferences.forEach(function (url) {
request(url, function (response) {
var body = "";
response.on("data", function (chunk) { data += chunk; });
response.on("end", function () {
validator.setRemoteReference(url, JSON.parse(body));
finished++;
if (finished === missingReferences.length) {
validate();
}
});
});
});
} else {
finish();
}
}
validate();
}
describe("Automatic schema loading", function () {
it("should download schemas and validate successfully", function (done) {
if (isBrowser) {
// skip this test in browsers
expect(1).toBe(1);
done();
return;
}
var validator = new ZSchema();
var schema = { "$ref": "http://json-schema.org/draft-04/schema#" };
var data = { "minLength": 1 };
validateWithAutomaticDownloads(validator, data, schema, function (err, valid) {
expect(valid).toBe(true);
expect(err).toBe(null);
done();
});
});
it("should download schemas and fail validating", function (done) {
if (typeof window !== "undefined") {
// skip this test in browsers
expect(1).toBe(1);
done();
return;
}
var validator = new ZSchema();
var schema = { "$ref": "http://json-schema.org/draft-04/schema#" };
var data = { "minLength": -1 };
validateWithAutomaticDownloads(validator, data, schema, function (err, valid) {
expect(valid).toBe(false);
expect(err[0].code).toBe("MINIMUM");
done();
});
});
});