-
Notifications
You must be signed in to change notification settings - Fork 17
/
index.js
190 lines (170 loc) · 5.98 KB
/
index.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
(function () {
'use strict';
/*jshint -W003*/
if (typeof require === 'function' && typeof exports === 'object' && typeof module === 'object') {
// NodeJS
module.exports = getPayload(
require('tv4'),
require('jsonpointer.js')
);
} else if (typeof define === 'function' && define.amd) {
// AMD
define('chai-json-schema', [
'tv4',
'jsonpointer'
], function (tv4, jsonpointer) {
return getPayload(tv4, jsonpointer);
});
} else {
// Other environment (usually <script> tag): plug in to global chai instance directly.
chai.use(getPayload(
window.tv4,
window.jsonpointer
));
}
function getPayload(tv4Module, jsonpointer) {
// return the chai plugin (a function)
return function (chai, utils) {
var assert = chai.assert;
var flag = utils.flag;
// check if we have all dependencies
assert.ok(tv4Module, 'tv4 dependency');
assert.ok(jsonpointer, 'jsonpointer dependency');
// export and use our own instance
chai.tv4 = tv4Module.freshApi();
chai.tv4.cyclicCheck = false;
chai.tv4.banUnknown = false;
chai.tv4.multiple = false;
function forEachI(arr, func, scope) {
for (var i = 0, ii = arr.length; i < ii; i++) {
func.call(scope, arr[i], i, arr);
}
}
// make a compact debug string from any object
function valueStrim(value, cutoff) {
var strimLimit = typeof cutoff === 'undefined' ? 60 : cutoff;
var t = typeof value;
if (t === 'function') {
return '[function]';
}
if (t === 'object') {
value = JSON.stringify(value);
if (value.length > strimLimit) {
value = value.substr(0, strimLimit) + '...';
}
return value;
}
if (t === 'string') {
if (value.length > strimLimit) {
return JSON.stringify(value.substr(0, strimLimit)) + '...';
}
return JSON.stringify(value);
}
return '' + value;
}
function extractSchemaLabel(schema, max) {
max = typeof max === 'undefined' ? 40 : max;
var label = '';
if (schema.id) {
label = schema.id;
}
if (schema.title) {
label += (label ? ' (' + schema.title + ')' : schema.title);
}
if (!label && schema.description) {
label = valueStrim(schema.description, max);
}
if (!label) {
label = valueStrim(schema, max);
}
return label;
}
// print validation errors
var formatResult = function (error, data, schema, indent) {
var schemaValue;
var dataValue;
var schemaLabel;
// assemble error string
var ret = '';
ret += '\n' + indent + error.message;
schemaLabel = extractSchemaLabel(schema, 60);
if (schemaLabel) {
ret += '\n' + indent + ' schema: ' + schemaLabel;
}
if (error.schemaPath) {
schemaValue = jsonpointer.get(schema, error.schemaPath);
ret += '\n' + indent + ' rule: ' + error.schemaPath + ' -> ' + valueStrim(schemaValue);
}
if (error.dataPath) {
dataValue = jsonpointer.get(data, error.dataPath);
ret += '\n' + indent + ' field: ' + error.dataPath + ' -> ' + utils.type(dataValue) + ': ' + valueStrim(dataValue);
}
// sub errors are not implemented (yet?)
// https://github.com/chaijs/chai-json-schema/issues/3
/*if (error.subErrors) {
forEachI(error.subErrors, function (error) {
ret += formatResult(error, data, schema, indent + indent);
});
}*/
return ret;
};
// add the method
chai.Assertion.addMethod('jsonSchema', function (schema, msg) {
if (msg) {
flag(this, 'message', msg);
}
var obj = this._obj;
// note: don't assert.ok(obj) -> zero or empty string is a valid and describable json-value
assert.ok(schema, 'schema');
// single result
var result = null;
if (chai.tv4.multiple) {
result = chai.tv4.validateMultiple(obj, schema, chai.tv4.cyclicCheck, chai.tv4.banUnknown);
} else {
result = chai.tv4.validateResult(obj, schema, chai.tv4.cyclicCheck, chai.tv4.banUnknown);
}
// assertion fails on missing schemas
var pass = result.valid && (result.missing.length === 0);
// assemble readable message
var label = extractSchemaLabel(schema, 30);
// assemble error report
var details = '';
if (!pass) {
var indent = ' ';
details += ' -> \'' + valueStrim(obj, 30) + '\'';
if (result.error) {
details += formatResult(result.error, obj, schema, indent);
}
else if (result.errors) {
forEachI(result.errors, function (error) {
details += formatResult(error, obj, schema, indent);
});
}
if (result.missing.length === 1) {
details += '\n' + 'missing 1 schema: ' + result.missing[0];
}
else if (result.missing.length > 0) {
details += '\n' + 'missing ' + result.missing.length + ' schemas:';
forEachI(result.missing, function (missing) {
details += '\n' + missing;
});
}
}
// pass hardcoded strings and no actual value (mocha forces nasty string diffs)
this.assert(
pass
, 'expected value to match json-schema \'' + label + '\'' + details
, 'expected value not to match json-schema \'' + label + '\'' + details
, label
);
});
// export tdd style
assert.jsonSchema = function (val, exp, msg) {
new chai.Assertion(val, msg).to.be.jsonSchema(exp);
};
assert.notJsonSchema = function (val, exp, msg) {
new chai.Assertion(val, msg).to.not.be.jsonSchema(exp);
};
};
}
}());