Skip to content

Commit c4b0942

Browse files
author
David Tang
committed
the condition callback is invoked with the changes and content
1 parent d7aed25 commit c4b0942

File tree

4 files changed

+9
-50
lines changed

4 files changed

+9
-50
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ const Validations = {
2525
creditCardNumber: validateSometimes([
2626
validatePresence(true),
2727
validateNumber({ is: 16 })
28-
], function(changes) {
28+
], function(changes, content) {
2929
return get(changes, 'paymentMethod.isCreditCard')
3030
})
3131
};
3232
```
3333

34-
`validateSometimes` takes 2 arguments. The first is a list of validators and the second is a callback function which represents the condition. If the condition callback returns `true`, the rules will be added.
34+
`validateSometimes` takes 2 arguments. The first is a list of validators. The second argument is a callback function which represents the condition. If the condition callback returns `true`, the rules will be added. This callback function will be invoked with the changeset's changes and content.
3535

3636
```js
3737
import Changeset from 'ember-changeset';

addon/validators/sometimes.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
export default function validateSometimes(validators, condition) {
22
return validators.map(function(validator) {
33
return function(key, newValue, oldValue, changes, content) {
4-
if (condition(changes)) {
4+
if (condition(changes, content)) {
55
return validator(key, newValue, oldValue, changes, content);
66
}
77
return true;

tests/dummy/app/routes/application.js

Lines changed: 0 additions & 44 deletions
This file was deleted.

tests/unit/validators/sometimes-test.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,23 +36,26 @@ test('if the condition returns true, the validators are invoked and their result
3636
assert.deepEqual(validations, ['Error message A', true]);
3737
});
3838

39-
test('the condition is invoked with the changes for each validator', function(assert) {
39+
test('the condition is invoked with the changes and content for each validator', function(assert) {
4040
let key = 'name';
4141
let newValue = 'Yehuda';
4242
let oldValue = 'YK';
4343
let changes = {};
44+
let content = {};
4445

4546
let validatorA = sinon.stub().returns('Error message A');
4647
let validatorB = sinon.stub().returns(true);
4748
let condition = sinon.spy();
4849

4950
let validators = validateSometimes([validatorA, validatorB], condition);
5051
validators.map((validator) => {
51-
return validator(key, newValue, oldValue, changes);
52+
return validator(key, newValue, oldValue, changes, content);
5253
});
5354
assert.equal(condition.callCount, 2);
5455
assert.strictEqual(condition.getCall(0).args[0], changes);
56+
assert.strictEqual(condition.getCall(0).args[1], content);
5557
assert.strictEqual(condition.getCall(1).args[0], changes);
58+
assert.strictEqual(condition.getCall(1).args[1], content);
5659
});
5760

5861
test('each validator is invoked with key, newValue, oldValue, changes, and content', function(assert) {
@@ -64,7 +67,7 @@ test('each validator is invoked with key, newValue, oldValue, changes, and conte
6467

6568
let validatorA = sinon.spy();
6669
let validatorB = sinon.spy();
67-
let condition = sinon.stub().returns(true);
70+
let condition = sinon.stub().returns(true);
6871
let validators = validateSometimes([validatorA, validatorB], condition);
6972

7073
validators.map((validator) => {

0 commit comments

Comments
 (0)