Skip to content

Commit eb51eb2

Browse files
author
David Tang
authored
Merge pull request #15 from skaterdav85/updates
Updates
2 parents eb266e7 + 7142d82 commit eb51eb2

File tree

4 files changed

+29
-6
lines changed

4 files changed

+29
-6
lines changed

README.md

Lines changed: 19 additions & 2 deletions
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 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()`.
31+
`validateSometimes()` takes 2 arguments. The first is a validator or an array 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';
@@ -65,11 +65,28 @@ You can also have a combination of validations that will always run and conditio
6565
import { validateNumber } from 'ember-changeset-validations/validators';
6666
import validateSometimes from 'ember-changeset-conditional-validations/validators/sometimes';
6767

68+
export default {
69+
someProperty: [
70+
validateNumber({ integer: true }),
71+
validateSometimes(validateNumber({ gt: 5 }), function() {
72+
// condition
73+
})
74+
]
75+
};
76+
```
77+
78+
Let's say in the previous example that you also wanted to conditionally validate that the number is less than 10. You could do something like the following:
79+
80+
```js
81+
import { validateNumber } from 'ember-changeset-validations/validators';
82+
import validateSometimes from 'ember-changeset-conditional-validations/validators/sometimes';
83+
6884
export default {
6985
someProperty: [
7086
validateNumber({ integer: true }),
7187
...validateSometimes([
72-
validateNumber({ gt: 5 })
88+
validateNumber({ gt: 5 }),
89+
validateNumber({ lt: 10 })
7390
], function() {
7491
// condition
7592
})

addon/validators/sometimes.js

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

33
export default function validateSometimes(validator, condition) {
4-
return Array.isArray(validator) ? validator.map(mapValidator) : mapValidator(validator)
4+
if (Array.isArray(arguments[0])) {
5+
let validators = arguments[0];
6+
return validators.map(guardValidatorWithCondition);
7+
} else {
8+
let validator = arguments[0];
9+
return guardValidatorWithCondition(validator);
10+
}
511

6-
function mapValidator(validator) {
12+
function guardValidatorWithCondition(validator) {
713
return function(key, newValue, oldValue, changes, content) {
814
let thisValue = {
915
get(property) {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ember-changeset-conditional-validations",
3-
"version": "0.4.2",
3+
"version": "0.5.0",
44
"description": "Conditional validations for ember-changeset-validations",
55
"keywords": [
66
"ember-addon",

tests/unit/validators/sometimes-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ 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) {
21+
test('a validator function is returned if given a validator', function(assert) {
2222
let validatorA = function() {};
2323
let condition = function() {};
2424

0 commit comments

Comments
 (0)