Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix broken validation and multiple custom #12

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
2 changes: 2 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
bower_components/
tests/
tmp/
dist/

.bowerrc
.editorconfig
Expand All @@ -9,5 +10,6 @@ tmp/
.npmignore
**/.gitkeep
bower.json
ember-cli-build.js
Brocfile.js
testem.json
3 changes: 3 additions & 0 deletions .watchmanconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"ignore_dirs": ["tmp"]
}
31 changes: 26 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -225,32 +225,53 @@ The value has to have only numeric values.
````

### Custom
Define a custom callback funciton to validate the model's value. The validation callback is passed 3 values: the _key_, _value_, _model's scope_. return true (or a truthy value) to pass the validation, return false (or falsy value) to fail the validation.
Define a custom callback function to validate the model's value. The validation callback is passed 3 values: the _key_, _value_, _model's scope_. return true (or a truthy value) to pass the validation, return false (or falsy value) to fail the validation.

````js
validations: {
lotteryNumber: {
custom: funciton(key, value, model){
custom: function(key, value, model){
return model.get('accountBalance') > 1 ? true : false;
}
}
}
````

this has the same action as above except will use a custom message insetad of the default.
this has the same action as above except will use a custom message instead of the default.
````js
validations: {
lotteryNumber: {
custom: {
validation: funciton(key, value, model){
validation: function(key, value, model){
return model.get('accountBalance') > 1 ? true : false;
},
message: 'You can't win off of good looks and charm.'
message: 'You can\'t win off of good looks and charm.'
}
}
}
````

to have multiple custom validation functions on the same property, give 'custom' an array of either of the two syntax above.
````js
validations: {
lotteryNumber: {
custom: [
{
validation: function(key, value, model){
return model.get('accountBalance') > 1 ? true : false;
},
message: 'You can\'t win off of good looks and charm.'
},
{
validation: function(key, value, model){
return model.get('accountBalance') > 1 ? true : false;
},
message: 'You can\'t win off of good looks and charm.'
}
]
}
}
````

### Password
A set of validators which are especially useful for validating passwords. Be aware that these all of these password-aimed validations will work standalone and carry the same [common options](#common-options) with the rest of the validations. They don't only work for passwords!
Expand Down
30 changes: 17 additions & 13 deletions addon/mixins/model-validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ export default Ember.Mixin.create({
if(Object.keys(errors).length !== 0){
var stateToTransition = this.get('isNew') ? 'created.uncommitted' : 'updated.uncommitted';
this.transitionTo(stateToTransition);
store.recordWasInvalid(this, errors);
var recordModel = this.adapterDidInvalidate ? this : this._internalModel;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This fixes the errors with 1.13.x and allows for current uses to still work. @esbanarango

store.recordWasInvalid(recordModel, errors);
}
return false;
}else{
Expand All @@ -47,14 +48,17 @@ export default Ember.Mixin.create({

/**** Validators ****/
_validateCustom: function(property, validation){
var customValidator = this._getCustomValidator(validation);
if (customValidator) {
var passedCustomValidation = customValidator(property, this.get(property), this);
if (!passedCustomValidation) {
this.set('isValidNow', false);
this._addToErrors(property, validation.custom, Messages.customValidationMessage);
}
}
validation = Ember.isArray(validation.custom) ? validation.custom : [validation.custom];
for (var i = 0; i < validation.length; i++) {
var customValidator = this._getCustomValidator(validation[i]);
if (customValidator) {
var passedCustomValidation = customValidator(property, this.get(property), this);
if (!passedCustomValidation) {
this.set('isValidNow', false);
this._addToErrors(property, validation[i], Messages.customValidationMessage);
}
}
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this now ensures that validation is an array or converts it to one. then iterates over it and executes the custom validation as it did before, but for each in the array

},
_validatePresence: function(property, validation) {
if (Ember.isBlank(this.get(property))){
Expand Down Expand Up @@ -248,10 +252,10 @@ export default Ember.Mixin.create({
return validateThis;
},
_getCustomValidator: function(validation){
var customValidator = validation.custom;
if (Ember.typeOf(validation.custom) === 'object' && validation.custom.hasOwnProperty('validation')) {
customValidator = validation.custom.validation;
}
var customValidator = validation;
if (Ember.typeOf(validation) === 'object' && validation.hasOwnProperty('validation')) {
customValidator = validation.validation;
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this just changes how the function is passed a validation object and returns the custom validation funciton (to account for it being iterated over in the function calling it)

return typeof customValidator === 'function' ? customValidator : false;
},
_getCustomMessage: function(validationObj,defaultMessage) {
Expand Down
10 changes: 5 additions & 5 deletions bower.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"name": "ember-model-validator",
"dependencies": {
"ember": "1.11.1",
"ember": "1.13.3",
"ember-cli-shims": "ember-cli/ember-cli-shims#0.0.3",
"ember-cli-test-loader": "ember-cli-test-loader#0.1.3",
"ember-data": "1.0.0-beta.16.1",
"ember-load-initializers": "ember-cli/ember-load-initializers#0.1.4",
"ember-data": "1.13.5",
"ember-load-initializers": "ember-cli/ember-load-initializers#0.1.5",
"ember-mocha": "~0.7.0",
"ember-resolver": "~0.1.15",
"ember-resolver": "~0.1.18",
"jquery": "^1.11.1",
"loader.js": "ember-cli/loader.js#3.2.0"
}
}
}
35 changes: 35 additions & 0 deletions config/ember-try.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
module.exports = {
scenarios: [
{
name: 'default',
dependencies: { }
},
{
name: 'ember-release',
dependencies: {
'ember': 'components/ember#release'
},
resolutions: {
'ember': 'release'
}
},
// {
// name: 'ember-beta',
// dependencies: {
// 'ember': 'components/ember#beta'
// },
// resolutions: {
// 'ember': 'beta'
// }
// },
// {
// name: 'ember-canary',
// dependencies: {
// 'ember': 'components/ember#canary'
// },
// resolutions: {
// 'ember': 'canary'
// }
// }
]
};
17 changes: 17 additions & 0 deletions ember-cli-build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/* global require, module */
var EmberApp = require('ember-cli/lib/broccoli/ember-addon');

module.exports = function(defaults) {
var app = new EmberApp(defaults, {
// Add options here
});

/*
This build file specifes the options for the dummy test app of this
addon, located in `/tests/dummy`
This build file does *not* influence how the addon or the app using it
behave. You most likely want to be modifying `./index.js` or app's build file
*/

return app.toTree();
};
18 changes: 11 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,22 @@
"license": "MIT",
"devDependencies": {
"broccoli-asset-rev": "^2.0.2",
"ember-cli": "0.2.3",
"ember-cli-app-version": "0.3.3",
"ember-cli-dependency-checker": "0.0.8",
"ember-cli-htmlbars": "0.7.4",
"ember-cli-ic-ajax": "0.1.1",
"ember-cli": "^1.13.1",
"ember-cli-app-version": "0.4.0",
"ember-cli-dependency-checker": "1.0.0",
"ember-cli-content-security-policy": "0.4.0",
"ember-cli-htmlbars": "0.7.9",
"ember-cli-htmlbars-inline-precompile": "^0.1.1",
"ember-cli-ic-ajax": "0.2.1",
"ember-cli-inject-live-reload": "^1.3.0",
"ember-cli-mocha": "^0.6.1",
"ember-cli-uglify": "1.0.1",
"ember-data": "1.0.0-beta.16.1",
"ember-cli-release": "0.2.3",
"ember-data": "1.13.5",
"ember-disable-prototype-extensions": "^1.0.0",
"ember-disable-proxy-controllers": "^1.0.0",
"ember-export-application-global": "^1.0.2",
"ember-try": "0.0.4"
"ember-try": "0.0.6"
},
"keywords": [
"ember-addon",
Expand Down
18 changes: 17 additions & 1 deletion tests/dummy/app/models/fake-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ export default DS.Model.extend(Validator,{

otherFake: DS.belongsTo('other-model'),

thing: DS.attr(''),

validations: {
name: {
presence: { errorAs:'profile.name' },
Expand Down Expand Up @@ -81,6 +83,20 @@ export default DS.Model.extend(Validator,{
mustContainNumber: true,
mustContainSpecial: true
},
thing: {
custom: [
{
validation: function(key, value, _this){
return (value !== 'blahblahblahblahbthishouldneverfaillahblahblah');
}
},
{
validation: function(key, value, _this){
return (value !== 'fail');
}
}
]
},
mySubdomain:{
subdomain:{ reserved:['admin','blog'], message: 'this subdomain is super invalid' }
},
Expand All @@ -98,7 +114,7 @@ export default DS.Model.extend(Validator,{
},
lotteryNumber: {
numericality: true,
custom: {
custom: {
validation: function(key, value, _this){
var favColor = _this.get('favoriteColor');
return !!favColor;
Expand Down
6 changes: 6 additions & 0 deletions tests/unit/mixins/model-validator-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ describe('ModelValidatorMixin', function() {
expect( model.get('errors').errorsFor('password').mapBy('message')[0][0] ).to.equal(Messages.customValidationMessage);
});

it('validates the an array of custom validations', function(){
var model = this.subject({thing: 'fail'});
expect(model.validate()).to.equal(false);
expect( model.get('errors').errorsFor('thing').mapBy('message')[0][0] ).to.equal(Messages.customValidationMessage);
});

it('validates the email format of the attributes set on `validations.email`', function() {
var model = this.subject({email:'adsfasdf$'});
expect(model.validate()).to.equal(false);
Expand Down