From fb746c0bbb69105e8b9273193f935d985f0c68b7 Mon Sep 17 00:00:00 2001 From: david Date: Mon, 13 Jul 2015 17:51:32 -0700 Subject: [PATCH 1/4] fix broken validation and added support for multiple custom validation on the same property --- addon/mixins/model-validator.js | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/addon/mixins/model-validator.js b/addon/mixins/model-validator.js index 40f65211..c8f33f63 100644 --- a/addon/mixins/model-validator.js +++ b/addon/mixins/model-validator.js @@ -37,7 +37,7 @@ 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); + store.recordWasInvalid(this._internalModel, errors); } return false; }else{ @@ -47,14 +47,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); + } + } + } }, _validatePresence: function(property, validation) { if (Ember.isBlank(this.get(property))){ @@ -248,10 +251,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; + } return typeof customValidator === 'function' ? customValidator : false; }, _getCustomMessage: function(validationObj,defaultMessage) { From 084d0226aa16df89e9bbeefc65c86dc6e234e24f Mon Sep 17 00:00:00 2001 From: david Date: Mon, 13 Jul 2015 20:07:26 -0700 Subject: [PATCH 2/4] added test for array of customs and upgraded ember-cli/ember/enmber-data --- .npmignore | 2 ++ .watchmanconfig | 3 ++ README.md | 31 ++++++++++++++++---- bower.json | 10 +++---- config/ember-try.js | 35 +++++++++++++++++++++++ ember-cli-build.js | 17 +++++++++++ package.json | 18 +++++++----- tests/dummy/app/models/fake-model.js | 18 +++++++++++- tests/unit/mixins/model-validator-test.js | 6 ++++ 9 files changed, 122 insertions(+), 18 deletions(-) create mode 100644 .watchmanconfig create mode 100644 config/ember-try.js create mode 100644 ember-cli-build.js diff --git a/.npmignore b/.npmignore index a28e4ab3..d522ca2b 100644 --- a/.npmignore +++ b/.npmignore @@ -1,6 +1,7 @@ bower_components/ tests/ tmp/ +dist/ .bowerrc .editorconfig @@ -9,5 +10,6 @@ tmp/ .npmignore **/.gitkeep bower.json +ember-cli-build.js Brocfile.js testem.json diff --git a/.watchmanconfig b/.watchmanconfig new file mode 100644 index 00000000..5e9462c2 --- /dev/null +++ b/.watchmanconfig @@ -0,0 +1,3 @@ +{ + "ignore_dirs": ["tmp"] +} diff --git a/README.md b/README.md index 98292043..0f1f4a94 100644 --- a/README.md +++ b/README.md @@ -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! diff --git a/bower.json b/bower.json index 701306d4..9ca2fae9 100644 --- a/bower.json +++ b/bower.json @@ -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" } -} \ No newline at end of file +} diff --git a/config/ember-try.js b/config/ember-try.js new file mode 100644 index 00000000..83dab0f1 --- /dev/null +++ b/config/ember-try.js @@ -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' + } + } + ] +}; diff --git a/ember-cli-build.js b/ember-cli-build.js new file mode 100644 index 00000000..d37d64cd --- /dev/null +++ b/ember-cli-build.js @@ -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(); +}; diff --git a/package.json b/package.json index 6d805421..d9e87370 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/tests/dummy/app/models/fake-model.js b/tests/dummy/app/models/fake-model.js index 2032965f..03f65e9b 100644 --- a/tests/dummy/app/models/fake-model.js +++ b/tests/dummy/app/models/fake-model.js @@ -34,6 +34,8 @@ export default DS.Model.extend(Validator,{ otherFake: DS.belongsTo('other-model'), + thing: DS.attr(''), + validations: { name: { presence: { errorAs:'profile.name' }, @@ -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' } }, @@ -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; diff --git a/tests/unit/mixins/model-validator-test.js b/tests/unit/mixins/model-validator-test.js index 60ae561c..17a753dc 100644 --- a/tests/unit/mixins/model-validator-test.js +++ b/tests/unit/mixins/model-validator-test.js @@ -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); From 753d3067b06df721eab4e7c24d67adb1bdd15384 Mon Sep 17 00:00:00 2001 From: david Date: Mon, 13 Jul 2015 20:33:20 -0700 Subject: [PATCH 3/4] commented out trytest for beta and canary --- config/ember-try.js | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/config/ember-try.js b/config/ember-try.js index 83dab0f1..2ee8d734 100644 --- a/config/ember-try.js +++ b/config/ember-try.js @@ -13,23 +13,23 @@ module.exports = { 'ember': 'release' } }, - { - name: 'ember-beta', - dependencies: { - 'ember': 'components/ember#beta' - }, - resolutions: { - 'ember': 'beta' - } - }, - { - name: 'ember-canary', - dependencies: { - 'ember': 'components/ember#canary' - }, - resolutions: { - 'ember': 'canary' - } - } + // { + // name: 'ember-beta', + // dependencies: { + // 'ember': 'components/ember#beta' + // }, + // resolutions: { + // 'ember': 'beta' + // } + // }, + // { + // name: 'ember-canary', + // dependencies: { + // 'ember': 'components/ember#canary' + // }, + // resolutions: { + // 'ember': 'canary' + // } + // } ] }; From ed05ac7cee5981129005538e2db9056401a55fd6 Mon Sep 17 00:00:00 2001 From: david Date: Mon, 13 Jul 2015 20:47:25 -0700 Subject: [PATCH 4/4] added check for adapterDidInvalidate so that it should not break current users --- addon/mixins/model-validator.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/addon/mixins/model-validator.js b/addon/mixins/model-validator.js index c8f33f63..b8b69f54 100644 --- a/addon/mixins/model-validator.js +++ b/addon/mixins/model-validator.js @@ -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._internalModel, errors); + var recordModel = this.adapterDidInvalidate ? this : this._internalModel; + store.recordWasInvalid(recordModel, errors); } return false; }else{