Skip to content
Merged
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
4 changes: 3 additions & 1 deletion addon/validators/sometimes.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ export default function validateSometimes(validators, condition) {
return function(key, newValue, oldValue, changes, content) {
let thisValue = {
get(property) {
return get(changes, property) || get(content, property);
return get(changes, property) != null
Copy link
Owner

@iamdtang iamdtang Jul 8, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will work for false, but not undefined or null. How about this instead?

let thisValue = {
  get(property) {
    if (property.includes('.')) {
      return get(changes, property) || get(content, property);
    }

    if (changes.hasOwnProperty(property)) {
      return get(changes, property);
    } else {
      return get(content, property);
    }
  }
};

If the property exists in changes, then return that. This way, it should work with undefined and null too. Otherwise, return the property from content.

I'm not sure how best to handle property paths (i.e. this.get('a.b')), so that might need some more thinking, hence the check for a dot in the first conditional.

Copy link

@adambedford adambedford Mar 13, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has just become an issue for us. Setting a property to null doesn't behave as you'd expect because it gets proxied to the model, which still has the value.

? get(changes, property)
: get(content, property);
}
};

Expand Down
27 changes: 27 additions & 0 deletions tests/unit/validators/sometimes-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,30 @@ test('this.get() has the same semantics as Ember.get when accessing changes', fu
changeset.validate();
assert.notOk(changeset.get('isValid'));
});

test('this.get() works with boolean values', function(assert) {
const Validations = {
hasCreditCard: validatePresence(true),
creditCardNumber: validateSometimes([
validatePresence(true),
validateLength({ is: 16 })
], function () {
return this.get('hasCreditCard');
})
};

let settings = {
hasCreditCard: true
};
let changeset = new Changeset(settings, lookupValidator(Validations), Validations);
changeset.set('creditCardNumber', '1234567890123456');
changeset.validate();
assert.ok(changeset.get('isValid'), 'valid');
changeset.set('hasCreditCard', false);
changeset.set('creditCardNumber', '12');
changeset.validate();
assert.ok(changeset.get('isValid'), 'valid');
changeset.set('hasCreditCard', true);
changeset.validate();
assert.notOk(changeset.get('isValid'), 'invalid');
});