Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add optional onError callback #364

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@ The validator can be configured to throw in the event of a validation error:

* If the `throwError` option is set, it will throw at the first encountered validation error (like `throwFirst`), but the `ValidationError` object itself will be thrown. Note that, despite the name, this does not inherit from Error like `ValidatorResultError` does.

* If the `onError` option is set, the validator will call the provided callback with the `ValidationError` object.

The `ValidatorResultError` object has the same properties as `ValidatorResult` and additionally inherits from Error.

#### "nestedErrors" option
Expand Down
4 changes: 4 additions & 0 deletions lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ var ValidatorResult = exports.ValidatorResult = function ValidatorResult(instanc
this.throwError = options && options.throwError;
this.throwFirst = options && options.throwFirst;
this.throwAll = options && options.throwAll;
this.onError = options && options.onError;
this.disableFormat = options && options.disableFormat === true;
};

Expand All @@ -60,6 +61,9 @@ ValidatorResult.prototype.addError = function addError(detail) {
}else if(this.throwError){
throw err;
}
if(this.onError) {
this.onError(err)
}
return err;
};

Expand Down
1 change: 1 addition & 0 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ export interface Options {
required?: boolean;
throwFirst?: boolean;
throwAll?: boolean;
onError?: (err: ValidationError) => void;
nestedErrors?: boolean;
}

Expand Down
13 changes: 13 additions & 0 deletions test/Validator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,19 @@ describe('Validator', function () {
return true;
});
});
it('options.onError', function () {
var schema = {
type: 'array',
items: {type: 'number'},
}
var errors = []
validator.validate([0,'1','2',3,4], schema, {
onError: function (err) {
errors.push(err)
}
});
assert.strictEqual(errors.length, 2);
});
it('subschema references (named reference)', function () {
var schema = {
items: {$ref: '#items'},
Expand Down