├── .codeclimate.yml ├── .editorconfig ├── .ember-cli ├── .eslintrc.js ├── .gitignore ├── .npmignore ├── .travis.yml ├── .watchmanconfig ├── CHANGELOG.md ├── LICENSE.md ├── README.md ├── addon ├── .gitkeep ├── helpers │ └── changeset.js └── index.js ├── app ├── .gitkeep └── helpers │ └── changeset.js ├── config ├── ember-try.js └── environment.js ├── ember-cli-build.js ├── index.js ├── package.json ├── testem.js ├── tests ├── dummy │ ├── app │ │ ├── app.js │ │ ├── components │ │ │ ├── .gitkeep │ │ │ └── user-form.js │ │ ├── controllers │ │ │ ├── .gitkeep │ │ │ └── index.js │ │ ├── helpers │ │ │ └── .gitkeep │ │ ├── index.html │ │ ├── models │ │ │ ├── .gitkeep │ │ │ └── user.js │ │ ├── resolver.js │ │ ├── router.js │ │ ├── routes │ │ │ ├── .gitkeep │ │ │ └── index.js │ │ ├── styles │ │ │ └── app.css │ │ └── templates │ │ │ ├── application.hbs │ │ │ ├── components │ │ │ ├── .gitkeep │ │ │ └── user-form.hbs │ │ │ └── index.hbs │ ├── config │ │ ├── environment.js │ │ └── targets.js │ └── public │ │ └── robots.txt ├── helpers │ ├── .gitkeep │ ├── module-for-acceptance.js │ └── validated-object.js ├── index.html ├── integration │ ├── helpers │ │ └── changeset-test.js │ └── index-test.js ├── test-helper.js └── unit │ └── .gitkeep ├── vendor └── .gitkeep └── yarn.lock /.codeclimate.yml: -------------------------------------------------------------------------------- 1 | languages: 2 | JavaScript: true 3 | exclude_paths: 4 | - "tests/*" 5 | - "config/*" 6 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # EditorConfig helps developers define and maintain consistent 2 | # coding styles between different editors and IDEs 3 | # editorconfig.org 4 | 5 | root = true 6 | 7 | 8 | [*] 9 | end_of_line = lf 10 | charset = utf-8 11 | trim_trailing_whitespace = true 12 | insert_final_newline = true 13 | indent_style = space 14 | indent_size = 2 15 | 16 | [*.hbs] 17 | insert_final_newline = false 18 | 19 | [*.{diff,md}] 20 | trim_trailing_whitespace = false 21 | -------------------------------------------------------------------------------- /.ember-cli: -------------------------------------------------------------------------------- 1 | { 2 | /** 3 | Ember CLI sends analytics information by default. The data is completely 4 | anonymous, but there are times when you might want to disable this behavior. 5 | 6 | Setting `disableAnalytics` to true will prevent any data from being sent. 7 | */ 8 | "disableAnalytics": false 9 | } 10 | -------------------------------------------------------------------------------- /.eslintrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | root: true, 3 | parserOptions: { 4 | ecmaVersion: 2017, 5 | sourceType: 'module' 6 | }, 7 | plugins: [ 8 | 'ember' 9 | ], 10 | extends: [ 11 | 'eslint:recommended', 12 | 'plugin:ember/recommended' 13 | ], 14 | env: { 15 | browser: true 16 | }, 17 | rules: { 18 | }, 19 | overrides: [ 20 | // node files 21 | { 22 | files: [ 23 | 'index.js', 24 | 'testem.js', 25 | 'ember-cli-build.js', 26 | 'config/**/*.js', 27 | 'tests/dummy/config/**/*.js' 28 | ], 29 | excludedFiles: [ 30 | 'app/**', 31 | 'addon/**', 32 | 'tests/dummy/app/**' 33 | ], 34 | parserOptions: { 35 | sourceType: 'script', 36 | ecmaVersion: 2015 37 | }, 38 | env: { 39 | browser: false, 40 | node: true 41 | }, 42 | plugins: ['node'], 43 | rules: Object.assign({}, require('eslint-plugin-node').configs.recommended.rules, { 44 | // add your custom rules and overrides for node files here 45 | }) 46 | } 47 | ] 48 | }; 49 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/ignore-files/ for more about ignoring files. 2 | 3 | # compiled output 4 | /dist 5 | /tmp 6 | 7 | # dependencies 8 | /node_modules 9 | /bower_components 10 | 11 | # misc 12 | /.sass-cache 13 | /connect.lock 14 | /coverage/* 15 | /libpeerconnection.log 16 | npm-debug.log* 17 | yarn-error.log 18 | testem.log 19 | /typings/* 20 | /.vscode/* 21 | .vscodeignore 22 | jsconfig.json 23 | 24 | # ember-try 25 | .node_modules.ember-try/ 26 | bower.json.ember-try 27 | package.json.ember-try 28 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | /bower_components 2 | /config/ember-try.js 3 | /dist 4 | /tests 5 | /tmp 6 | **/.gitkeep 7 | .bowerrc 8 | .editorconfig 9 | .ember-cli 10 | .eslintrc.js 11 | .gitignore 12 | .watchmanconfig 13 | .travis.yml 14 | bower.json 15 | ember-cli-build.js 16 | testem.js 17 | 18 | # ember-try 19 | .node_modules.ember-try/ 20 | bower.json.ember-try 21 | package.json.ember-try 22 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | --- 2 | language: node_js 3 | node_js: 4 | # we recommend testing addons with the same minimum supported node version as Ember CLI 5 | # so that your addon works for all apps 6 | - "6" 7 | 8 | dist: trusty 9 | 10 | addons: 11 | chrome: stable 12 | 13 | cache: 14 | yarn: true 15 | 16 | env: 17 | global: 18 | # See https://git.io/vdao3 for details. 19 | - JOBS=1 20 | matrix: 21 | # we recommend new addons test the current and previous LTS 22 | # as well as latest stable release (bonus points to beta/canary) 23 | - EMBER_TRY_SCENARIO=ember-lts-2.12 24 | - EMBER_TRY_SCENARIO=ember-lts-2.16 25 | - EMBER_TRY_SCENARIO=ember-lts-2.18 26 | - EMBER_TRY_SCENARIO=ember-release 27 | - EMBER_TRY_SCENARIO=ember-beta 28 | - EMBER_TRY_SCENARIO=ember-canary 29 | - EMBER_TRY_SCENARIO=ember-default 30 | 31 | matrix: 32 | fast_finish: true 33 | allow_failures: 34 | - env: EMBER_TRY_SCENARIO=ember-canary 35 | 36 | addons: 37 | code_climate: 38 | repo_token: 3c1dd3ff4af89209029ad977de3771056dcc5279a9df00c4ea697072edb4e7d6 39 | 40 | before_install: 41 | - curl -o- -L https://yarnpkg.com/install.sh | bash 42 | - export PATH=$HOME/.yarn/bin:$PATH 43 | install: 44 | - yarn install --no-lockfile --non-interactive 45 | 46 | script: 47 | - npm run lint:js 48 | # Usually, it's ok to finish the test scenario without reverting 49 | # to the addon's original dependency state, skipping "cleanup". 50 | - 'COVERAGE=true node_modules/.bin/ember try:one $EMBER_TRY_SCENARIO --skip-cleanup' 51 | 52 | after_script: 53 | - codeclimate-test-reporter < coverage/lcov.info 54 | -------------------------------------------------------------------------------- /.watchmanconfig: -------------------------------------------------------------------------------- 1 | { 2 | "ignore_dirs": ["tmp", "dist"] 3 | } 4 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2018 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Ember Changeset CP Validations 2 | 3 | [![Build Status](https://travis-ci.org/offirgolan/ember-changeset-cp-validations.svg)](https://travis-ci.org/offirgolan/ember-changeset-cp-validations) 4 | [![npm version](https://badge.fury.io/js/ember-changeset-cp-validations.svg)](http://badge.fury.io/js/ember-changeset-cp-validations) 5 | [![Code Climate](https://codeclimate.com/github/offirgolan/ember-changeset-cp-validations/badges/gpa.svg)](https://codeclimate.com/github/offirgolan/ember-changeset-cp-validations) 6 | [![Test Coverage](https://codeclimate.com/github/offirgolan/ember-changeset-cp-validations/badges/coverage.svg)](https://codeclimate.com/github/offirgolan/ember-changeset-cp-validations/coverage) 7 | [![Dependency Status](https://david-dm.org/offirgolan/ember-changeset-cp-validations.svg)](https://david-dm.org/offirgolan/ember-changeset-cp-validations) 8 | 9 | Ember CP Validations support for Ember Changeset 10 | 11 | ## Requirements 12 | 13 | - [ember-cp-validations](https://github.com/offirgolan/ember-cp-validations) v3.1.0 or above 14 | - [ember-changeset](https://github.com/poteto/ember-changeset) 15 | 16 | ## Installation 17 | 18 | ``` 19 | ember install ember-changeset-cp-validations 20 | ``` 21 | 22 | ## Helpful Links 23 | 24 | - ### [Changelog](CHANGELOG.md) 25 | 26 | ## Looking for help? 27 | If it is a bug [please open an issue on GitHub](http://github.com/offirgolan/ember-changeset-cp-validations/issues). 28 | 29 | ## Usage 30 | 31 | ### The Template Helper 32 | 33 | This addon updates the `changeset` helper by creating a changeset instance via the [`createChangeset`](#using-createchangeset). 34 | 35 | ```hbs 36 | {{dummy-form 37 | changeset=(changeset user) 38 | submit=(action "submit") 39 | rollback=(action "rollback") 40 | }} 41 | ``` 42 | 43 | Passing a custom action as a second argument to the `changeset` helper is supported but make 44 | sure to call the passed `validate` method to run the necessary validations. 45 | 46 | ```hbs 47 | {{dummy-form 48 | changeset=(changeset user (action "customValidate")) 49 | submit=(action "submit") 50 | rollback=(action "rollback") 51 | }} 52 | ``` 53 | 54 | ```js 55 | import Ember from 'ember'; 56 | 57 | const { Component } = Ember; 58 | 59 | export default Component.extend({ 60 | actions: { 61 | customValidate({ key, newValue, oldValue, changes }, validate) { 62 | // do some custom stuff 63 | 64 | return validate(...arguments); 65 | } 66 | } 67 | }); 68 | ``` 69 | 70 | ### Creating a Changeset 71 | 72 | There are 2 ways to create a changset programmatically. 73 | 74 | #### Using createChangeset 75 | 76 | `createChangeset` is a no fuss way of quickly creating a new changeset instance. 77 | It will return a new changeset instance that is setup for the passed model and its validations. 78 | 79 | ```js 80 | import Ember from 'ember'; 81 | import createChangeset from 'ember-changeset-cp-validations'; 82 | 83 | const { Component } = Ember; 84 | 85 | export default Component.extend({ 86 | init() { 87 | this._super(...arguments); 88 | 89 | let model = get(this, 'model'); 90 | this.changeset = createChangeset(model); 91 | } 92 | }); 93 | ``` 94 | 95 | #### Using buildChangeset 96 | 97 | `buildChangeset` allows you more freedom with creating the changeset instance. 98 | It will return a hash that includes the `validateFn` and `validationMap` required to 99 | support the validations for the passed model. 100 | 101 | ```js 102 | import Ember from 'ember'; 103 | import { buildChangeset } from 'ember-changeset-cp-validations'; 104 | 105 | const { Component } = Ember; 106 | 107 | export default Component.extend({ 108 | init() { 109 | this._super(...arguments); 110 | 111 | let model = get(this, 'model'); 112 | let { validateFn, validationMap } = buildChangeset(model); 113 | this.changeset = new Changeset(model, validateFn, validationMap); 114 | } 115 | }); 116 | ``` 117 | 118 | ### Disable CP Validations Dependents 119 | 120 | When changes get applied to the actual model, validations will be re-triggered since the dependents of the CPs 121 | have changed. As of ember-cp-validations __v3.1.0__, to disable this, you may put all or selective attribute CPs in a 122 | volatile state via the [volatile option](http://offirgolan.github.io/ember-cp-validations/docs/modules/Common%20Options.html#volatile). 123 | 124 | ```js 125 | const Validations = buildValidations({ 126 | username: { 127 | description: 'Username', 128 | validators: [ 129 | validator('presence', true), 130 | validator('length', { 131 | min: 5, 132 | max: 15 133 | }) 134 | ] 135 | }, 136 | 137 | password: { 138 | description: 'Password', 139 | validators: [ 140 | validator('presence', true), 141 | validator('length', { 142 | min: 4, 143 | max: 10, 144 | volatile: false 145 | }) 146 | ] 147 | } 148 | }, { 149 | volatile: true 150 | }); 151 | ``` 152 | -------------------------------------------------------------------------------- /addon/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/offirgolan/ember-changeset-cp-validations/d677d3b16c4aa37d141d55e87b162f4ee1f5758a/addon/.gitkeep -------------------------------------------------------------------------------- /addon/helpers/changeset.js: -------------------------------------------------------------------------------- 1 | import { helper } from '@ember/component/helper'; 2 | import createChangeset from 'ember-changeset-cp-validations'; 3 | 4 | export function changeset([ obj, fn ]) { 5 | return createChangeset(obj, fn); 6 | } 7 | 8 | export default helper(changeset); 9 | -------------------------------------------------------------------------------- /addon/index.js: -------------------------------------------------------------------------------- 1 | import { assert } from '@ember/debug'; 2 | import { typeOf } from '@ember/utils'; 3 | import Changeset from 'ember-changeset'; 4 | 5 | export function buildChangeset(model) { 6 | assert('Object does not contain any validations', typeOf(model.get('validations')) === 'instance'); 7 | 8 | return { 9 | validationMap: model.get('validations.validatableAttributes').reduce((o, attr) => { 10 | o[attr] = true; 11 | return o; 12 | }, {}), 13 | 14 | validateFn: ({ key, newValue }) => { 15 | return model.validateAttribute(key, newValue).then(({ validations }) => { 16 | return validations.get('isValid') ? true : validations.get('message'); 17 | }); 18 | } 19 | }; 20 | } 21 | 22 | export default function createChangeset(model, fn) { 23 | let { validateFn, validationMap } = buildChangeset(model); 24 | let _fn; 25 | 26 | if (fn && typeof fn === 'function') { 27 | _fn = function() { 28 | return fn(...arguments, validateFn); 29 | }; 30 | } 31 | 32 | return new Changeset(model, _fn || validateFn, validationMap); 33 | } 34 | -------------------------------------------------------------------------------- /app/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/offirgolan/ember-changeset-cp-validations/d677d3b16c4aa37d141d55e87b162f4ee1f5758a/app/.gitkeep -------------------------------------------------------------------------------- /app/helpers/changeset.js: -------------------------------------------------------------------------------- 1 | export { default, changeset } from 'ember-changeset-cp-validations/helpers/changeset'; 2 | -------------------------------------------------------------------------------- /config/ember-try.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const getChannelURL = require('ember-source-channel-url'); 4 | 5 | module.exports = function() { 6 | return Promise.all([ 7 | getChannelURL('release'), 8 | getChannelURL('beta'), 9 | getChannelURL('canary'), 10 | ]).then((urls) => { 11 | return { 12 | useYarn: true, 13 | scenarios: [ 14 | { 15 | name: 'ember-lts-2.12', 16 | npm: { 17 | devDependencies: { 18 | 'ember-source': '~2.12.0' 19 | } 20 | } 21 | }, 22 | { 23 | name: 'ember-lts-2.16', 24 | npm: { 25 | devDependencies: { 26 | 'ember-source': '~2.16.0' 27 | } 28 | } 29 | }, 30 | { 31 | name: 'ember-lts-2.18', 32 | npm: { 33 | devDependencies: { 34 | 'ember-source': '~2.18.0' 35 | } 36 | } 37 | }, 38 | { 39 | name: 'ember-release', 40 | npm: { 41 | devDependencies: { 42 | 'ember-source': urls[0] 43 | } 44 | } 45 | }, 46 | { 47 | name: 'ember-beta', 48 | npm: { 49 | devDependencies: { 50 | 'ember-source': urls[1] 51 | } 52 | } 53 | }, 54 | { 55 | name: 'ember-canary', 56 | npm: { 57 | devDependencies: { 58 | 'ember-source': urls[2] 59 | } 60 | } 61 | }, 62 | { 63 | name: 'ember-default', 64 | npm: { 65 | devDependencies: {} 66 | } 67 | } 68 | ] 69 | }; 70 | }); 71 | }; 72 | -------------------------------------------------------------------------------- /config/environment.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = function(/* environment, appConfig */) { 4 | return { }; 5 | }; 6 | -------------------------------------------------------------------------------- /ember-cli-build.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const EmberAddon = require('ember-cli/lib/broccoli/ember-addon'); 4 | 5 | module.exports = function(defaults) { 6 | let app = new EmberAddon(defaults, { 7 | // Add options here 8 | }); 9 | 10 | /* 11 | This build file specifies the options for the dummy test app of this 12 | addon, located in `/tests/dummy` 13 | This build file does *not* influence how the addon or the app using it 14 | behave. You most likely want to be modifying `./index.js` or app's build file 15 | */ 16 | 17 | return app.toTree(); 18 | }; 19 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var VersionChecker = require('ember-cli-version-checker'); 4 | 5 | module.exports = { 6 | name: 'ember-changeset-cp-validations', 7 | 8 | init: function() { 9 | if (this._super.init) { this._super.init.apply(this, arguments); } 10 | 11 | var checker = new VersionChecker(this); 12 | checker.for('ember-cp-validations', 'npm').assertAbove('3.0.1', 'ember-changeset-cp-validations requires ember-cp-validations v3.1.0 and above'); 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ember-changeset-cp-validations", 3 | "version": "0.2.0", 4 | "description": "Ember CP Validations support for Ember Changeset", 5 | "keywords": [ 6 | "ember-addon" 7 | ], 8 | "license": "MIT", 9 | "author": "offirgolan ", 10 | "directories": { 11 | "doc": "doc", 12 | "test": "tests" 13 | }, 14 | "repository": { 15 | "type": "git", 16 | "url": "git@github.com:offirgolan/ember-changeset-cp-validations.git" 17 | }, 18 | "scripts": { 19 | "build": "ember build", 20 | "lint:js": "eslint ./*.js addon addon-test-support app config lib server test-support tests", 21 | "start": "ember serve", 22 | "test": "ember try:each" 23 | }, 24 | "dependencies": { 25 | "ember-changeset": "^1.1.2", 26 | "ember-cli-babel": "^6.6.0", 27 | "ember-cli-version-checker": "^1.1.6" 28 | }, 29 | "devDependencies": { 30 | "broccoli-asset-rev": "^2.4.5", 31 | "ember-cli": "~3.0.0", 32 | "ember-cli-code-coverage": "0.3.8", 33 | "ember-cli-dependency-checker": "^2.0.0", 34 | "ember-cli-eslint": "^4.2.1", 35 | "ember-cli-htmlbars": "^2.0.1", 36 | "ember-cli-htmlbars-inline-precompile": "^1.0.0", 37 | "ember-cli-inject-live-reload": "^1.4.1", 38 | "ember-cli-qunit": "^4.1.1", 39 | "ember-cli-shims": "^1.2.0", 40 | "ember-cli-sri": "^2.1.0", 41 | "ember-cli-uglify": "^2.0.0", 42 | "ember-cp-validations": "^3.5.1", 43 | "ember-disable-prototype-extensions": "^1.1.2", 44 | "ember-export-application-global": "^2.0.0", 45 | "ember-getowner-polyfill": "1.0.1", 46 | "ember-load-initializers": "^1.0.0", 47 | "ember-maybe-import-regenerator": "^0.1.6", 48 | "ember-resolver": "^4.0.0", 49 | "ember-source": "~3.0.0", 50 | "ember-source-channel-url": "^1.0.1", 51 | "ember-try": "^0.2.23", 52 | "eslint-plugin-ember": "^5.0.0", 53 | "eslint-plugin-node": "^5.2.1", 54 | "loader.js": "^4.2.3" 55 | }, 56 | "engines": { 57 | "node": "^4.5 || 6.* || >= 7.*" 58 | }, 59 | "ember-addon": { 60 | "configPath": "tests/dummy/config", 61 | "after": [ 62 | "ember-changeset" 63 | ], 64 | "versionCompatibility": { 65 | "ember": ">1.13.0 <4.0.0" 66 | } 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /testem.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | test_page: 'tests/index.html?hidepassed', 3 | disable_watching: true, 4 | launch_in_ci: [ 5 | 'Chrome' 6 | ], 7 | launch_in_dev: [ 8 | 'Chrome' 9 | ], 10 | browser_args: { 11 | Chrome: { 12 | mode: 'ci', 13 | args: [ 14 | // --no-sandbox is needed when running Chrome inside a container 15 | process.env.TRAVIS ? '--no-sandbox' : null, 16 | 17 | '--disable-gpu', 18 | '--headless', 19 | '--remote-debugging-port=0', 20 | '--window-size=1440,900' 21 | ].filter(Boolean) 22 | } 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /tests/dummy/app/app.js: -------------------------------------------------------------------------------- 1 | import Application from '@ember/application'; 2 | import Resolver from './resolver'; 3 | import loadInitializers from 'ember-load-initializers'; 4 | import config from './config/environment'; 5 | 6 | const App = Application.extend({ 7 | modulePrefix: config.modulePrefix, 8 | podModulePrefix: config.podModulePrefix, 9 | Resolver 10 | }); 11 | 12 | loadInitializers(App, config.modulePrefix); 13 | 14 | export default App; 15 | -------------------------------------------------------------------------------- /tests/dummy/app/components/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/offirgolan/ember-changeset-cp-validations/d677d3b16c4aa37d141d55e87b162f4ee1f5758a/tests/dummy/app/components/.gitkeep -------------------------------------------------------------------------------- /tests/dummy/app/components/user-form.js: -------------------------------------------------------------------------------- 1 | import Component from '@ember/component'; 2 | 3 | export default Component.extend({ 4 | 5 | }); 6 | -------------------------------------------------------------------------------- /tests/dummy/app/controllers/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/offirgolan/ember-changeset-cp-validations/d677d3b16c4aa37d141d55e87b162f4ee1f5758a/tests/dummy/app/controllers/.gitkeep -------------------------------------------------------------------------------- /tests/dummy/app/controllers/index.js: -------------------------------------------------------------------------------- 1 | import Controller from '@ember/controller'; 2 | 3 | export default Controller.extend({ 4 | actions: { 5 | submit(changeset) { 6 | return changeset.save(); 7 | }, 8 | 9 | rollback(changeset) { 10 | return changeset.rollback(); 11 | } 12 | } 13 | }); 14 | -------------------------------------------------------------------------------- /tests/dummy/app/helpers/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/offirgolan/ember-changeset-cp-validations/d677d3b16c4aa37d141d55e87b162f4ee1f5758a/tests/dummy/app/helpers/.gitkeep -------------------------------------------------------------------------------- /tests/dummy/app/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Dummy 7 | 8 | 9 | 10 | {{content-for "head"}} 11 | 12 | 13 | 14 | 15 | {{content-for "head-footer"}} 16 | 17 | 18 | {{content-for "body"}} 19 | 20 | 21 | 22 | 23 | {{content-for "body-footer"}} 24 | 25 | 26 | -------------------------------------------------------------------------------- /tests/dummy/app/models/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/offirgolan/ember-changeset-cp-validations/d677d3b16c4aa37d141d55e87b162f4ee1f5758a/tests/dummy/app/models/.gitkeep -------------------------------------------------------------------------------- /tests/dummy/app/models/user.js: -------------------------------------------------------------------------------- 1 | import { resolve } from 'rsvp'; 2 | import DS from 'ember-data'; 3 | import { validator, buildValidations } from 'ember-cp-validations'; 4 | 5 | const { attr } = DS; 6 | 7 | const Validations = buildValidations({ 8 | username: { 9 | description: 'Username', 10 | validators: [ 11 | validator('presence', true), 12 | validator('length', { 13 | min: 5, 14 | max: 15 15 | }) 16 | ] 17 | }, 18 | password: { 19 | description: 'Password', 20 | validators: [ 21 | validator('presence', true), 22 | validator('length', { 23 | min: 4, 24 | max: 10 25 | }), 26 | validator('format', { 27 | regex: /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{4,10}$/, 28 | message: '{description} must include at least one upper case letter, one lower case letter, and a number' 29 | }), 30 | validator('length', { 31 | isWarning: true, 32 | min: 6, 33 | message: 'What kind of weak password is that?' 34 | }) 35 | ] 36 | }, 37 | email: { 38 | validators: [ 39 | validator('presence', true), 40 | validator('format', { 41 | type: 'email' 42 | }) 43 | ] 44 | }, 45 | emailConfirmation: validator('confirmation', { 46 | on: 'email', 47 | message: 'Email addresses do not match' 48 | }) 49 | }, { 50 | debounce: 0, 51 | volatile: true 52 | }); 53 | 54 | export default DS.Model.extend(Validations, { 55 | username: attr('string'), 56 | password: attr('string'), 57 | email: attr('string'), 58 | 59 | save() { 60 | return resolve(this); 61 | } 62 | }); 63 | -------------------------------------------------------------------------------- /tests/dummy/app/resolver.js: -------------------------------------------------------------------------------- 1 | import Resolver from 'ember-resolver'; 2 | 3 | export default Resolver; 4 | -------------------------------------------------------------------------------- /tests/dummy/app/router.js: -------------------------------------------------------------------------------- 1 | import EmberRouter from '@ember/routing/router'; 2 | import config from './config/environment'; 3 | 4 | const Router = EmberRouter.extend({ 5 | location: config.locationType, 6 | rootURL: config.rootURL 7 | }); 8 | 9 | Router.map(function() { 10 | }); 11 | 12 | export default Router; 13 | -------------------------------------------------------------------------------- /tests/dummy/app/routes/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/offirgolan/ember-changeset-cp-validations/d677d3b16c4aa37d141d55e87b162f4ee1f5758a/tests/dummy/app/routes/.gitkeep -------------------------------------------------------------------------------- /tests/dummy/app/routes/index.js: -------------------------------------------------------------------------------- 1 | import Route from '@ember/routing/route'; 2 | 3 | export default Route.extend({ 4 | model() { 5 | return this.store.createRecord('user'); 6 | } 7 | }); 8 | -------------------------------------------------------------------------------- /tests/dummy/app/styles/app.css: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/offirgolan/ember-changeset-cp-validations/d677d3b16c4aa37d141d55e87b162f4ee1f5758a/tests/dummy/app/styles/app.css -------------------------------------------------------------------------------- /tests/dummy/app/templates/application.hbs: -------------------------------------------------------------------------------- 1 |

