Skip to content

Commit

Permalink
Do not reject promises on validation fails
Browse files Browse the repository at this point in the history
  • Loading branch information
jhnns committed May 11, 2016
1 parent bd4bf32 commit dec6aa1
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,9 @@ PandaSchema.validate(panda)
})
```

The promise will still be resolved even when the validation fails, because a failed validation is
not an error, it's an expected state.

The promise provides a reference to the final validation result object. It contains the intermediate
result of all synchronous validators:

Expand Down
4 changes: 2 additions & 2 deletions plugins/validation/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,12 @@ function validationPlugin(Schema) {
throw new Error("Validators not defined: Have you registered the validate plugin before any schema definitions?");
}

promise = new Promise(function (resolve, reject) {
promise = new Promise(function (resolve) {
function done() {
if (typeof callback === "function") {
setTimeout(doCallback, 0);
}
result.result ? resolve(result) : reject(result);
resolve(result);
}

if (self.keys.length === 0) {
Expand Down
10 changes: 5 additions & 5 deletions test/validation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ describe("plugins/validation", function () {
});
});

it("should reject if validation fails", function () {
it("should not reject if validation fails", function () {
schema = new Schema({
age: {
type: Number,
Expand All @@ -312,11 +312,11 @@ describe("plugins/validation", function () {
});

return schema.validate({ age: 1 })
.then(function () {
throw new Error("Should not resolve");
})
.catch(function (validation) {
.then(function (validation) {
expect(validation).to.eql({ result: false, model: { age: 1 }, errors: { age: ["min"] } });
})
.catch(function () {
throw new Error("Should not reject");
});
});

Expand Down

0 comments on commit dec6aa1

Please sign in to comment.