Skip to content

Commit

Permalink
export hasValidations decorator for es6 style models (#583)
Browse files Browse the repository at this point in the history
* export hasValidations decorator for es6 style models

update docs

* update node level

* don’t require ember-beta to succeed

* streamline test
  • Loading branch information
danielspaniel authored and offirgolan committed Apr 16, 2018
1 parent e7616ed commit 580cced
Show file tree
Hide file tree
Showing 11 changed files with 168 additions and 7 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module.exports = {
root: true,
parser: 'babel-eslint',
parserOptions: {
ecmaVersion: 2017,
sourceType: 'module'
Expand Down
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ language: node_js
node_js:
# we recommend testing addons with the same minimum supported node version as Ember CLI
# so that your addon works for all apps
- "4"
- "6"

sudo: false
dist: trusty
Expand Down Expand Up @@ -33,6 +33,7 @@ env:
matrix:
fast_finish: true
allow_failures:
- env: EMBER_TRY_SCENARIO=ember-beta
- env: EMBER_TRY_SCENARIO=ember-canary

before_install:
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ __No observers were used nor harmed while developing and testing this addon.__
- I18n support
- Debounceable validations
- Warning validations
- Decortators for es6 style model/component classes

[![Introduction to Ember CP Validations](https://cloud.githubusercontent.com/assets/2922250/21854491/ebda55b8-d7e8-11e6-8d13-00dff93be8d8.png)](https://embermap.com/video/ember-cp-validations)

Expand Down
Empty file removed addon/.gitkeep
Empty file.
22 changes: 22 additions & 0 deletions addon/decorators/has-validations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Decorator that allows you to mixin validations when using es6 style model classes
*
* Usage:
*
* import { hasValidations } from 'ember-cp-validations';
*
* @hasValidations(Validations)
* export default class YourModel extends Model {
* @attr('string') name
* };
*
*
* @param validations
* @returns {decorator}
*/

export function hasValidations(validations) {
return function decorator(target) {
target.prototype.reopen(validations);
};
}
5 changes: 4 additions & 1 deletion addon/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Factory from './validations/factory';
import Validator from './validations/validator';
import { hasValidations as validationDecorator } from './decorators/has-validations';

/**
* ## Installation
Expand Down Expand Up @@ -301,8 +302,10 @@ import Validator from './validations/validator';

export const buildValidations = Factory;
export const validator = Validator;
export const hasValidations = validationDecorator;

export default {
buildValidations,
validator
validator,
hasValidations
};
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
],
"license": "MIT",
"devDependencies": {
"@ember-decorators/babel-transforms": "^2.0.1",
"babel-eslint": "^8.2.3",
"bootstrap-sass": "^3.3.7",
"broccoli-asset-rev": "^2.4.6",
"ember-bootstrap": "^1.2.1",
Expand All @@ -60,6 +62,7 @@
"ember-cli-yuidoc": "^0.8.8",
"ember-code-snippet": "^2.0.1",
"ember-data": "^2.18.0",
"ember-decorators": "^1.3.4",
"ember-disable-prototype-extensions": "^1.1.2",
"ember-export-application-global": "^2.0.0",
"ember-font-awesome": "^3.0.5",
Expand Down
Empty file removed tests/dummy/app/models/.gitkeep
Empty file.
16 changes: 16 additions & 0 deletions tests/dummy/app/models/company.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import Model from 'ember-data/model';
import { attr } from 'ember-decorators/data';
import {
hasValidations,
validator,
buildValidations
} from 'ember-cp-validations';

const Validations = buildValidations({
name: validator('presence', { presence: true, description: 'Name' })
});

@hasValidations(Validations)
export default class Company extends Model {
@attr('string') name;
}
14 changes: 14 additions & 0 deletions tests/unit/validations/ds-model-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,18 @@ module('Unit | Validations | DS.Model', function(hooks) {
'isValid was expected to be FALSE'
);
});

test('for es6 class style model with decorator declaring validations', function(assert) {
let company = run(() => {
return this.owner.lookup('service:store').createRecord('company');
});

run(() => company.setProperties({ name: null }));

assert.notOk(company.get('validations.isValid'), 'isValid => FALSE');

run(() => company.setProperties({ name: 'dude' }));

assert.ok(company.get('validations.isValid'), 'isValid => TRUE');
});
});
110 changes: 105 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
dependencies:
"@babel/highlight" "7.0.0-beta.42"

"@babel/[email protected]":
version "7.0.0-beta.44"
resolved "https://registry.yarnpkg.com/@babel/code-frame/-/code-frame-7.0.0-beta.44.tgz#2a02643368de80916162be70865c97774f3adbd9"
dependencies:
"@babel/highlight" "7.0.0-beta.44"

"@babel/[email protected]":
version "7.0.0-beta.42"
resolved "https://registry.yarnpkg.com/@babel/generator/-/generator-7.0.0-beta.42.tgz#777bb50f39c94a7e57f73202d833141f8159af33"
Expand All @@ -18,6 +24,16 @@
source-map "^0.5.0"
trim-right "^1.0.1"

"@babel/[email protected]":
version "7.0.0-beta.44"
resolved "https://registry.yarnpkg.com/@babel/generator/-/generator-7.0.0-beta.44.tgz#c7e67b9b5284afcf69b309b50d7d37f3e5033d42"
dependencies:
"@babel/types" "7.0.0-beta.44"
jsesc "^2.5.1"
lodash "^4.2.0"
source-map "^0.5.0"
trim-right "^1.0.1"

"@babel/[email protected]":
version "7.0.0-beta.42"
resolved "https://registry.yarnpkg.com/@babel/helper-function-name/-/helper-function-name-7.0.0-beta.42.tgz#b38b8f4f85168d1812c543dd700b5d549b0c4658"
Expand All @@ -26,18 +42,38 @@
"@babel/template" "7.0.0-beta.42"
"@babel/types" "7.0.0-beta.42"

"@babel/[email protected]":
version "7.0.0-beta.44"
resolved "https://registry.yarnpkg.com/@babel/helper-function-name/-/helper-function-name-7.0.0-beta.44.tgz#e18552aaae2231100a6e485e03854bc3532d44dd"
dependencies:
"@babel/helper-get-function-arity" "7.0.0-beta.44"
"@babel/template" "7.0.0-beta.44"
"@babel/types" "7.0.0-beta.44"

"@babel/[email protected]":
version "7.0.0-beta.42"
resolved "https://registry.yarnpkg.com/@babel/helper-get-function-arity/-/helper-get-function-arity-7.0.0-beta.42.tgz#ad072e32f912c033053fc80478169aeadc22191e"
dependencies:
"@babel/types" "7.0.0-beta.42"

"@babel/[email protected]":
version "7.0.0-beta.44"
resolved "https://registry.yarnpkg.com/@babel/helper-get-function-arity/-/helper-get-function-arity-7.0.0-beta.44.tgz#d03ca6dd2b9f7b0b1e6b32c56c72836140db3a15"
dependencies:
"@babel/types" "7.0.0-beta.44"

"@babel/[email protected]":
version "7.0.0-beta.42"
resolved "https://registry.yarnpkg.com/@babel/helper-split-export-declaration/-/helper-split-export-declaration-7.0.0-beta.42.tgz#0d0d5254220a9cc4e7e226240306b939dc210ee7"
dependencies:
"@babel/types" "7.0.0-beta.42"

"@babel/[email protected]":
version "7.0.0-beta.44"
resolved "https://registry.yarnpkg.com/@babel/helper-split-export-declaration/-/helper-split-export-declaration-7.0.0-beta.44.tgz#c0b351735e0fbcb3822c8ad8db4e583b05ebd9dc"
dependencies:
"@babel/types" "7.0.0-beta.44"

"@babel/[email protected]":
version "7.0.0-beta.42"
resolved "https://registry.yarnpkg.com/@babel/highlight/-/highlight-7.0.0-beta.42.tgz#a502a1c0d6f99b2b0e81d468a1b0c0e81e3f3623"
Expand All @@ -46,6 +82,14 @@
esutils "^2.0.2"
js-tokens "^3.0.0"

"@babel/[email protected]":
version "7.0.0-beta.44"
resolved "https://registry.yarnpkg.com/@babel/highlight/-/highlight-7.0.0-beta.44.tgz#18c94ce543916a80553edcdcf681890b200747d5"
dependencies:
chalk "^2.0.0"
esutils "^2.0.2"
js-tokens "^3.0.0"

"@babel/[email protected]":
version "7.0.0-beta.42"
resolved "https://registry.yarnpkg.com/@babel/template/-/template-7.0.0-beta.42.tgz#7186d4e70d44cdec975049ba0a73bdaf5cdee052"
Expand All @@ -55,6 +99,30 @@
babylon "7.0.0-beta.42"
lodash "^4.2.0"

"@babel/[email protected]":
version "7.0.0-beta.44"
resolved "https://registry.yarnpkg.com/@babel/template/-/template-7.0.0-beta.44.tgz#f8832f4fdcee5d59bf515e595fc5106c529b394f"
dependencies:
"@babel/code-frame" "7.0.0-beta.44"
"@babel/types" "7.0.0-beta.44"
babylon "7.0.0-beta.44"
lodash "^4.2.0"

"@babel/[email protected]":
version "7.0.0-beta.44"
resolved "https://registry.yarnpkg.com/@babel/traverse/-/traverse-7.0.0-beta.44.tgz#a970a2c45477ad18017e2e465a0606feee0d2966"
dependencies:
"@babel/code-frame" "7.0.0-beta.44"
"@babel/generator" "7.0.0-beta.44"
"@babel/helper-function-name" "7.0.0-beta.44"
"@babel/helper-split-export-declaration" "7.0.0-beta.44"
"@babel/types" "7.0.0-beta.44"
babylon "7.0.0-beta.44"
debug "^3.1.0"
globals "^11.1.0"
invariant "^2.2.0"
lodash "^4.2.0"

"@babel/traverse@^7.0.0-beta.40":
version "7.0.0-beta.42"
resolved "https://registry.yarnpkg.com/@babel/traverse/-/traverse-7.0.0-beta.42.tgz#f4bf4d1e33d41baf45205e2d0463591d57326285"
Expand All @@ -78,6 +146,14 @@
lodash "^4.2.0"
to-fast-properties "^2.0.0"

"@babel/[email protected]":
version "7.0.0-beta.44"
resolved "https://registry.yarnpkg.com/@babel/types/-/types-7.0.0-beta.44.tgz#6b1b164591f77dec0a0342aca995f2d046b3a757"
dependencies:
esutils "^2.0.2"
lodash "^4.2.0"
to-fast-properties "^2.0.0"

"@ember-decorators/argument@^0.8.10":
version "0.8.13"
resolved "https://registry.yarnpkg.com/@ember-decorators/argument/-/argument-0.8.13.tgz#217804178c4278a8c264a2878b66c0cb55b77e3d"
Expand All @@ -100,6 +176,15 @@
ember-cli-babel "^6.6.0"
ember-cli-version-checker "^2.1.0"

"@ember-decorators/babel-transforms@^2.0.1":
version "2.0.1"
resolved "https://registry.yarnpkg.com/@ember-decorators/babel-transforms/-/babel-transforms-2.0.1.tgz#75b668cfe996fa920c940ad723be015cddf904db"
dependencies:
babel-plugin-transform-class-properties "^6.24.1"
babel-plugin-transform-decorators-legacy "^1.3.4"
ember-cli-babel "^6.6.0"
ember-cli-version-checker "^2.1.0"

"@ember-decorators/utils@^0.2.0":
version "0.2.0"
resolved "https://registry.yarnpkg.com/@ember-decorators/utils/-/utils-0.2.0.tgz#395362c75c4f85aa63aa7cbed77a6486fd6e5f22"
Expand Down Expand Up @@ -637,6 +722,17 @@ babel-eslint@^8.0.3:
eslint-scope "~3.7.1"
eslint-visitor-keys "^1.0.0"

babel-eslint@^8.2.3:
version "8.2.3"
resolved "https://registry.yarnpkg.com/babel-eslint/-/babel-eslint-8.2.3.tgz#1a2e6681cc9bc4473c32899e59915e19cd6733cf"
dependencies:
"@babel/code-frame" "7.0.0-beta.44"
"@babel/traverse" "7.0.0-beta.44"
"@babel/types" "7.0.0-beta.44"
babylon "7.0.0-beta.44"
eslint-scope "~3.7.1"
eslint-visitor-keys "^1.0.0"

babel-generator@^6.26.0:
version "6.26.1"
resolved "https://registry.yarnpkg.com/babel-generator/-/babel-generator-6.26.1.tgz#1844408d3b8f0d35a404ea7ac180f087a601bd90"
Expand Down Expand Up @@ -1211,6 +1307,10 @@ [email protected], babylon@^7.0.0-beta.40:
version "7.0.0-beta.42"
resolved "https://registry.yarnpkg.com/babylon/-/babylon-7.0.0-beta.42.tgz#67cfabcd4f3ec82999d29031ccdea89d0ba99657"

[email protected]:
version "7.0.0-beta.44"
resolved "https://registry.yarnpkg.com/babylon/-/babylon-7.0.0-beta.44.tgz#89159e15e6e30c5096e22d738d8c0af8a0e8ca1d"

babylon@^5.8.38:
version "5.8.38"
resolved "https://registry.yarnpkg.com/babylon/-/babylon-5.8.38.tgz#ec9b120b11bf6ccd4173a18bf217e60b79859ffd"
Expand Down Expand Up @@ -3289,8 +3389,8 @@ ember-concurrency@^0.8.7:
ember-maybe-import-regenerator "^0.1.5"

ember-data@^2.18.0:
version "2.18.0"
resolved "https://registry.yarnpkg.com/ember-data/-/ember-data-2.18.0.tgz#8e374e540ea7c3b101983eb51cd0beedaa4d230d"
version "2.18.2"
resolved "https://registry.yarnpkg.com/ember-data/-/ember-data-2.18.2.tgz#3c6e99c9aed77ef8945451c39f39865f370f3db4"
dependencies:
amd-name-resolver "0.0.7"
babel-plugin-ember-modules-api-polyfill "^1.4.2"
Expand Down Expand Up @@ -3322,7 +3422,7 @@ ember-data@^2.18.0:
semver "^5.1.0"
silent-error "^1.0.0"

ember-decorators@^1.3.2:
ember-decorators@^1.3.2, ember-decorators@^1.3.4:
version "1.3.4"
resolved "https://registry.yarnpkg.com/ember-decorators/-/ember-decorators-1.3.4.tgz#801115ae1be9157bbb75280991ee3d07d12c6cce"
dependencies:
Expand Down Expand Up @@ -5468,8 +5568,8 @@ jju@^1.1.0:
resolved "https://registry.yarnpkg.com/jju/-/jju-1.3.0.tgz#dadd9ef01924bc728b03f2f7979bdbd62f7a2aaa"

jquery@^3.2.1:
version "3.2.1"
resolved "https://registry.yarnpkg.com/jquery/-/jquery-3.2.1.tgz#5c4d9de652af6cd0a770154a631bba12b015c787"
version "3.3.1"
resolved "https://registry.yarnpkg.com/jquery/-/jquery-3.3.1.tgz#958ce29e81c9790f31be7792df5d4d95fc57fbca"

js-base64@^2.1.8:
version "2.4.3"
Expand Down

0 comments on commit 580cced

Please sign in to comment.