Skip to content

Commit 1bf06ac

Browse files
author
David Tang
authored
Merge pull request #11 from Henridv/master
Support boolean values in this.get()
2 parents 1f377f4 + 1534586 commit 1bf06ac

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

addon/validators/sometimes.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ export default function validateSometimes(validators, condition) {
55
return function(key, newValue, oldValue, changes, content) {
66
let thisValue = {
77
get(property) {
8-
return get(changes, property) || get(content, property);
8+
return get(changes, property) != null
9+
? get(changes, property)
10+
: get(content, property);
911
}
1012
};
1113

tests/unit/validators/sometimes-test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,3 +180,30 @@ test('this.get() has the same semantics as Ember.get when accessing changes', fu
180180
changeset.validate();
181181
assert.notOk(changeset.get('isValid'));
182182
});
183+
184+
test('this.get() works with boolean values', function(assert) {
185+
const Validations = {
186+
hasCreditCard: validatePresence(true),
187+
creditCardNumber: validateSometimes([
188+
validatePresence(true),
189+
validateLength({ is: 16 })
190+
], function () {
191+
return this.get('hasCreditCard');
192+
})
193+
};
194+
195+
let settings = {
196+
hasCreditCard: true
197+
};
198+
let changeset = new Changeset(settings, lookupValidator(Validations), Validations);
199+
changeset.set('creditCardNumber', '1234567890123456');
200+
changeset.validate();
201+
assert.ok(changeset.get('isValid'), 'valid');
202+
changeset.set('hasCreditCard', false);
203+
changeset.set('creditCardNumber', '12');
204+
changeset.validate();
205+
assert.ok(changeset.get('isValid'), 'valid');
206+
changeset.set('hasCreditCard', true);
207+
changeset.validate();
208+
assert.notOk(changeset.get('isValid'), 'invalid');
209+
});

0 commit comments

Comments
 (0)