Skip to content

Commit 02c56c2

Browse files
committed
Fix infuriating validation bug
1 parent 057ccb1 commit 02c56c2

2 files changed

Lines changed: 22 additions & 2 deletions

File tree

‎lib/Instance.js‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ function Instance(Model, opts) {
6262
});
6363

6464
for (var k in opts.validations) {
65+
required = false;
66+
6567
if (Model.properties[k]) {
6668
required = Model.properties[k].required;
6769
} else {

‎test/integration/validation.js‎

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,18 @@ describe("Validations", function() {
6464
var setupUnique = function (ignoreCase, scope, msg) {
6565
return function (done) {
6666
Product = db.define("product_unique", {
67-
name : String,
67+
instock : { type: 'boolean', required: true, defaultValue: false },
68+
name : String,
6869
category : String
6970
}, {
71+
cache: false,
7072
validations: {
71-
name: [ORM.validators.unique({ ignoreCase: ignoreCase, scope: scope }, msg)]
73+
name : ORM.validators.unique({ ignoreCase: ignoreCase, scope: scope }, msg),
74+
instock : ORM.validators.required(),
75+
productId : ORM.validators.unique() // this must be straight after a required & validated row.
7276
}
7377
});
78+
Product.hasOne('product', Product, { field: 'productId', required: false, autoFetch: true });
7479

7580
return helper.dropSync(Product, done);
7681
};
@@ -102,6 +107,19 @@ describe("Validations", function() {
102107
});
103108
});
104109
});
110+
111+
// Technically this is covered by the tests above, but I'm putting it here for clarity's sake. 3 HOURS WASTED *sigh.
112+
it("should not leak required state from previous validation for association properties [regression test]", function (done) {
113+
Product.create({ name: 'pencil', productId: null}, function (err, product) {
114+
should.not.exist(err);
115+
116+
Product.create({ name: 'pencilcase', productId: null }, function (err, product) {
117+
should.not.exist(err);
118+
119+
return done();
120+
});
121+
});
122+
});
105123
});
106124

107125
describe("scope", function () {

0 commit comments

Comments
 (0)