Skip to content

Commit

Permalink
Merge pull request #363 from osi-jehrlich/fix-stack-size-exceeded
Browse files Browse the repository at this point in the history
Fix "maximum call stack size exceeded" when there are many errors
  • Loading branch information
awwright committed May 10, 2022
2 parents 6f65129 + 1fc38f5 commit 8117c85
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ ValidatorResult.prototype.importErrors = function importErrors(res) {
if (typeof res == 'string' || (res && res.validatorType)) {
this.addError(res);
} else if (res && res.errors) {
Array.prototype.push.apply(this.errors, res.errors);
this.errors = this.errors.concat(res.errors)
}
};

Expand Down
17 changes: 17 additions & 0 deletions test/Validator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
var Validator = require('../lib/index.js').Validator;
var SchemaError = require('../lib/index.js').SchemaError;
var ValidationError = require('../lib/index.js').ValidationError;
var ValidatorResult = require('../lib/index.js').ValidatorResult;
var ValidatorResultError = require('../lib/index.js').ValidatorResultError;
var assert = require('assert');

Expand Down Expand Up @@ -282,6 +283,22 @@ describe('Validator', function () {
return true;
});
});
it('million errors', function () {
var schema = {
type: 'number',
oneMillionErrors: true,
};
validator.attributes.oneMillionErrors = function(instance, schema, options, ctx) {
const result = new ValidatorResult(instance, schema, options, ctx);
for(var i = 0; i < 1000000; i++) {
result.addError('oneMillionErrors error');
}
return result;
}
var res = validator.validate(1, schema, {});
assert(!res.valid);
assert.strictEqual(res.errors.length, 1000000);
});
it('subschema references (named reference)', function () {
var schema = {
items: {$ref: '#items'},
Expand Down

0 comments on commit 8117c85

Please sign in to comment.