Skip to content

Commit b4260f6

Browse files
authored
Merge pull request #38 from Niklv/master
Add multiple option
2 parents 80473ea + 67d69df commit b4260f6

File tree

3 files changed

+73
-7
lines changed

3 files changed

+73
-7
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,15 @@ chai.tv4.banUnknown = true;
156156

157157
Passed to the internal `tv4` validate call makes validation fail on unknown schema properties. Use this to make sure your schema do not contain undesirable data.
158158

159+
**Validate multiple errors**
160+
161+
````js
162+
chai.tv4.multiple = true;
163+
````
164+
165+
Call `tv4.validateMultiple` for validation instead of `tv4.validateResult`. Use this if you want see all validation errors.
166+
167+
159168
### Remote references
160169

161170
Due to the synchronous nature of assertions there will be no support for dynamically loading remote references during validation.

index.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,10 @@
3838
chai.tv4 = tv4Module.freshApi();
3939
chai.tv4.cyclicCheck = false;
4040
chai.tv4.banUnknown = false;
41+
chai.tv4.multiple = false;
4142

4243
function forEachI(arr, func, scope) {
43-
for (var i = arr.length, ii = arr.length; i < ii; i++) {
44+
for (var i = 0, ii = arr.length; i < ii; i++) {
4445
func.call(scope, arr[i], i, arr);
4546
}
4647
}
@@ -131,7 +132,12 @@
131132
assert.ok(schema, 'schema');
132133

133134
// single result
134-
var result = chai.tv4.validateResult(obj, schema, chai.tv4.cyclicCheck, chai.tv4.banUnknown);
135+
var result = null;
136+
if (chai.tv4.multiple) {
137+
result = chai.tv4.validateMultiple(obj, schema, chai.tv4.cyclicCheck, chai.tv4.banUnknown);
138+
} else {
139+
result = chai.tv4.validateResult(obj, schema, chai.tv4.cyclicCheck, chai.tv4.banUnknown);
140+
}
135141
// assertion fails on missing schemas
136142
var pass = result.valid && (result.missing.length === 0);
137143

test/suite.js

Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@
7979
}],
8080
invalid: [{
8181
data: {
82-
intKey: 3,
82+
intKey: 'three',
8383
stringKey: false
8484
}
8585
}]
@@ -130,24 +130,25 @@
130130
data: {
131131
skin: 'thin',
132132
colors: ['yellow'],
133-
taste: 0
133+
taste: 0,
134+
worms: 2
134135
}
135136
}, {
136137
data: {
137138
skin: 'thin',
138139
colors: [1, 2, 3],
139-
taste: 5
140+
taste: 4
140141
}
141142
}, {
142143
data: {
143144
skin: 321,
144-
colors: ['yellow'],
145+
colors: ['yellow', 'yellow'],
145146
taste: 5
146147
}
147148
}, { data: {
148149
skin: 'thin',
149150
colors: ['yellow'],
150-
taste: 5,
151+
taste: 4,
151152
worms: 3
152153
}
153154
}]
@@ -214,6 +215,34 @@
214215
}).to.fail('#' + i);
215216
});
216217
});
218+
it('should/expect output single negation', function () {
219+
testCase.invalid.forEach(function (obj, i) {
220+
expect(function () {
221+
expect(obj.data).to.be.jsonSchema(testCase.schema, 'expect() #' + i);
222+
}).to.throw(/(.+?\n){4}/);
223+
expect(function () {
224+
obj.data.should.be.jsonSchema(testCase.schema, 'should #' + i);
225+
}).to.throw(/(.+?\n){4}/);
226+
});
227+
});
228+
describe('should/expect output multiple negation', function () {
229+
before(function () {
230+
chai.tv4.multiple = true;
231+
});
232+
after(function () {
233+
chai.tv4.multiple = false;
234+
});
235+
it('should/expect multiple negation', function () {
236+
testCase.invalid.forEach(function (obj, i) {
237+
expect(function () {
238+
expect(obj.data).to.be.jsonSchema(testCase.schema, 'expect() #' + i);
239+
}).to.throw(/(.+?\n){5,}/);
240+
expect(function () {
241+
obj.data.should.be.jsonSchema(testCase.schema, 'should #' + i);
242+
}).to.throw(/(.+?\n){5,}/);
243+
});
244+
});
245+
});
217246
});
218247
});
219248
});
@@ -252,6 +281,28 @@
252281
}).to.fail('#' + i);
253282
});
254283
});
284+
it('should/expect output single negation', function () {
285+
testCase.invalid.forEach(function (obj, i) {
286+
expect(function () {
287+
assert.jsonSchema(obj.data, testCase.schema, '#' + i);
288+
}).to.throw(/(.+?\n){4}/);
289+
});
290+
});
291+
describe('should/expect output multiple negation', function () {
292+
before(function () {
293+
chai.tv4.multiple = true;
294+
});
295+
after(function () {
296+
chai.tv4.multiple = false;
297+
});
298+
it('should/expect multiple negation', function () {
299+
testCase.invalid.forEach(function (obj, i) {
300+
expect(function () {
301+
assert.jsonSchema(obj.data, testCase.schema, '#' + i);
302+
}).to.throw(/(.+?\n){5,}/);
303+
});
304+
});
305+
});
255306
});
256307
});
257308
});

0 commit comments

Comments
 (0)