Skip to content

Commit 02ec96e

Browse files
author
David Tang
committed
this.get should use Ember.get
1 parent 0c752e6 commit 02ec96e

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

addon/validators/sometimes.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
import { get } from '@ember/object';
2+
13
export default function validateSometimes(validators, condition) {
24
return validators.map(function(validator) {
35
return function(key, newValue, oldValue, changes, content) {
46
let thisValue = {
57
get(property) {
6-
return changes[property] || content[property];
8+
return get(changes, property) || get(content, property);
79
}
810
};
911

tests/unit/validators/sometimes-test.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,46 @@ test('this.get() to access the content', function(assert) {
137137
changeset.save();
138138
assert.ok(changeset.get('isValid'), 'valid');
139139
});
140+
141+
test('this.get() has the same semantics as Ember.get when accessing content', function(assert) {
142+
const Validations = {
143+
paymentMethod: validatePresence(true),
144+
creditCardNumber: validateSometimes([
145+
validatePresence(true),
146+
validateLength({ is: 16 })
147+
], function() {
148+
return this.get('paymentMethod.isCreditCard');
149+
})
150+
};
151+
152+
let settings = {
153+
paymentMethod: {
154+
isCreditCard: true
155+
},
156+
creditCardNumber: 12
157+
};
158+
let changeset = new Changeset(settings, lookupValidator(Validations), Validations);
159+
changeset.validate();
160+
assert.notOk(changeset.get('isValid'));
161+
});
162+
163+
test('this.get() has the same semantics as Ember.get when accessing changes', function(assert) {
164+
const Validations = {
165+
paymentMethod: validatePresence(true),
166+
creditCardNumber: validateSometimes([
167+
validatePresence(true),
168+
validateLength({ is: 16 })
169+
], function() {
170+
return this.get('paymentMethod.isCreditCard');
171+
})
172+
};
173+
174+
let settings = {};
175+
let changeset = new Changeset(settings, lookupValidator(Validations), Validations);
176+
changeset.set('paymentMethod', {
177+
isCreditCard: true
178+
});
179+
changeset.set('creditCardNumber', '12');
180+
changeset.validate();
181+
assert.notOk(changeset.get('isValid'));
182+
});

0 commit comments

Comments
 (0)