-
-
Notifications
You must be signed in to change notification settings - Fork 174
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9aa92a7
commit a161318
Showing
11 changed files
with
218 additions
and
148 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import Base from 'ember-cp-validations/validators/base'; | ||
import { assign } from '@ember/polyfills'; | ||
import { assert } from '@ember/debug'; | ||
|
||
/** | ||
* Accepts a custom `validate` function. | ||
* | ||
* ## Examples | ||
* | ||
* ```javascript | ||
* validator('inline', { | ||
* username: 'offirgolan', | ||
* validate(value, options, model, attribute) { | ||
* return value === options.username ? | ||
* true : | ||
* `Username must be ${options.username}`; | ||
* } | ||
* }); | ||
* ``` | ||
* | ||
* @class Inline | ||
* @module Validators | ||
* @extends Base | ||
*/ | ||
export default Base.extend({ | ||
/** | ||
* Override the validator's `validate` method with the one that was | ||
* passed in via the options. | ||
* | ||
* @method buildOptions | ||
* @param {Object} options | ||
* @param {Object} defaultOptions | ||
* @param {Object} globalOptions | ||
* @return {Object} | ||
*/ | ||
buildOptions(options = {}, ...args) { | ||
assert( | ||
`[validator:inline] You must pass in a validate function`, | ||
options && typeof options.validate === 'function' | ||
); | ||
|
||
const opts = assign({}, options); | ||
|
||
this.validate = opts.validate; | ||
delete opts.validate; | ||
|
||
return this._super(opts, ...args); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from 'ember-cp-validations/validators/inline'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -91,19 +91,17 @@ test('nested ds-error validator creates correct dependent keys', function(assert | |
|
||
test('custom dependent keys - simple', function(assert) { | ||
let Validations = buildValidations({ | ||
fullName: validator( | ||
function(value, options, model) { | ||
fullName: validator('inline', { | ||
dependentKeys: ['model.firstName', 'model.lastName'], | ||
validate(value, options, model) { | ||
let firstName = model.get('firstName'); | ||
let lastName = model.get('lastName'); | ||
if (!firstName || !lastName) { | ||
return 'Full name requires first and last name'; | ||
} | ||
return true; | ||
}, | ||
{ | ||
dependentKeys: ['model.firstName', 'model.lastName'] | ||
} | ||
) | ||
}) | ||
}); | ||
|
||
let obj = setupObject(this, EmberObject.extend(Validations)); | ||
|
@@ -127,19 +125,17 @@ test('custom dependent keys - default options', function(assert) { | |
fullName: { | ||
dependentKeys: ['model.firstName'], | ||
validators: [ | ||
validator( | ||
function(value, options, model) { | ||
validator('inline', { | ||
dependentKeys: ['model.lastName'], | ||
validate(value, options, model) { | ||
let firstName = model.get('firstName'); | ||
let lastName = model.get('lastName'); | ||
if (!firstName || !lastName) { | ||
return 'Full name requires first and last name'; | ||
} | ||
return true; | ||
}, | ||
{ | ||
dependentKeys: ['model.lastName'] | ||
} | ||
) | ||
}) | ||
] | ||
} | ||
}); | ||
|
@@ -166,20 +162,18 @@ test('custom dependent keys - global options', function(assert) { | |
fullName: { | ||
dependentKeys: ['model.firstName'], | ||
validators: [ | ||
validator( | ||
function(value, options, model) { | ||
validator('inline', { | ||
dependentKeys: ['model.lastName'], | ||
validate(value, options, model) { | ||
let firstName = model.get('firstName'); | ||
let lastName = model.get('lastName'); | ||
let middleName = model.get('middleName'); | ||
if (!firstName || !lastName || !middleName) { | ||
return 'Full name requires first, middle, and last name'; | ||
} | ||
return true; | ||
}, | ||
{ | ||
dependentKeys: ['model.lastName'] | ||
} | ||
) | ||
}) | ||
] | ||
} | ||
}, | ||
|
@@ -211,19 +205,17 @@ test('custom dependent keys - global options', function(assert) { | |
|
||
test('custom dependent keys - nested object', function(assert) { | ||
let Validations = buildValidations({ | ||
page: validator( | ||
function(value, options, model) { | ||
page: validator('inline', { | ||
dependentKeys: ['model.currPage', 'model.meta.pages.last'], | ||
validate(value, options, model) { | ||
let currPage = model.get('currPage'); | ||
let lastPage = model.get('meta.pages.last'); | ||
if (currPage > lastPage) { | ||
return 'Cannot exceed max page'; | ||
} | ||
return true; | ||
}, | ||
{ | ||
dependentKeys: ['model.currPage', 'model.meta.pages.last'] | ||
} | ||
) | ||
}) | ||
}); | ||
|
||
let obj = setupObject(this, EmberObject.extend(Validations), { | ||
|
@@ -255,18 +247,16 @@ test('custom dependent keys - nested object', function(assert) { | |
|
||
test('custom dependent keys - array', function(assert) { | ||
let Validations = buildValidations({ | ||
friends: validator( | ||
function(value, options, model) { | ||
friends: validator('inline', { | ||
dependentKeys: ['model.friends.[]'], | ||
validate(value, options, model) { | ||
let friends = model.get('friends'); | ||
if (!friends || friends.length === 0) { | ||
return 'User must have a friend'; | ||
} | ||
return true; | ||
}, | ||
{ | ||
dependentKeys: ['model.friends.[]'] | ||
} | ||
) | ||
}) | ||
}); | ||
|
||
let obj = setupObject(this, EmberObject.extend(Validations), { | ||
|
@@ -297,8 +287,9 @@ test('custom dependent keys - array', function(assert) { | |
|
||
test('custom dependent keys - array of objects', function(assert) { | ||
let Validations = buildValidations({ | ||
friends: validator( | ||
function(value, options, model) { | ||
friends: validator('inline', { | ||
dependentKeys: ['[email protected]'], | ||
validate(value, options, model) { | ||
let friends = model.get('friends'); | ||
if (!friends || friends.length === 0) { | ||
return 'User must have a friend'; | ||
|
@@ -309,11 +300,8 @@ test('custom dependent keys - array of objects', function(assert) { | |
} | ||
} | ||
return true; | ||
}, | ||
{ | ||
dependentKeys: ['[email protected]'] | ||
} | ||
) | ||
}) | ||
}); | ||
|
||
let obj = setupObject(this, EmberObject.extend(Validations), { | ||
|
Oops, something went wrong.