Welcome to Ember

2 | 3 | {{outlet}} -------------------------------------------------------------------------------- /tests/dummy/app/templates/components/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/offirgolan/ember-changeset-cp-validations/d677d3b16c4aa37d141d55e87b162f4ee1f5758a/tests/dummy/app/templates/components/.gitkeep -------------------------------------------------------------------------------- /tests/dummy/app/templates/components/user-form.hbs: -------------------------------------------------------------------------------- 1 | {{#with (changeset model) as |changeset|}} 2 |
3 | {{input value=changeset.username placeholder="Username"}} 4 | {{input value=changeset.password placeholder="Password"}} 5 | {{input value=changeset.email placeholder="Email"}} 6 | 7 |

{{v-get model 'username' 'message'}}

8 |

{{changeset.error.password.validation}}

9 | 10 | 11 | 12 |
13 | {{/with}} 14 | -------------------------------------------------------------------------------- /tests/dummy/app/templates/index.hbs: -------------------------------------------------------------------------------- 1 | {{user-form 2 | model=model 3 | submit=(action "submit") 4 | rollback=(action "rollback")}} 5 | -------------------------------------------------------------------------------- /tests/dummy/config/environment.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = function(environment) { 4 | let ENV = { 5 | modulePrefix: 'dummy', 6 | environment, 7 | rootURL: '/', 8 | locationType: 'auto', 9 | EmberENV: { 10 | FEATURES: { 11 | // Here you can enable experimental features on an ember canary build 12 | // e.g. 'with-controller': true 13 | }, 14 | EXTEND_PROTOTYPES: { 15 | // Prevent Ember Data from overriding Date.parse. 16 | Date: false 17 | } 18 | }, 19 | 20 | APP: { 21 | // Here you can pass flags/options to your application instance 22 | // when it is created 23 | } 24 | }; 25 | 26 | if (environment === 'development') { 27 | // ENV.APP.LOG_RESOLVER = true; 28 | // ENV.APP.LOG_ACTIVE_GENERATION = true; 29 | // ENV.APP.LOG_TRANSITIONS = true; 30 | // ENV.APP.LOG_TRANSITIONS_INTERNAL = true; 31 | // ENV.APP.LOG_VIEW_LOOKUPS = true; 32 | ENV.EmberENV.RAISE_ON_DEPRECATION = true; 33 | } 34 | 35 | if (environment === 'test') { 36 | // Testem prefers this... 37 | ENV.locationType = 'none'; 38 | 39 | // keep test console output quieter 40 | ENV.APP.LOG_ACTIVE_GENERATION = false; 41 | ENV.APP.LOG_VIEW_LOOKUPS = false; 42 | 43 | // Deprecations should be treated as errors 44 | ENV.EmberENV.RAISE_ON_DEPRECATION = !process.env['ALLOW_DEPRECATIONS']; 45 | 46 | ENV.APP.rootElement = '#ember-testing'; 47 | ENV.APP.autoboot = false; 48 | } 49 | 50 | if (environment === 'production') { 51 | // here you can enable a production-specific feature 52 | } 53 | 54 | return ENV; 55 | }; 56 | -------------------------------------------------------------------------------- /tests/dummy/config/targets.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const browsers = [ 4 | 'last 1 Chrome versions', 5 | 'last 1 Firefox versions', 6 | 'last 1 Safari versions' 7 | ]; 8 | 9 | const isCI = !!process.env.CI; 10 | const isProduction = process.env.EMBER_ENV === 'production'; 11 | 12 | if (isCI || isProduction) { 13 | browsers.push('ie 11'); 14 | } 15 | 16 | module.exports = { 17 | browsers 18 | }; 19 | -------------------------------------------------------------------------------- /tests/dummy/public/robots.txt: -------------------------------------------------------------------------------- 1 | # http://www.robotstxt.org 2 | User-agent: * 3 | Disallow: 4 | -------------------------------------------------------------------------------- /tests/helpers/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/offirgolan/ember-changeset-cp-validations/d677d3b16c4aa37d141d55e87b162f4ee1f5758a/tests/helpers/.gitkeep -------------------------------------------------------------------------------- /tests/helpers/module-for-acceptance.js: -------------------------------------------------------------------------------- 1 | import { Promise } from 'rsvp'; 2 | import { module } from 'qunit'; 3 | import startApp from '../helpers/start-app'; 4 | import destroyApp from '../helpers/destroy-app'; 5 | 6 | export default function(name, options = {}) { 7 | module(name, { 8 | beforeEach() { 9 | this.application = startApp(); 10 | 11 | if (options.beforeEach) { 12 | return options.beforeEach.call(this, ...arguments); 13 | } 14 | }, 15 | 16 | afterEach() { 17 | let afterEach = options.afterEach && options.afterEach.call(this, ...arguments); 18 | return Promise.resolve(afterEach).then(() => destroyApp(this.application)); 19 | } 20 | }); 21 | } 22 | -------------------------------------------------------------------------------- /tests/helpers/validated-object.js: -------------------------------------------------------------------------------- 1 | import EmberObject from '@ember/object'; 2 | import { validator, buildValidations } from 'ember-cp-validations'; 3 | 4 | export const Validations = buildValidations({ 5 | username: { 6 | description: 'Username', 7 | validators: [ 8 | validator('presence', true), 9 | validator('length', { 10 | min: 5, 11 | max: 15 12 | }) 13 | ] 14 | }, 15 | password: { 16 | description: 'Password', 17 | validators: [ 18 | validator('presence', true), 19 | validator('length', { 20 | min: 4, 21 | max: 10 22 | }) 23 | ] 24 | } 25 | }); 26 | 27 | export default EmberObject.extend(Validations); 28 | -------------------------------------------------------------------------------- /tests/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Dummy Tests 7 | 8 | 9 | 10 | {{content-for "head"}} 11 | {{content-for "test-head"}} 12 | 13 | 14 | 15 | 16 | 17 | {{content-for "head-footer"}} 18 | {{content-for "test-head-footer"}} 19 | 20 | 21 | {{content-for "body"}} 22 | {{content-for "test-body"}} 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | {{content-for "body-footer"}} 31 | {{content-for "test-body-footer"}} 32 | 33 | 34 | -------------------------------------------------------------------------------- /tests/integration/helpers/changeset-test.js: -------------------------------------------------------------------------------- 1 | import EmberObject from '@ember/object'; 2 | import { changeset } from 'dummy/helpers/changeset'; 3 | import { module, test } from 'qunit'; 4 | import ValidatedObject from '../../helpers/validated-object'; 5 | import { setupRenderingTest } from 'ember-qunit'; 6 | 7 | let model; 8 | 9 | module('Integration | Helper | changeset', function(hooks) { 10 | setupRenderingTest(hooks); 11 | 12 | hooks.beforeEach(function() { 13 | model = ValidatedObject.extend(this.owner.ownerInjection()).create(); 14 | }); 15 | 16 | test('it works', function(assert) { 17 | let cs = changeset([ model ]); 18 | assert.ok(cs && cs instanceof EmberObject); 19 | }); 20 | 21 | test('it respects custom function', function(assert) { 22 | assert.expect(1); 23 | 24 | let fn = () => { 25 | assert.ok(true); 26 | return true; 27 | }; 28 | let cs = changeset([ model, fn ]); 29 | 30 | cs.set('foo', 'bar'); 31 | }); 32 | 33 | test('it validates', function(assert) { 34 | let done = assert.async(); 35 | let cs = changeset([ model ]); 36 | 37 | cs.validate().then(() => { 38 | assert.equal(cs.get('error.username.validation'), 'Username can\'t be blank'); 39 | assert.equal(cs.get('error.password.validation'), 'Password can\'t be blank'); 40 | done(); 41 | }); 42 | }); 43 | }); 44 | -------------------------------------------------------------------------------- /tests/integration/index-test.js: -------------------------------------------------------------------------------- 1 | import { 2 | default as createChangeset, 3 | buildChangeset 4 | } from 'ember-changeset-cp-validations'; 5 | import { module, test } from 'qunit'; 6 | import ValidatedObject from '../helpers/validated-object'; 7 | import { setupRenderingTest } from 'ember-qunit'; 8 | 9 | let model; 10 | 11 | module('Integration | Index', function(hooks) { 12 | setupRenderingTest(hooks); 13 | 14 | hooks.beforeEach(function() { 15 | model = ValidatedObject.extend(this.owner.ownerInjection()).create(); 16 | }); 17 | 18 | test('buildChangeset works', function(assert) { 19 | let { validateFn, validationMap } = buildChangeset(model); 20 | 21 | assert.equal(typeof validateFn, 'function'); 22 | assert.equal(typeof validationMap, 'object'); 23 | }); 24 | 25 | test('buildChangeset correctly generates the validation map', function(assert) { 26 | let { validationMap } = buildChangeset(model); 27 | 28 | assert.deepEqual(validationMap, { username: true, password: true }); 29 | }); 30 | 31 | test('buildChangeset correctly generates the validate fn', function(assert) { 32 | let done = assert.async(); 33 | let { validateFn } = buildChangeset(model); 34 | 35 | validateFn({ key: 'username', newValue: 'og' }).then((message) => { 36 | assert.equal(message, 'Username is too short (minimum is 5 characters)'); 37 | done(); 38 | }); 39 | }); 40 | 41 | test('createChangeset correctly wraps a passed in fn', function(assert) { 42 | assert.expect(1); 43 | 44 | let fn = (o, validate) => assert.equal(typeof validate, 'function'); 45 | let cs = createChangeset(model, fn); 46 | 47 | cs.set('username', 'Offir'); 48 | }); 49 | }); 50 | -------------------------------------------------------------------------------- /tests/test-helper.js: -------------------------------------------------------------------------------- 1 | import Application from '../app'; 2 | import config from '../config/environment'; 3 | import { setApplication } from '@ember/test-helpers'; 4 | import { start } from 'ember-qunit'; 5 | 6 | setApplication(Application.create(config.APP)); 7 | 8 | start(); 9 | -------------------------------------------------------------------------------- /tests/unit/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/offirgolan/ember-changeset-cp-validations/d677d3b16c4aa37d141d55e87b162f4ee1f5758a/tests/unit/.gitkeep -------------------------------------------------------------------------------- /vendor/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/offirgolan/ember-changeset-cp-validations/d677d3b16c4aa37d141d55e87b162f4ee1f5758a/vendor/.gitkeep --------------------------------------------------------------------------------