diff --git a/.eslintrc.js b/.eslintrc.js index 434405ad..3a5461d4 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -1,5 +1,6 @@ module.exports = { root: true, + parser: 'babel-eslint', parserOptions: { ecmaVersion: 2017, sourceType: 'module' diff --git a/.travis.yml b/.travis.yml index 159e77e9..af0932c1 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 @@ -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: diff --git a/README.md b/README.md index 586a52aa..f5c30601 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/addon/.gitkeep b/addon/.gitkeep deleted file mode 100644 index e69de29b..00000000 diff --git a/addon/decorators/has-validations.js b/addon/decorators/has-validations.js new file mode 100644 index 00000000..b54f7b1c --- /dev/null +++ b/addon/decorators/has-validations.js @@ -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); + }; +} diff --git a/addon/index.js b/addon/index.js index df09ca82..c5436f0b 100644 --- a/addon/index.js +++ b/addon/index.js @@ -1,5 +1,6 @@ import Factory from './validations/factory'; import Validator from './validations/validator'; +import { hasValidations as validationDecorator } from './decorators/has-validations'; /** * ## Installation @@ -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 }; diff --git a/package.json b/package.json index 5a4a7693..ca6f4b1a 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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", diff --git a/tests/dummy/app/models/.gitkeep b/tests/dummy/app/models/.gitkeep deleted file mode 100644 index e69de29b..00000000 diff --git a/tests/dummy/app/models/company.js b/tests/dummy/app/models/company.js new file mode 100644 index 00000000..41b8f08e --- /dev/null +++ b/tests/dummy/app/models/company.js @@ -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; +} diff --git a/tests/unit/validations/ds-model-test.js b/tests/unit/validations/ds-model-test.js index 2c365878..d54a1857 100644 --- a/tests/unit/validations/ds-model-test.js +++ b/tests/unit/validations/ds-model-test.js @@ -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'); + }); }); diff --git a/yarn.lock b/yarn.lock index b427b3c1..2a263e04 100644 --- a/yarn.lock +++ b/yarn.lock @@ -8,6 +8,12 @@ dependencies: "@babel/highlight" "7.0.0-beta.42" +"@babel/code-frame@7.0.0-beta.44": + 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/generator@7.0.0-beta.42": version "7.0.0-beta.42" resolved "https://registry.yarnpkg.com/@babel/generator/-/generator-7.0.0-beta.42.tgz#777bb50f39c94a7e57f73202d833141f8159af33" @@ -18,6 +24,16 @@ source-map "^0.5.0" trim-right "^1.0.1" +"@babel/generator@7.0.0-beta.44": + 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/helper-function-name@7.0.0-beta.42": 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" @@ -26,18 +42,38 @@ "@babel/template" "7.0.0-beta.42" "@babel/types" "7.0.0-beta.42" +"@babel/helper-function-name@7.0.0-beta.44": + 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/helper-get-function-arity@7.0.0-beta.42": 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/helper-get-function-arity@7.0.0-beta.44": + 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/helper-split-export-declaration@7.0.0-beta.42": 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/helper-split-export-declaration@7.0.0-beta.44": + 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/highlight@7.0.0-beta.42": version "7.0.0-beta.42" resolved "https://registry.yarnpkg.com/@babel/highlight/-/highlight-7.0.0-beta.42.tgz#a502a1c0d6f99b2b0e81d468a1b0c0e81e3f3623" @@ -46,6 +82,14 @@ esutils "^2.0.2" js-tokens "^3.0.0" +"@babel/highlight@7.0.0-beta.44": + 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/template@7.0.0-beta.42": version "7.0.0-beta.42" resolved "https://registry.yarnpkg.com/@babel/template/-/template-7.0.0-beta.42.tgz#7186d4e70d44cdec975049ba0a73bdaf5cdee052" @@ -55,6 +99,30 @@ babylon "7.0.0-beta.42" lodash "^4.2.0" +"@babel/template@7.0.0-beta.44": + 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/traverse@7.0.0-beta.44": + 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" @@ -78,6 +146,14 @@ lodash "^4.2.0" to-fast-properties "^2.0.0" +"@babel/types@7.0.0-beta.44": + 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" @@ -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" @@ -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" @@ -1211,6 +1307,10 @@ babylon@7.0.0-beta.42, 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" +babylon@7.0.0-beta.44: + 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" @@ -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" @@ -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: @@ -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"