Skip to content

Commit eb266e7

Browse files
sovietspaceshipDavid Tang
authored andcommitted
Support single validator in addition to arrays of validators (#14)
* support non-Array as argument to validateSometimes for consistency with plain ember-changeset-validations * Update sometimes.js * add unit tests * Update README.md
1 parent 2f1a44f commit eb266e7

File tree

3 files changed

+39
-5
lines changed

3 files changed

+39
-5
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export default {
2828
};
2929
```
3030

31-
`validateSometimes()` takes 2 arguments. The first is a list of validators you want applied to the attribute. 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. The callback will also be invoked with its `this` value set to an object that has a `get()` method for accessing a property. `this.get(property)` first proxies to the changes and then the underlying content, and has the same semantics as `Ember.get()`.
31+
`validateSometimes()` takes 2 arguments. The first is a validator or list of validators you want applied to the attribute. 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. The callback will also be invoked with its `this` value set to an object that has a `get()` method for accessing a property. `this.get(property)` first proxies to the changes and then the underlying content, and has the same semantics as `Ember.get()`.
3232

3333
```js
3434
import Changeset from 'ember-changeset';

addon/validators/sometimes.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import { get } from '@ember/object';
22

3-
export default function validateSometimes(validators, condition) {
4-
return validators.map(function(validator) {
3+
export default function validateSometimes(validator, condition) {
4+
return Array.isArray(validator) ? validator.map(mapValidator) : mapValidator(validator)
5+
6+
function mapValidator(validator) {
57
return function(key, newValue, oldValue, changes, content) {
68
let thisValue = {
79
get(property) {
@@ -16,5 +18,5 @@ export default function validateSometimes(validators, condition) {
1618
}
1719
return true;
1820
};
19-
});
21+
}
2022
}

tests/unit/validators/sometimes-test.js

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import isFunction from './../../helpers/is-function';
77
import Changeset from 'ember-changeset';
88

99
module('Unit | Validator | sometimes', function() {
10-
test('an array of validators is returned', function(assert) {
10+
test('an array of validators is returned when given an array', function(assert) {
1111
let validatorA = function() {};
1212
let validatorB = function() {};
1313
let condition = function() {};
@@ -18,6 +18,15 @@ module('Unit | Validator | sometimes', function() {
1818
assert.ok(validators.every(isFunction));
1919
});
2020

21+
test('an validator function is returned if given a validator', function(assert) {
22+
let validatorA = function() {};
23+
let condition = function() {};
24+
25+
let validator = validateSometimes(validatorA, condition);
26+
27+
assert.ok(isFunction(validator));
28+
});
29+
2130
test('if the condition returns false, the validators return true', function(assert) {
2231
let validatorA = function() {};
2332
let validatorB = function() {};
@@ -91,6 +100,29 @@ module('Unit | Validator | sometimes', function() {
91100
assert.strictEqual(validatorB.firstCall.args[4], content);
92101
});
93102

103+
test('single validator is invoked with key, newValue, oldValue, changes, and content', function(assert) {
104+
let key = 'name';
105+
let newValue = 'Yehuda';
106+
let oldValue = 'YK';
107+
let changes = {};
108+
let content = {};
109+
110+
let validatorA = sinon.spy();
111+
let condition = () => true;
112+
113+
let validator = validateSometimes(validatorA, condition);
114+
115+
assert.ok(isFunction(validator));
116+
117+
validator(key, newValue, oldValue, changes, content);
118+
119+
assert.strictEqual(validatorA.firstCall.args[0], key);
120+
assert.strictEqual(validatorA.firstCall.args[1], newValue);
121+
assert.strictEqual(validatorA.firstCall.args[2], oldValue);
122+
assert.strictEqual(validatorA.firstCall.args[3], changes);
123+
assert.strictEqual(validatorA.firstCall.args[4], content);
124+
});
125+
94126
test('this.get() when accessing the changes', function(assert) {
95127
let Validations = {
96128
paymentMethod: validatePresence(true),

0 commit comments

Comments
 (0)