|
30 | 30 | var assert = chai.assert;
|
31 | 31 | var flag = utils.flag;
|
32 | 32 |
|
33 |
| - //check if we have all dependencies |
| 33 | + // check if we have all dependencies |
34 | 34 | assert.ok(tv4Module, 'tv4 dependency');
|
35 | 35 | assert.ok(jsonpointer, 'jsonpointer dependency');
|
36 | 36 |
|
37 |
| - //export and use our own instance |
| 37 | + // export and use our own instance |
38 | 38 | chai.tv4 = tv4Module.freshApi();
|
39 | 39 | chai.tv4.cyclicCheck = false;
|
40 | 40 | chai.tv4.banUnknown = false;
|
|
45 | 45 | }
|
46 | 46 | }
|
47 | 47 |
|
48 |
| - //make a compact debug string from any object |
| 48 | + // make a compact debug string from any object |
49 | 49 | function valueStrim(value, cutoff) {
|
50 | 50 | var strimLimit = typeof cutoff === 'undefined' ? 60 : cutoff;
|
51 | 51 |
|
|
87 | 87 | return label;
|
88 | 88 | }
|
89 | 89 |
|
90 |
| - //print validation errors |
| 90 | + // print validation errors |
91 | 91 | var formatResult = function (error, data, schema, indent) {
|
92 | 92 | var schemaValue;
|
93 | 93 | var dataValue;
|
94 | 94 | var schemaLabel;
|
95 | 95 |
|
96 |
| - //assemble error string |
| 96 | + // assemble error string |
97 | 97 | var ret = '';
|
98 | 98 | ret += '\n' + indent + error.message;
|
99 | 99 |
|
|
110 | 110 | ret += '\n' + indent + ' field: ' + error.dataPath + ' -> ' + utils.type(dataValue) + ': ' + valueStrim(dataValue);
|
111 | 111 | }
|
112 | 112 |
|
113 |
| - //go deeper |
| 113 | + // sub errors are not implemented (yet?) |
| 114 | + // https://github.com/chaijs/chai-json-schema/issues/3 |
114 | 115 | /*if (error.subErrors) {
|
115 | 116 | forEachI(error.subErrors, function (error) {
|
116 | 117 | ret += formatResult(error, data, schema, indent + indent);
|
|
119 | 120 | return ret;
|
120 | 121 | };
|
121 | 122 |
|
122 |
| - //add the method |
| 123 | + // add the method |
123 | 124 | chai.Assertion.addMethod('jsonSchema', function (schema, msg) {
|
124 | 125 | if (msg) {
|
125 | 126 | flag(this, 'message', msg);
|
126 | 127 | }
|
127 | 128 | var obj = this._obj;
|
128 | 129 |
|
129 |
| - //note: don't assert.ok(obj) -> zero or empty string is a valid and describable json-value |
| 130 | + // note: don't assert.ok(obj) -> zero or empty string is a valid and describable json-value |
130 | 131 | assert.ok(schema, 'schema');
|
131 | 132 |
|
132 |
| - //single result |
| 133 | + // single result |
133 | 134 | var result = chai.tv4.validateResult(obj, schema, chai.tv4.cyclicCheck, chai.tv4.banUnknown);
|
134 |
| - //assertion fails on missing schemas |
| 135 | + // assertion fails on missing schemas |
135 | 136 | var pass = result.valid && (result.missing.length === 0);
|
136 | 137 |
|
137 |
| - //assemble readable message |
| 138 | + // assemble readable message |
138 | 139 | var label = extractSchemaLabel(schema, 30);
|
139 | 140 |
|
140 |
| - //assemble error report |
| 141 | + // assemble error report |
141 | 142 | var details = '';
|
142 | 143 | if (!pass) {
|
143 | 144 | var indent = ' ';
|
|
162 | 163 | });
|
163 | 164 | }
|
164 | 165 | }
|
165 |
| - //pass hardcoded strings and no actual value (mocha forces nasty string diffs) |
| 166 | + // pass hardcoded strings and no actual value (mocha forces nasty string diffs) |
166 | 167 | this.assert(
|
167 | 168 | pass
|
168 | 169 | , 'expected value to match json-schema \'' + label + '\'' + details
|
|
171 | 172 | );
|
172 | 173 | });
|
173 | 174 |
|
174 |
| - //export tdd style |
| 175 | + // export tdd style |
175 | 176 | assert.jsonSchema = function (val, exp, msg) {
|
176 | 177 | new chai.Assertion(val, msg).to.be.jsonSchema(exp);
|
177 | 178 | };
|
|
0 commit comments