diff --git a/README.md b/README.md index 7ed89ad3..b094d23b 100644 --- a/README.md +++ b/README.md @@ -30,24 +30,23 @@ You can also learn more by watching this Global Ember Meetup talk: [![Introduction to ember-cp-validations](https://i.vimeocdn.com/video/545445254.png?mw=1920&mh=1080&q=70)](https://vimeo.com/146857699) -Installation ------------------------------------------------------------------------------- +## Installation ```shell ember install ember-cp-validations ``` -## Upgrading to 3.x +## Upgrading to 4.x -If you are upgrading from 2.x to 3.x, please checkout the [upgrading documentation](UPGRADING.md). +If you are upgrading from 3.x to 4.x, please checkout the [upgrading documentation](UPGRADING.md). ## Helpful Links - ### [Live Demo](http://offirgolan.github.io/ember-cp-validations) - ### Documentation - - [3.x](http://offirgolan.github.io/ember-cp-validations/docs) - - [2.x](https://rawgit.com/offirgolan/ember-cp-validations/c08fedbf3dcfff1e8904a6469c8defd1fc2bfdf5/docs/modules/Home.html) + - [4.x](http://offirgolan.github.io/ember-cp-validations/docs) + - [3.x](https://rawgit.com/offirgolan/ember-cp-validations/c4123c983e54f24dd790ffa1bad66cfdf2f47ec6/docs/index.html) - ### [Changelog](CHANGELOG.md) diff --git a/UPGRADING.md b/UPGRADING.md index 0a344c6e..aa4fc553 100644 --- a/UPGRADING.md +++ b/UPGRADING.md @@ -1,70 +1,42 @@ -# Upgrading v2.x to 3.x +# Upgrading v3.x to 4.x This document is here to show breaking changes when upgrading from v2.x to v3.x -### Computed Options +## Support Latest 2 LTS Releases -In 2.x, we introduced the notion of `Options as Functions` which allowed any validator's option to be a function that would -be lazily called right before a validation happened. In 3.x, we noticed that such implementation very much resembled the -workings of a Computed Property (without the caching of course). Converting our functional approach to an Ember CP approach made defining validations a whole lot simpler. +As Ember is evolving, we have to be able to keep up. v3.x supported Ember versions as old +as 1.11 which not only made this addon difficult to maintain, but also added a +lot of bloat. Going forward, this addon will target and test against only the 2 +latest LTS releases. -**Before (2.x)** +## Inline Validator -```javascript -validator('length', { - dependentKeys: ['isDisabled', 'meta.username.minLength', 'meta.username.maxLength'], - disabled(model) { - return model.get('isDisabled'); - }, - min(model) { - return model.get('meta.username.minLength') - }, - max(model) { - return model.get('meta.username.maxLength') - }, - description(model, attribute) { - return model.generateDescription(attribute); - } -}); -``` - -**After (3.x)** - -```javascript -validator('length', { - disabled: Ember.computed.not('model.meta.username.isEnabled'), - min: Ember.computed.readOnly('model.meta.username.minLength'), - max: Ember.computed.readOnly('model.meta.username.maxLength'), - description: Ember.computed(function() { - // CPs have access to the `model` and `attribute` - return this.get('model').generateDescription(this.get('attribute')); - }).volatile() // Disable caching and force recompute on every get call -}); -``` - -Some more reasons why this is better: - -- Any option that uses a CP doesn't have to re-declare those dependents in the `dependentKeys` collection. -- You can use any Ember.computed operation (computed.`and`, computed.`or`, computed.`filterBy`, etc.) +This library has always supported the ability to pass in a custom validate function +to the `validator` but it didn't feel consistent with the rest of the API. To normalize +this, we created a new `inline` validator that you can pass a function to via +the `validate` option. -### dependentKeys - -There might be instances where your validator is dependent on external properties. For this reason, we introduced `dependentKeys` in 2.x. In 3.x, the only change to this is that all dependent keys must be prefixed with `model`. - -**Before (2.x)** +**Before (3.x)** ```javascript -validator('presence', { - presence: true, - dependentKeys: ['someService.someProperty', 'foo', 'bar.baz'] +validator(function(value, options, model, attribute) { + return value === options.username ? + true : + `Username must be ${options.username}`; +}, { + username: 'offirgolan' }); ``` -**After (3.x)** +**After (4.x)** ```javascript -validator('presence', { - presence: true, - dependentKeys: ['model.someService.someProperty', 'model.foo', 'model.bar.baz'] +validator('inline', { + username: 'offirgolan', + validate(value, options, model, attribute) { + return value === options.username ? + true : + `Username must be ${options.username}`; + } }); ``` diff --git a/addon/validators/base.js b/addon/validators/base.js index 8b55d140..b14a9ef0 100644 --- a/addon/validators/base.js +++ b/addon/validators/base.js @@ -274,7 +274,7 @@ const Base = EmberObject.extend({ * @param {String} type The validator type (e.x. 'presence', 'length', etc.) * The following types are unsupported: * 'alias', 'belongs-to', 'dependent', 'has-many' - * @param {...} args The params to pass through to the validator + * @param {...args} args The arguments to pass through to the validator * @return {Object} The test result object which will contain `isValid` * and `message`. If the validator is async, then the * return value will be a promise. diff --git a/package.json b/package.json index e094edaa..ae4a9ae6 100644 --- a/package.json +++ b/package.json @@ -80,14 +80,16 @@ "loader.js": "^4.2.3", "prettier": "^1.10.2", "qunit-dom": "^0.5.0", - "yuidoc-ember-theme": "^1.4.0" + "yuidoc-ember-theme": "^2.0.1" }, "keywords": [ "ember-addon", "ember-data", "validator", "validation", - "model" + "model", + "input", + "form" ], "dependencies": { "ember-cli-babel": "^6.6.0", diff --git a/tests/dummy/app/templates/application.hbs b/tests/dummy/app/templates/application.hbs index c6fbce89..870fe9dc 100644 --- a/tests/dummy/app/templates/application.hbs +++ b/tests/dummy/app/templates/application.hbs @@ -12,7 +12,8 @@ {{#nav.dropdown as |dd|}} {{#dd.toggle}}Documentation {{/dd.toggle}} {{#dd.menu as |ddm|}} - {{#ddm.item}}v3.x{{/ddm.item}} + {{#ddm.item}}v4.x{{/ddm.item}} + {{#ddm.item}}v3.x{{/ddm.item}} {{#ddm.item}}v2.x{{/ddm.item}} {{/dd.menu}} {{/nav.dropdown}} diff --git a/yarn.lock b/yarn.lock index 1bdd887a..b427b3c1 100644 --- a/yarn.lock +++ b/yarn.lock @@ -9238,9 +9238,9 @@ yui@^3.18.1: dependencies: request "~2.40.0" -yuidoc-ember-theme@^1.4.0: - version "1.4.0" - resolved "https://registry.yarnpkg.com/yuidoc-ember-theme/-/yuidoc-ember-theme-1.4.0.tgz#b7ba649be58993530b3092e12fc114a07e81c414" +yuidoc-ember-theme@^2.0.1: + version "2.0.1" + resolved "https://registry.yarnpkg.com/yuidoc-ember-theme/-/yuidoc-ember-theme-2.0.1.tgz#3798c592e3f35bf4216b7be40903cc039702c8b6" yuidocjs@^0.10.0: version "0.10.2"