diff --git a/.jscsrc b/.jscsrc new file mode 100644 index 00000000..ae490f5f --- /dev/null +++ b/.jscsrc @@ -0,0 +1,5 @@ +{ + "preset": "ember-suave", + "disallowDirectPropertyAccess": false, + "requireCommentsToIncludeAccess": false +} diff --git a/.vscodeignore b/.vscodeignore new file mode 100644 index 00000000..5f5fbfe7 --- /dev/null +++ b/.vscodeignore @@ -0,0 +1,10 @@ +dist/** +tmp/** +build/** +cache/** +node_modules/** +bower_components/** +.sass-cache/** +connect.lock/** +coverage/*/** +libpeerconnection.log \ No newline at end of file diff --git a/addon/-private/ember-validator.js b/addon/-private/ember-validator.js new file mode 100644 index 00000000..8c5c54c1 --- /dev/null +++ b/addon/-private/ember-validator.js @@ -0,0 +1,14 @@ +import Base from 'ember-cp-validations/validators/base'; +import { validate as _validate } from 'ember-validators'; + +export default Base.extend({ + validate() { + let result = _validate(this.get('_type'), ...arguments); + + if (result && typeof result === 'object') { + return this.createErrorMessage(result.type, result.value, result.context); + } + + return result; + } +}); diff --git a/addon/validations/internal-result-object.js b/addon/-private/internal-result-object.js similarity index 72% rename from addon/validations/internal-result-object.js rename to addon/-private/internal-result-object.js index ea8ebac3..e10036b0 100644 --- a/addon/validations/internal-result-object.js +++ b/addon/-private/internal-result-object.js @@ -4,7 +4,7 @@ */ import Ember from 'ember'; -import ValidationError from './error'; +import ValidationError from '../validations/error'; import { isDsModel, isPromise } from '../utils/utils'; const { @@ -13,30 +13,31 @@ const { isNone, computed, canInvoke, - makeArray, + makeArray } = Ember; const { and, - not + not, + readOnly } = computed; export default Ember.Object.extend({ model: null, isValid: true, isValidating: false, - type: computed.readOnly('_validator._type'), message: null, attribute: '', attrValue: null, _promise: null, _validator: null, + _type: readOnly('_validator._type'), init() { this._super(...arguments); - if(this.get('isAsync')) { + if (this.get('isAsync')) { this._handlePromise(); } }, @@ -45,21 +46,21 @@ export default Ember.Object.extend({ isNotValidating: not('isValidating'), isTruelyValid: and('isNotValidating', 'isValid'), - isAsync: computed('_promise', function () { + isAsync: computed('_promise', function() { return isPromise(get(this, '_promise')); }), - isDirty: computed('attrValue', function () { - const model = get(this, 'model'); - const attribute = get(this, 'attribute'); - const attrValue = get(this, 'attrValue'); + isDirty: computed('attrValue', function() { + let model = get(this, 'model'); + let attribute = get(this, 'attribute'); + let attrValue = get(this, 'attrValue'); // Check default model values if (isDsModel(model) && canInvoke(model, 'eachAttribute')) { - const attrMeta = model.get('constructor.attributes').get(attribute); + let attrMeta = model.get('constructor.attributes').get(attribute); if (attrMeta) { - const defaultValue = attrMeta.options.defaultValue; + let { defaultValue } = attrMeta.options; if (!isNone(defaultValue)) { return defaultValue !== attrValue; @@ -69,14 +70,14 @@ export default Ember.Object.extend({ return !isNone(attrValue); }), - messages: computed('message', function () { + messages: computed('message', function() { return makeArray(get(this, 'message')); }), - error: computed('isInvalid', 'type', 'message', 'attribute', function () { + error: computed('isInvalid', 'type', 'message', 'attribute', function() { if (get(this, 'isInvalid')) { return ValidationError.create({ - type: get(this, 'type'), + type: get(this, '_type'), message: get(this, 'message'), attribute: get(this, 'attribute') }); @@ -85,7 +86,7 @@ export default Ember.Object.extend({ return null; }), - errors: computed('error', function () { + errors: computed('error', function() { return makeArray(get(this, 'error')); }), diff --git a/addon/validations/result.js b/addon/-private/result.js similarity index 88% rename from addon/validations/result.js rename to addon/-private/result.js index 0a0155bd..c155561e 100644 --- a/addon/validations/result.js +++ b/addon/-private/result.js @@ -4,7 +4,7 @@ */ import Ember from 'ember'; -import ValidationResultCollection from './result-collection'; +import ResultCollection from '../validations/result-collection'; import InternalResultObject from './internal-result-object'; const { @@ -14,7 +14,7 @@ const { isArray, computed, setProperties, - getProperties, + getProperties } = Ember; const { @@ -70,8 +70,8 @@ const Result = Ember.Object.extend({ * @type {Boolean} */ _isReadOnly: computed('_validations', function() { - const validations = get(this, '_validations'); - return (validations instanceof ValidationResultCollection) || get(validations, 'isValidations'); + let validations = get(this, '_validations'); + return (validations instanceof ResultCollection) || get(validations, 'isValidations'); }).readOnly(), /** @@ -157,7 +157,7 @@ const Result = Ember.Object.extend({ * @private * @type {Result} */ - _validations: computed('model', 'attribute', '_promise', '_validator', function () { + _validations: computed('model', 'attribute', '_promise', '_validator', function() { return InternalResultObject.extend({ attrValue: computed.readOnly(`model.${get(this, 'attribute')}`) }).create(getProperties(this, ['model', 'attribute', '_promise', '_validator'])); @@ -185,9 +185,9 @@ const Result = Ember.Object.extend({ * @param result */ update(result) { - const validations = get(this, '_validations'); - const validator = get(this, '_validator'); - const { model, attribute } = getProperties(this, ['model', 'attribute']); + let validations = get(this, '_validations'); + let validator = get(this, '_validator'); + let { model, attribute } = getProperties(this, ['model', 'attribute']); if (isNone(result)) { this.update(false); @@ -197,9 +197,9 @@ const Result = Ember.Object.extend({ if (get(result, 'isValidations')) { set(this, '_validations', result); } else if (isArray(result)) { - const validationResultsCollection = ValidationResultCollection.create({ + let validationResultsCollection = ResultCollection.create({ attribute, - content: result.map(r => Result.create({ + content: result.map((r) => Result.create({ attribute, model, _validator: validator, @@ -228,9 +228,9 @@ const Result = Ember.Object.extend({ */ _handlePromise() { get(this, '_promise').then( - result => this.update(result), - result => this.update(result) - ).catch(reason => { + (result) => this.update(result), + (result) => this.update(result) + ).catch((reason) => { // TODO: send into error state throw reason; }); diff --git a/addon/utils/assign.js b/addon/utils/assign.js index 4cafbd06..beddbd50 100644 --- a/addon/utils/assign.js +++ b/addon/utils/assign.js @@ -18,13 +18,13 @@ const { } = Ember; export default function assign(obj, path, value, useEmberObject = false, delimiter = '.') { - const keyPath = path.split(delimiter); - const lastKeyIndex = keyPath.length - 1; + let keyPath = path.split(delimiter); + let lastKeyIndex = keyPath.length - 1; let currObj = obj; // Iterate over each key in the path (minus the last one which is the property to be assigned) for (let i = 0; i < lastKeyIndex; ++i) { - const key = keyPath[i]; + let key = keyPath[i]; // Create a new object if it doesnt exist if (isNone(get(currObj, key))) { diff --git a/addon/utils/cycle-breaker.js b/addon/utils/cycle-breaker.js index 2a64ab95..ac00040b 100644 --- a/addon/utils/cycle-breaker.js +++ b/addon/utils/cycle-breaker.js @@ -13,9 +13,9 @@ import MetaData from './meta-data'; export default function cycleBreaker(fn, value) { - const key = MetaData.symbol('cycle'); + let key = MetaData.symbol('cycle'); - return function () { + return function() { if (MetaData.getData(this, key)) { return value; } diff --git a/addon/utils/flatten.js b/addon/utils/flatten.js index 2c7bad87..b9a1a240 100644 --- a/addon/utils/flatten.js +++ b/addon/utils/flatten.js @@ -7,7 +7,7 @@ export default function flatten(array = []) { let result = []; for (let i = 0, l = array.length; i < l; i++) { - const item = array[i]; + let item = array[i]; if (Array.isArray(item)) { result = result.concat(flatten(item)); diff --git a/addon/utils/meta-data.js b/addon/utils/meta-data.js index b6d9144b..a7dfda8f 100644 --- a/addon/utils/meta-data.js +++ b/addon/utils/meta-data.js @@ -13,8 +13,8 @@ function symbol(key) { } function getData(obj, s) { - const m = Ember.meta(obj); - const data = m[dataKey]; + let m = Ember.meta(obj); + let data = m[dataKey]; if (data) { return data[s]; @@ -22,8 +22,8 @@ function getData(obj, s) { } function setData(obj, s, value) { - const m = Ember.meta(obj); - const data = m[dataKey] = m[dataKey] || {}; + let m = Ember.meta(obj); + let data = m[dataKey] = m[dataKey] || {}; data[s] = value; } diff --git a/addon/utils/utils.js b/addon/utils/utils.js index 604d3ca1..3aa7f5e7 100644 --- a/addon/utils/utils.js +++ b/addon/utils/utils.js @@ -5,6 +5,7 @@ import Ember from 'ember'; import isHTMLSafe from 'ember-string-ishtmlsafe-polyfill'; +import _requireModule from 'ember-validators/utils/require-module'; const DS = requireModule('ember-data'); @@ -15,15 +16,8 @@ const { A: emberArray } = Ember; -export function requireModule(module) { - const rjs = self.requirejs; - - if ( - (rjs.has && rjs.has(module)) || - (!rjs.has && (rjs.entries[module] || rjs.entries[module + '/index'])) - ) { - return self.require(module).default; - } +export function requireModule() { + return _requireModule(...arguments); } export function unwrapString(s) { @@ -59,17 +53,17 @@ export function isEmberObject(o) { } export function isValidatable(value) { - const v = unwrapProxy(value); + let v = unwrapProxy(value); return isDsModel(v) ? !get(v, 'isDeleted') : true; } export function getValidatableValue(value) { - if(!value) { + if (!value) { return value; } - if(isDSManyArray(value)) { - return emberArray(value.filter(v => isValidatable(v))); + if (isDSManyArray(value)) { + return emberArray(value.filter((v) => isValidatable(v))); } return isValidatable(value) ? value : undefined; diff --git a/addon/validations/factory.js b/addon/validations/factory.js index c804e0dc..adea045d 100644 --- a/addon/validations/factory.js +++ b/addon/validations/factory.js @@ -7,8 +7,8 @@ import Ember from 'ember'; import getOwner from 'ember-getowner-polyfill'; import flatten from '../utils/flatten'; import assign from '../utils/assign'; -import ValidationResult from './result'; -import ValidationResultCollection from './result-collection'; +import ValidationResult from '../-private/result'; +import ResultCollection from './result-collection'; import BaseValidator from '../validators/base'; import cycleBreaker from '../utils/cycle-breaker'; import shouldCallSuper from '../utils/should-call-super'; @@ -101,7 +101,7 @@ function buildValidations(validations = {}, globalOptions = {}) { let Validations, validationMixinCount; - const ValidationsMixin = Ember.Mixin.create({ + let ValidationsMixin = Ember.Mixin.create({ init() { this._super(...arguments); @@ -109,10 +109,11 @@ function buildValidations(validations = {}, globalOptions = {}) { this.__validationsMixinCount__ = this.__validationsMixinCount__ || 0; validationMixinCount = ++this.__validationsMixinCount__; }, - __validationsClass__: computed(function () { + __validationsClass__: computed(function() { if (!Validations) { let inheritedClass; - if(shouldCallSuper(this, '__validationsClass__') || validationMixinCount > 1) { + + if (shouldCallSuper(this, '__validationsClass__') || validationMixinCount > 1) { inheritedClass = this._super(); } @@ -120,10 +121,8 @@ function buildValidations(validations = {}, globalOptions = {}) { } return Validations; }).readOnly(), - validations: computed(function () { - return this.get('__validationsClass__').create({ - model: this - }); + validations: computed(function() { + return this.get('__validationsClass__').create({ model: this }); }).readOnly(), validate() { return get(this, 'validations').validate(...arguments); @@ -161,28 +160,27 @@ function buildValidations(validations = {}, globalOptions = {}) { * @return */ function normalizeOptions(validations = {}, globalOptions = {}) { - const validatableAttrs = Object.keys(validations); + let validatableAttrs = Object.keys(validations); - validatableAttrs.forEach(attribute => { - const rules = validations[attribute]; + validatableAttrs.forEach((attribute) => { + let rules = validations[attribute]; if (rules && typeof rules === 'object' && isArray(rules.validators)) { - const options = Object.keys(rules).reduce((o, k) => { + let options = Object.keys(rules).reduce((o, k) => { if (k !== 'validators') { o[k] = rules[k]; } return o; }, {}); - const validators = rules.validators; - - validators.forEach(v => { + let { validators } = rules; + validators.forEach((v) => { v.defaultOptions = options; }); validations[attribute] = validators; } validations[attribute] = makeArray(validations[attribute]); - validations[attribute].forEach(v => { + validations[attribute].forEach((v) => { v.globalOptions = globalOptions; }); }); @@ -208,7 +206,7 @@ function createValidationsClass(inheritedValidationsClass, validations, model) { // Setup validation inheritance if (inheritedValidationsClass && inheritedValidationsClass.__isCPValidationsClass__) { - const inheritedValidations = inheritedValidationsClass.create(); + let inheritedValidations = inheritedValidationsClass.create(); validationRules = merge(validationRules, inheritedValidations.get('_validationRules')); validatableAttributes = emberArray(inheritedValidations.get('validatableAttributes').concat(validatableAttributes)).uniq(); @@ -221,18 +219,18 @@ function createValidationsClass(inheritedValidationsClass, validations, model) { }, validationRules); // Create the mixin that holds all the top level validation props (isValid, messages, etc) - const TopLevelProps = createTopLevelPropsMixin(validatableAttributes); + let TopLevelProps = createTopLevelPropsMixin(validatableAttributes); // Create the `attrs` class which will add the current model reference once instantiated - const AttrsClass = createAttrsClass(validatableAttributes, validationRules, model); + let AttrsClass = createAttrsClass(validatableAttributes, validationRules, model); // Create `validations` class - const ValidationsClass = Ember.Object.extend(TopLevelProps, { + let ValidationsClass = Ember.Object.extend(TopLevelProps, { model: null, attrs: null, isValidations: true, - validatableAttributes: computed(function () { + validatableAttributes: computed(function() { return validatableAttributes; }).readOnly(), @@ -241,7 +239,7 @@ function createValidationsClass(inheritedValidationsClass, validations, model) { _debouncedValidations: null, // Private - _validationRules: computed(function () { + _validationRules: computed(function() { return validationRules; }).readOnly(), @@ -262,19 +260,19 @@ function createValidationsClass(inheritedValidationsClass, validations, model) { destroy() { this._super(...arguments); - const validatableAttrs = get(this, 'validatableAttributes'); - const debouncedValidations = get(this, '_debouncedValidations'); + let validatableAttrs = get(this, 'validatableAttributes'); + let debouncedValidations = get(this, '_debouncedValidations'); // Initiate attrs destroy to cleanup any remaining model references this.get('attrs').destroy(); // Cancel all debounced timers - validatableAttrs.forEach(attr => { - const attrCache = get(debouncedValidations, attr); + validatableAttrs.forEach((attr) => { + let attrCache = get(debouncedValidations, attr); if (!isNone(attrCache)) { // Itterate over each attribute and cancel all of its debounced validations - Object.keys(attrCache).forEach(v => run.cancel(attrCache[v])); + Object.keys(attrCache).forEach((v) => run.cancel(attrCache[v])); } }); } @@ -303,36 +301,38 @@ function createValidationsClass(inheritedValidationsClass, validations, model) { * @return {Ember.Object} */ function createAttrsClass(validatableAttributes, validationRules, model) { - const nestedClasses = {}; - const rootPath = 'root'; + let nestedClasses = {}; + let rootPath = 'root'; - const AttrsClass = Ember.Object.extend({ + let AttrsClass = Ember.Object.extend({ __path__: rootPath, init() { this._super(...arguments); - const _model = this.get('_model'); - const path = this.get('__path__'); + let _model = this.get('_model'); + let path = this.get('__path__'); /* Instantiate the nested attrs classes for the current path */ - Object.keys(nestedClasses[path] || []).forEach(key => { - set(this, key, nestedClasses[path][key].create({ _model })); + Object.keys(nestedClasses[path] || []).forEach((key) => { + set(this, key, nestedClasses[path][key].create({ + _model + })); }); }, destroy() { this._super(...arguments); - const path = this.get('__path__'); + let path = this.get('__path__'); /* Remove the model reference from each nested class and destroy it */ - Object.keys(nestedClasses[path] || []).forEach(key => { - const o = get(this, key); + Object.keys(nestedClasses[path] || []).forEach((key) => { + let o = get(this, key); o.set('_model', null); o.destroy(); }); @@ -342,7 +342,7 @@ function createAttrsClass(validatableAttributes, validationRules, model) { /* Insert CPs + Create nested classes */ - validatableAttributes.forEach(attribute => { + validatableAttributes.forEach((attribute) => { let path = attribute.split('.'); let attr = path.pop(); let currPath = [rootPath]; @@ -350,9 +350,9 @@ function createAttrsClass(validatableAttributes, validationRules, model) { let cpHash = {}; // Iterate over the path and create the necessary nested classes along the way - for(let i = 0; i < path.length; i++) { - const key = path[i]; - const currPathStr = currPath.join('.'); + for (let i = 0; i < path.length; i++) { + let key = path[i]; + let currPathStr = currPath.join('.'); let _nestedClasses; nestedClasses[currPathStr] = nestedClasses[currPathStr] || {}; @@ -360,8 +360,10 @@ function createAttrsClass(validatableAttributes, validationRules, model) { currPath.push(key); - if(!_nestedClasses[key]) { - _nestedClasses[key] = AttrsClass.extend({ __path__: currPath.join('.') }); + if (!_nestedClasses[key]) { + _nestedClasses[key] = AttrsClass.extend({ + __path__: currPath.join('.') + }); } currClass = _nestedClasses[key]; @@ -384,23 +386,23 @@ function createAttrsClass(validatableAttributes, validationRules, model) { * @param {Object} model Since the CPs are created once per class on the first initialization, * this is the first model that was instantiated * @param {Array} validations - * @return {Ember.ComputedProperty} A computed property which is a ValidationResultCollection + * @return {Ember.ComputedProperty} A computed property which is a ResultCollection */ function createCPValidationFor(attribute, model, validations) { - const dependentKeys = getCPDependentKeysFor(attribute, model, validations); + let dependentKeys = getCPDependentKeysFor(attribute, model, validations); - return computed(...dependentKeys, cycleBreaker(function () { - const model = get(this, '_model'); - const validators = !isNone(model) ? getValidatorsFor(attribute, model) : []; + return computed(...dependentKeys, cycleBreaker(function() { + let model = get(this, '_model'); + let validators = !isNone(model) ? getValidatorsFor(attribute, model) : []; - const validationResults = generateValidationResultsFor(attribute, model, validators, (validator, options) => { - const debounce = getWithDefault(options, 'debounce', 0); + let validationResults = generateValidationResultsFor(attribute, model, validators, (validator, options) => { + let debounce = getWithDefault(options, 'debounce', 0); if (debounce > 0) { - const cache = getDebouncedValidationsCacheFor(attribute, model); + let cache = getDebouncedValidationsCacheFor(attribute, model); // Return a promise and pass the resolve method to the debounce handler - return new Promise(resolve => { + return new Promise((resolve) => { cache[guidFor(validator)] = run.debounce(validator, debouncedValidate, validator, model, attribute, resolve, debounce, false); }); } else { @@ -408,7 +410,10 @@ function createCPValidationFor(attribute, model, validations) { } }); - return ValidationResultCollection.create({ attribute, content: validationResults }); + return ResultCollection.create({ + attribute, + content: validationResults + }); })).readOnly(); } @@ -430,13 +435,13 @@ function generateValidationResultsFor(attribute, model, validators, validate) { let isInvalid = false; let value, result; - return validators.map(validator => { - const options = get(validator, 'options').copy(); - const isWarning = getWithDefault(options, 'isWarning', false); - const disabled = getWithDefault(options, 'disabled', false); - const lazy = getWithDefault(options, 'lazy', true); + return validators.map((validator) => { + let options = get(validator, 'options').copy(); + let isWarning = getWithDefault(options, 'isWarning', false); + let disabled = getWithDefault(options, 'disabled', false); + let lazy = getWithDefault(options, 'lazy', true); - if(disabled || (lazy && isInvalid) || !isModelValidatable) { + if (disabled || (lazy && isInvalid) || !isModelValidatable) { value = true; } else { value = validate(validator, options, model, attribute); @@ -448,7 +453,7 @@ function generateValidationResultsFor(attribute, model, validators, validate) { If the current result is invalid, the rest of the validations do not need to be triggered (if lazy) since the attribute is already in an invalid state. */ - if(!isInvalid && !isWarning && get(result, 'isInvalid')) { + if (!isInvalid && !isWarning && get(result, 'isInvalid')) { isInvalid = true; } @@ -466,7 +471,7 @@ function generateValidationResultsFor(attribute, model, validators, validate) { */ function createTopLevelPropsMixin(validatableAttrs) { // Expose the following properties as public APIs via readOnly aliases - const aliases = [ + let aliases = [ 'isWarning', 'isValid', 'isValidating', @@ -486,7 +491,7 @@ function createTopLevelPropsMixin(validatableAttrs) { '_promise' ]; - const topLevelProps = aliases.reduce((props, alias) => { + let topLevelProps = aliases.reduce((props, alias) => { props[alias] = computed.readOnly(`__attrsResultCollection__.${alias}`); return props; }, {}); @@ -495,9 +500,9 @@ function createTopLevelPropsMixin(validatableAttrs) { /* Dedupe logic by creating a top level ResultCollection for all attr's ResultCollections */ - __attrsResultCollection__: computed(...validatableAttrs.map(attr => `attrs.${attr}`), function () { - return ValidationResultCollection.create({ - content: validatableAttrs.map(attr => get(this, `attrs.${attr}`)) + __attrsResultCollection__: computed(...validatableAttrs.map((attr) => `attrs.${attr}`), function() { + return ResultCollection.create({ + content: validatableAttrs.map((attr) => get(this, `attrs.${attr}`)) }); }).readOnly() }); @@ -515,23 +520,23 @@ function createTopLevelPropsMixin(validatableAttrs) { * @return {Array} Unique list of dependencies */ function getCPDependentKeysFor(attribute, model, validations) { - const owner = getOwner(model); + let owner = getOwner(model); - let dependentKeys = validations.map(validation => { - const type = validation._type; - const options = validation.options; - const Validator = type === 'function' ? BaseValidator : lookupValidator(owner, type); - const baseDependents = BaseValidator.getDependentsFor(attribute, options) || []; - const dependents = Validator.getDependentsFor(attribute, options) || []; + let dependentKeys = validations.map((validation) => { + let { options } = validation; + let type = validation._type; + let Validator = type === 'function' ? BaseValidator : lookupValidator(owner, type); + let baseDependents = BaseValidator.getDependentsFor(attribute, options) || []; + let dependents = Validator.getDependentsFor(attribute, options) || []; - const specifiedDependents = [].concat( + let specifiedDependents = [].concat( getWithDefault(options, 'dependentKeys', []), getWithDefault(validation, 'defaultOptions.dependentKeys', []), getWithDefault(validation, 'globalOptions.dependentKeys', []) ); // Extract dependentKeys from option CPs - const cpDependents = [].concat( + let cpDependents = [].concat( extractOptionsDependentKeys(options), extractOptionsDependentKeys(get(validation, 'defaultOptions')), extractOptionsDependentKeys(get(validation, 'globalOptions')) @@ -548,12 +553,12 @@ function getCPDependentKeysFor(attribute, model, validations) { dependentKeys.push(`model.${attribute}`); - if(isDsModel(model)) { + if (isDsModel(model)) { dependentKeys.push(`model.isDeleted`); } - dependentKeys = dependentKeys.map(d => { - return d.split('.')[0] === 'model' ? '_' + d : d; + dependentKeys = dependentKeys.map((d) => { + return `${d.split('.')[0] === 'model' ? '_' : ''}${d}`; }); return emberArray(dependentKeys).uniq(); @@ -568,11 +573,11 @@ function getCPDependentKeysFor(attribute, model, validations) { * @return {Array} dependentKeys */ function extractOptionsDependentKeys(options) { - if(options && typeof options === 'object') { + if (options && typeof options === 'object') { return Object.keys(options).reduce((arr, key) => { let option = options[key]; - if(option && typeof option === 'object' && option.isDescriptor) { + if (option && typeof option === 'object' && option.isDescriptor) { return arr.concat(option._dependentKeys || []); } @@ -596,8 +601,8 @@ function extractOptionsDependentKeys(options) { * @param {Function} resolve */ function debouncedValidate(validator, model, attribute, resolve) { - const options = get(validator, 'options').copy(); - const value = validator.getValue(); + let options = get(validator, 'options').copy(); + let value = validator.getValue(); resolve(validator.validate(value, options, model, attribute)); } @@ -614,20 +619,18 @@ function debouncedValidate(validator, model, attribute, resolve) { */ function validationReturnValueHandler(attribute, value, model, validator) { let result; + let commonProps = { + model, + attribute, + _validator: validator + }; if (isPromise(value)) { - result = ValidationResult.create({ - attribute, - _promise: Promise.resolve(value), - model, - _validator: validator + result = ValidationResult.create(commonProps, { + _promise: Promise.resolve(value) }); } else { - result = ValidationResult.create({ - attribute, - model, - _validator: validator - }); + result = ValidationResult.create(commonProps); result.update(value); } @@ -644,7 +647,7 @@ function validationReturnValueHandler(attribute, value, model, validator) { * @return {Array} */ function getValidatorsFor(attribute, model) { - const validators = get(model, `validations._validators.${attribute}`); + let validators = get(model, `validations._validators.${attribute}`); if (!isNone(validators)) { return validators; @@ -663,7 +666,7 @@ function getValidatorsFor(attribute, model) { * @return {Map} */ function getDebouncedValidationsCacheFor(attribute, model) { - const debouncedValidations = get(model, 'validations._debouncedValidations'); + let debouncedValidations = get(model, 'validations._debouncedValidations'); if (isNone(get(debouncedValidations, attribute))) { assign(debouncedValidations, attribute, {}); @@ -682,11 +685,11 @@ function getDebouncedValidationsCacheFor(attribute, model) { * @return {Array} */ function createValidatorsFor(attribute, model) { - const validations = get(model, 'validations'); - const validationRules = makeArray(get(validations, `_validationRules.${attribute}`)); - const validatorCache = get(validations, '_validators'); - const owner = getOwner(model); - const validators = []; + let validations = get(model, 'validations'); + let validationRules = makeArray(get(validations, `_validationRules.${attribute}`)); + let validatorCache = get(validations, '_validators'); + let owner = getOwner(model); + let validators = []; let validator; // We must have an owner to be able to lookup our validators @@ -694,7 +697,7 @@ function createValidatorsFor(attribute, model) { throw new TypeError(`[ember-cp-validations] ${model.toString()} is missing a container or owner.`); } - validationRules.forEach(v => { + validationRules.forEach((v) => { v.attribute = attribute; v.model = model; @@ -724,7 +727,7 @@ function createValidatorsFor(attribute, model) { * @return {Class} Validator class or undefined if not found */ function lookupValidator(owner, type) { - const validatorClass = owner._lookupFactory(`validator:${type}`); + let validatorClass = owner._lookupFactory(`validator:${type}`); if (isNone(validatorClass)) { throw new Error(`[ember-cp-validations] Validator not found of type: ${type}.`); @@ -754,17 +757,17 @@ function lookupValidator(owner, type) { * @return {Promise or Object} Promise if async is true, object if async is false */ function validate(options = {}, async = true) { - const model = get(this, 'model'); - const whiteList = makeArray(options.on); - const blackList = makeArray(options.excludes); + let model = get(this, 'model'); + let whiteList = makeArray(options.on); + let blackList = makeArray(options.excludes); - const validationResults = get(this, 'validatableAttributes').reduce((v, name) => { + let validationResults = get(this, 'validatableAttributes').reduce((v, name) => { if (!isEmpty(blackList) && blackList.indexOf(name) !== -1) { return v; } if (isEmpty(whiteList) || whiteList.indexOf(name) !== -1) { - const validationResult = get(this, `attrs.${name}`); + let validationResult = get(this, `attrs.${name}`); // If an async validation is found, throw an error if (!async && get(validationResult, 'isAsync')) { @@ -776,11 +779,11 @@ function validate(options = {}, async = true) { return v; }, []); - const resultCollection = ValidationResultCollection.create({ + let resultCollection = ResultCollection.create({ content: validationResults }); - const resultObject = { + let resultObject = { model, validations: resultCollection }; @@ -814,19 +817,22 @@ function validate(options = {}, async = true) { * @async */ function validateAttribute(attribute, value) { - const model = get(this, 'model'); - const validators = !isNone(model) ? getValidatorsFor(attribute, model) : []; + let model = get(this, 'model'); + let validators = !isNone(model) ? getValidatorsFor(attribute, model) : []; - const validationResults = generateValidationResultsFor(attribute, model, validators, (validator, options) => { + let validationResults = generateValidationResultsFor(attribute, model, validators, (validator, options) => { return validator.validate(value, options, model, attribute); }); - const validations = ValidationResultCollection.create({ + let validations = ResultCollection.create({ attribute, content: flatten(validationResults) }); - const result = { model, validations }; + let result = { + model, + validations + }; return Promise.resolve(get(validations, 'isAsync') ? get(validations, '_promise').then(() => result) : result); } @@ -837,7 +843,7 @@ function validateAttribute(attribute, value) { * - `excludes` (**Array**): Exclude validation on the given attributes * * ```javascript - * const { m, validations } = model.validateSync(); + * let { m, validations } = model.validateSync(); * validations.get('isValid') // true or false * ``` * diff --git a/addon/validations/result-collection.js b/addon/validations/result-collection.js index 1e522ab5..13247007 100644 --- a/addon/validations/result-collection.js +++ b/addon/validations/result-collection.js @@ -20,7 +20,7 @@ const { const A = emberArray(); function callable(method) { - return function (collection) { + return function(collection) { return A[method].apply(collection, arguments); }; } @@ -32,13 +32,13 @@ const compact = callable('compact'); CP Macros */ function isAny(collection, key, value, defaultValue) { - return computed(`${collection}.@each.${key}`, cycleBreaker(function () { + return computed(`${collection}.@each.${key}`, cycleBreaker(function() { return get(this, collection).isAny(key, value); }, defaultValue)); } function isEvery(collection, key, value, defaultValue) { - return computed(`${collection}.@each.${key}`, cycleBreaker(function () { + return computed(`${collection}.@each.${key}`, cycleBreaker(function() { return get(this, collection).isEvery(key, value); }, defaultValue)); } @@ -183,8 +183,8 @@ export default Ember.ArrayProxy.extend({ * @readOnly * @type {Array} */ - messages: computed('_errorContent.@each.messages', cycleBreaker(function () { - const messages = flatten(get(this, '_errorContent').getEach('messages')); + messages: computed('_errorContent.@each.messages', cycleBreaker(function() { + let messages = flatten(get(this, '_errorContent').getEach('messages')); return uniq(compact(messages)); })).readOnly(), @@ -216,8 +216,8 @@ export default Ember.ArrayProxy.extend({ * @readOnly * @type {Array} */ - warningMessages: computed('_warningContent.@each.messages', cycleBreaker(function () { - const messages = flatten(get(this, '_warningContent').getEach('messages')); + warningMessages: computed('_warningContent.@each.messages', cycleBreaker(function() { + let messages = flatten(get(this, '_warningContent').getEach('messages')); return uniq(compact(messages)); })).readOnly(), @@ -250,7 +250,7 @@ export default Ember.ArrayProxy.extend({ * @readOnly * @type {Array} */ - warnings: computed('attribute', '_warningContent.@each.errors', cycleBreaker(function () { + warnings: computed('attribute', '_warningContent.@each.errors', cycleBreaker(function() { return computeErrorCollection(get(this, 'attribute'), get(this, '_warningContent')); })).readOnly(), @@ -283,7 +283,7 @@ export default Ember.ArrayProxy.extend({ * @readOnly * @type {Array} */ - errors: computed('attribute', '_errorContent.@each.errors', cycleBreaker(function () { + errors: computed('attribute', '_errorContent.@each.errors', cycleBreaker(function() { return computeErrorCollection(get(this, 'attribute'), get(this, '_errorContent')); })).readOnly(), @@ -334,7 +334,7 @@ export default Ember.ArrayProxy.extend({ * @readOnly * @type {Object} */ - options: computed('_contentValidators.@each.options', function () { + options: computed('_contentValidators.@each.options', function() { return groupValidatorOptions(get(this, '_contentValidators')); }).readOnly(), @@ -344,7 +344,7 @@ export default Ember.ArrayProxy.extend({ * @private * @type {Promise} */ - _promise: computed('content.@each._promise', cycleBreaker(function () { + _promise: computed('content.@each._promise', cycleBreaker(function() { return RSVP.allSettled(compact(flatten(this.getEach('_promise')))); })).readOnly(), @@ -367,16 +367,15 @@ export default Ember.ArrayProxy.extend({ * @type {Array} * @private */ - _warningContent: computed.filterBy('content', 'isWarning', true).readOnly(), + _warningContent: computed.filterBy('content', 'isWarning', true).readOnly() }); - function computeErrorCollection(attribute, content = []) { let errors = flatten(content.getEach('errors')); errors = uniq(compact(errors)); - errors.forEach(e => { - if(attribute && e.get('attribute') !== attribute) { + errors.forEach((e) => { + if (attribute && e.get('attribute') !== attribute) { e.set('parentAttribute', attribute); } }); @@ -394,8 +393,8 @@ function groupValidatorOptions(validators = []) { return options; } - const type = get(v, '_type'); - const vOpts = get(v, 'options').copy(); + let type = get(v, '_type'); + let vOpts = get(v, 'options').copy(); if (options[type]) { if (isArray(options[type])) { diff --git a/addon/validations/validator.js b/addon/validations/validator.js index 289a60e6..5d98f9c1 100644 --- a/addon/validations/validator.js +++ b/addon/validations/validator.js @@ -75,7 +75,7 @@ const { * * Default: __false__ * - * If set to __true__, disables the given validator. + * If set to __true__, disables the given validator. * * ```js * // Examples @@ -192,8 +192,8 @@ const { * @submodule Common Options */ -export default function (arg1, options) { - const props = { +export default function(arg1, options) { + let props = { options: isNone(options) ? {} : options }; diff --git a/addon/validators/alias.js b/addon/validators/alias.js index b4b75df2..770baa4e 100644 --- a/addon/validators/alias.js +++ b/addon/validators/alias.js @@ -9,7 +9,7 @@ import Base from 'ember-cp-validations/validators/base'; const { get, assert, - isEmpty, + isPresent, getProperties } = Ember; @@ -36,6 +36,7 @@ const { * @extends Base */ const Alias = Base.extend({ + _type: 'alias', /** * Normalized options passed in. @@ -65,11 +66,11 @@ const Alias = Base.extend({ }, validate(value, options, model, attribute) { - const { alias, firstMessageOnly } = getProperties(options, ['alias', 'firstMessageOnly']); + let { alias, firstMessageOnly } = getProperties(options, ['alias', 'firstMessageOnly']); - assert(`[ember-cp-validations] [validator:alias] [${attribute}] option 'alias' is required`, !isEmpty(alias)); + assert(`[validator:alias] [${attribute}] option 'alias' is required`, isPresent(alias)); - const aliasValidation = get(model, `validations.attrs.${alias}`); + let aliasValidation = get(model, `validations.attrs.${alias}`); return firstMessageOnly ? get(aliasValidation, 'message') : get(aliasValidation, 'content'); } @@ -77,9 +78,9 @@ const Alias = Base.extend({ Alias.reopenClass({ getDependentsFor(attribute, options) { - const alias = typeof options === 'string' ? options : get(options, 'alias'); + let alias = typeof options === 'string' ? options : get(options, 'alias'); - assert(`[ember-cp-validations] [validator:alias] [${attribute}] 'alias' must be a string`, typeof alias === 'string'); + assert(`[validator:alias] [${attribute}] 'alias' must be a string`, typeof alias === 'string'); return [ `${alias}.messages.[]`, `${alias}.isTruelyValid` ]; } diff --git a/addon/validators/base.js b/addon/validators/base.js index 89b5816a..decd942a 100644 --- a/addon/validators/base.js +++ b/addon/validators/base.js @@ -81,10 +81,10 @@ const Base = Ember.Object.extend({ init() { this._super(...arguments); - const globalOptions = get(this, 'globalOptions'); - const defaultOptions = get(this, 'defaultOptions'); - const options = get(this, 'options'); - const owner = getOwner(this); + let globalOptions = get(this, 'globalOptions'); + let defaultOptions = get(this, 'defaultOptions'); + let options = get(this, 'options'); + let owner = getOwner(this); let errorMessages; if (!isNone(owner)) { @@ -110,19 +110,19 @@ const Base = Ember.Object.extend({ * @return {Object} */ buildOptions(options = {}, defaultOptions = {}, globalOptions = {}) { - const builtOptions = assign(assign(assign({}, globalOptions), defaultOptions), options); + let builtOptions = assign(assign(assign({}, globalOptions), defaultOptions), options); // Overwrite the validator's value method if it exists in the options and remove it since // there is no need for it to be passed around this.value = builtOptions.value || this.value; delete builtOptions.value; - const OptionsClass = Ember.Object.extend(builtOptions, { + let OptionsClass = Ember.Object.extend(builtOptions, { model: computed(() => get(this, 'model')).readOnly(), attribute: computed(() => get(this, 'attribute')).readOnly(), copy(deep) { - if(deep) { + if (deep) { return OptionsClass.create(); } @@ -158,7 +158,7 @@ const Base = Ember.Object.extend({ * @return {Mixed} value */ getValue() { - const value = this.value(get(this, 'model'), get(this, 'attribute')); + let value = this.value(get(this, 'model'), get(this, 'attribute')); return getValidatableValue(value); }, @@ -214,7 +214,7 @@ const Base = Ember.Object.extend({ * @return {String} The generated message */ createErrorMessage(type, value, options = {}) { - const messages = this.get('errorMessages'); + let messages = this.get('errorMessages'); let message = unwrapString(get(options, 'message')); set(options, 'description', messages.getDescriptionFor(get(this, 'attribute'), options)); diff --git a/addon/validators/belongs-to.js b/addon/validators/belongs-to.js index 5258dd9b..f51c89d8 100755 --- a/addon/validators/belongs-to.js +++ b/addon/validators/belongs-to.js @@ -11,7 +11,6 @@ const { get } = Ember; - /** * Identifies a `belongs-to` relationship in an Ember Data Model or Ember.Object. * This is used to create a link to the validations object of the child model. @@ -80,10 +79,12 @@ const { * @extends Base */ const BelongsTo = Base.extend({ + _type: 'belongs-to', + validate(value) { if (value) { if (isPromise(value)) { - return value.then(model => model ? get(model, 'validations') : true); + return value.then((model) => model ? get(model, 'validations') : true); } return get(value, 'validations'); } diff --git a/addon/validators/collection.js b/addon/validators/collection.js index 710d9296..72face27 100755 --- a/addon/validators/collection.js +++ b/addon/validators/collection.js @@ -4,11 +4,10 @@ */ import Ember from 'ember'; -import Base from 'ember-cp-validations/validators/base'; +import EmberValidator from 'ember-cp-validations/-private/ember-validator'; const { - get, - isArray + get } = Ember; /** @@ -29,7 +28,8 @@ const { * @module Validators * @extends Base */ -const Collection = Base.extend({ +const Collection = EmberValidator.extend({ + _type: 'collection', /** * Normalized options passed in. @@ -56,26 +56,12 @@ const Collection = Base.extend({ }; } return this._super(opts, defaultOptions, globalOptions); - }, - - validate(value, options) { - const isCollection = get(options, 'collection'); - - if (isCollection === true && !isArray(value)) { - return this.createErrorMessage('collection', value, options); - } - - if (isCollection === false && isArray(value)) { - return this.createErrorMessage('singular', value, options); - } - - return true; } }); Collection.reopenClass({ getDependentsFor(attribute, options) { - return (options === true || get(options, 'collection') === true) ? [ `model.${attribute}.[]` ] : []; + return (options === true || get(options, 'collection') === true) ? [`model.${attribute}.[]`] : []; } }); diff --git a/addon/validators/confirmation.js b/addon/validators/confirmation.js index 1944f54b..2a85ab1b 100755 --- a/addon/validators/confirmation.js +++ b/addon/validators/confirmation.js @@ -4,13 +4,11 @@ */ import Ember from 'ember'; -import Base from 'ember-cp-validations/validators/base'; +import EmberValidator from 'ember-cp-validations/-private/ember-validator'; const { get, - assert, - isEqual, - isEmpty, + assert } = Ember; /** @@ -34,27 +32,17 @@ const { * @module Validators * @extends Base */ -const Confirmation = Base.extend({ - validate(value, options, model, attribute) { - const on = get(options, 'on'); - - assert(`[ember-cp-validations] [validator:confirmation] [${attribute}] option 'on' is required`, !isEmpty(on)); - - if (!isEqual(value, get(model, on))) { - return this.createErrorMessage('confirmation', value, options); - } - - return true; - } +const Confirmation = EmberValidator.extend({ + _type: 'confirmation' }); Confirmation.reopenClass({ getDependentsFor(attribute, options) { - const on = get(options, 'on'); + let on = get(options, 'on'); - assert(`[ember-cp-validations] [validator:confirmation] [${attribute}] 'on' must be a string`, typeof on === 'string'); + assert(`[validator:confirmation] [${attribute}] 'on' must be a string`, typeof on === 'string'); - return on ? [ `model.${on}` ] : []; + return on ? [`model.${on}`] : []; } }); diff --git a/addon/validators/date.js b/addon/validators/date.js index 0fcf8dd7..c4d7f2f2 100755 --- a/addon/validators/date.js +++ b/addon/validators/date.js @@ -3,24 +3,7 @@ * Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms. */ -import Ember from 'ember'; -import Base from 'ember-cp-validations/validators/base'; -import { requireModule } from 'ember-cp-validations/utils/utils'; - -const moment = requireModule('moment'); - -if (!moment) { - throw new Error('MomentJS is required to use the Date validator. The easiest way to install moment.js is to install ember-moment.\n' + - 'Installation instructions and documentation can be found at https://github.com/stefanpenner/ember-moment'); -} - -const { - getWithDefault, - getProperties, - isNone, - isEmpty, - set -} = Ember; +import EmberValidator from 'ember-cp-validations/-private/ember-validator'; /** * Validate over a date range. Uses [MomentJS](http://momentjs.com/) for date mathematics and calculations. @@ -53,70 +36,6 @@ const { * @module Validators * @extends Base */ -export default Base.extend({ - - _parseDate(date, format, useStrict = false) { - if (date === 'now' || isEmpty(date)) { - return moment(); - } - - return isNone(format) ? moment(new Date(date)) : moment(date, format, useStrict); - }, - - validate(value, options) { - const errorFormat = getWithDefault(options, 'errorFormat', 'MMM Do, YYYY'); - const { format, precision, allowBlank } = getProperties(options, ['format', 'precision', 'allowBlank']); - let { before, onOrBefore, after, onOrAfter } = getProperties(options, ['before', 'onOrBefore', 'after', 'onOrAfter']); - let date; - - if (allowBlank && isEmpty(value)) { - return true; - } - - if (format) { - date = this._parseDate(value, format, true); - if (!date.isValid()) { - return this.createErrorMessage('wrongDateFormat', value, options); - } - } else { - date = this._parseDate(value); - if (!date.isValid()) { - return this.createErrorMessage('date', value, options); - } - } - - if (before) { - before = this._parseDate(before, format); - if (!date.isBefore(before, precision)) { - set(options, 'before', before.format(errorFormat)); - return this.createErrorMessage('before', value, options); - } - } - - if (onOrBefore) { - onOrBefore = this._parseDate(onOrBefore, format); - if (!date.isSameOrBefore(onOrBefore, precision)) { - set(options, 'onOrBefore', onOrBefore.format(errorFormat)); - return this.createErrorMessage('onOrBefore', value, options); - } - } - - if (after) { - after = this._parseDate(after, format); - if (!date.isAfter(after, precision)) { - set(options, 'after', after.format(errorFormat)); - return this.createErrorMessage('after', value, options); - } - } - - if (onOrAfter) { - onOrAfter = this._parseDate(onOrAfter, format); - if (!date.isSameOrAfter(onOrAfter, precision)) { - set(options, 'onOrAfter', onOrAfter.format(errorFormat)); - return this.createErrorMessage('onOrAfter', value, options); - } - } - - return true; - } +export default EmberValidator.extend({ + _type: 'date' }); diff --git a/addon/validators/dependent.js b/addon/validators/dependent.js index 3b6961ba..8a7be4d2 100755 --- a/addon/validators/dependent.js +++ b/addon/validators/dependent.js @@ -14,6 +14,7 @@ const { assert, isNone, isEmpty, + isPresent, isArray } = Ember; @@ -36,10 +37,12 @@ const { * @extends Base */ const Dependent = Base.extend({ + _type: 'dependent', + validate(value, options, model, attribute) { - const { on, allowBlank } = getProperties(options, ['on', 'allowBlank']); + let { on, allowBlank } = getProperties(options, ['on', 'allowBlank']); - assert(`[ember-cp-validations] [validator:dependent] [${attribute}] option 'on' is required`, !isEmpty(on)); + assert(`[validator:dependent] [${attribute}] option 'on' is required`, isPresent(on)); if (isNone(model)) { return true; @@ -49,9 +52,9 @@ const Dependent = Base.extend({ return true; } - const dependentValidations = getWithDefault(options, 'on', A()).map(dependent => get(model, `validations.attrs.${dependent}`)); + let dependentValidations = getWithDefault(options, 'on', A()).map((dependent) => get(model, `validations.attrs.${dependent}`)); - if (!isEmpty(dependentValidations.filter(v => !get(v, 'isTruelyValid')))) { + if (!isEmpty(dependentValidations.filter((v) => !get(v, 'isTruelyValid')))) { return this.createErrorMessage('invalid', value, options); } @@ -61,12 +64,12 @@ const Dependent = Base.extend({ Dependent.reopenClass({ getDependentsFor(attribute, options) { - const dependents = get(options, 'on'); + let dependents = get(options, 'on'); - assert(`[ember-cp-validations] [validator:dependent] [${attribute}] 'on' must be an array`, isArray(dependents)); + assert(`[validator:dependent] [${attribute}] 'on' must be an array`, isArray(dependents)); if (!isEmpty(dependents)) { - return dependents.map(dependent => `${dependent}.isTruelyValid`); + return dependents.map((dependent) => `${dependent}.isTruelyValid`); } return []; diff --git a/addon/validators/ds-error.js b/addon/validators/ds-error.js index fec915f9..e1956bf6 100755 --- a/addon/validators/ds-error.js +++ b/addon/validators/ds-error.js @@ -3,20 +3,8 @@ * Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms. */ -import Ember from 'ember'; -import Base from 'ember-cp-validations/validators/base'; -import { requireModule } from 'ember-cp-validations/utils/utils'; - -const DS = requireModule('ember-data'); - -if (!DS) { - throw new Error('Ember-Data is required to use the DS Error validator.'); -} - -const { - get, - isNone -} = Ember; +import EmberValidator from 'ember-cp-validations/-private/ember-validator'; +import { getPathAndKey } from 'ember-validators/ds-error'; /** * Creates a link between this library and Ember-Data's [DS.Errors](http://emberjs.com/api/data/classes/DS.Errors.html) @@ -31,35 +19,16 @@ const { * @module Validators * @extends Base */ -const DSError = Base.extend({ - validate(value, options, model, attribute) { - let { path, key } = getPathAndKey(attribute); - - const errors = get(model, path); - - if (!isNone(errors) && errors instanceof DS.Errors && errors.has(key)) { - return get(errors.errorsFor(key), 'lastObject.message'); - } - - return true; - } +const DSError = EmberValidator.extend({ + _type: 'ds-error' }); DSError.reopenClass({ getDependentsFor(attribute) { let { path, key } = getPathAndKey(attribute); - return [ `model.${path}.${key}.[]` ]; + return [`model.${path}.${key}.[]`]; } }); -function getPathAndKey(attribute) { - let path = attribute.split('.'); - let key = path.pop(); - - path.push('errors'); - - return { path: path.join('.'), key }; -} - export default DSError; diff --git a/addon/validators/exclusion.js b/addon/validators/exclusion.js index 17842bca..80e09b4f 100755 --- a/addon/validators/exclusion.js +++ b/addon/validators/exclusion.js @@ -3,16 +3,7 @@ * Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms. */ -import Ember from 'ember'; -import Base from 'ember-cp-validations/validators/base'; - -const { - get, - typeOf, - isEmpty, - assert, - getProperties -} = Ember; +import EmberValidator from 'ember-cp-validations/-private/ember-validator'; /** * Validates that the attributes’ values are not included in a given list. All comparisons are done using strict equality so type matters! For range, the value type is checked against both lower and upper bounds for type equality. @@ -36,31 +27,6 @@ const { * @module Validators * @extends Base */ -export default Base.extend({ - validate(value, options, model, attribute) { - const array = get(options, 'in'); - const { range, allowBlank } = getProperties(options, ['range', 'allowBlank']); - - assert(`[ember-cp-validations] [validator:exclusion] [${attribute}] no options were passed in`, !isEmpty(Object.keys(options))); - - if (allowBlank && isEmpty(value)) { - return true; - } - - if (array && array.indexOf(value) !== -1) { - return this.createErrorMessage('exclusion', value, options); - } - - if (range && range.length === 2) { - const min = range[0]; - const max = range[1]; - const equalType = typeOf(value) === typeOf(min) && typeOf(value) === typeOf(max); - - if (equalType && min <= value && value <= max) { - return this.createErrorMessage('exclusion', value, options); - } - } - - return true; - } +export default EmberValidator.extend({ + _type: 'exclusion' }); diff --git a/addon/validators/format.js b/addon/validators/format.js index d288859a..3459ffd0 100755 --- a/addon/validators/format.js +++ b/addon/validators/format.js @@ -1,18 +1,15 @@ - /** * Copyright 2016, Yahoo! Inc. * Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms. */ import Ember from 'ember'; -import Base from 'ember-cp-validations/validators/base'; +import EmberValidator from 'ember-cp-validations/-private/ember-validator'; +import { regularExpressions } from 'ember-validators/format'; const { get, - isNone, - isEmpty, - assert, - getProperties + isNone } = Ember; /** @@ -57,13 +54,9 @@ const { * @module Validators * @extends Base */ -export default Base.extend({ - regularExpressions: { - email: /^[a-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/i, - emailOptionalTld: /^[a-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.?)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/i, - phone: /^([\+]?1\s*[-\/\.]?\s*)?(\((\d{3})\)|(\d{3}))\s*[-\/\.]?\s*(\d{3})\s*[-\/\.]?\s*(\d{4})\s*(([xX]|[eE][xX][tT]?[\.]?|extension)\s*([#*\d]+))*$/, - url: /(?:([A-Za-z]+):)?(\/{0,3})[a-zA-Z0-9][a-zA-Z-0-9]*(\.[\w-]+)+([\w.,@?^=%&:\/~+#-{}]*[\w@?^=%&\/~+#-{}])??/ - }, +export default EmberValidator.extend({ + _type: 'format', + regularExpressions, /** * Normalized options passed in by applying the desired regex or using the one declared @@ -75,8 +68,8 @@ export default Base.extend({ * @return {Object} */ buildOptions(options = {}, defaultOptions = {}, globalOptions = {}) { - const regularExpressions = get(this, 'regularExpressions'); - const { regex, type } = options; + let regularExpressions = get(this, 'regularExpressions'); + let { regex, type } = options; if (type && !isNone(regularExpressions[type]) && isNone(regex)) { if (type === 'email' && options.allowNonTld) { @@ -87,21 +80,5 @@ export default Base.extend({ } return this._super(options, defaultOptions, globalOptions); - }, - - validate(value, options, model, attribute) { - const { regex, type, allowBlank } = getProperties(options, ['regex', 'type', 'allowBlank']); - - assert(`[ember-cp-validations] [validator:format] [${attribute}] no options were passed in`, !isEmpty(Object.keys(options))); - - if (allowBlank && isEmpty(value)) { - return true; - } - - if (regex && !regex.test(value)) { - return this.createErrorMessage(type || 'invalid', value, options); - } - - return true; } }); diff --git a/addon/validators/has-many.js b/addon/validators/has-many.js index da707526..b2dabd27 100755 --- a/addon/validators/has-many.js +++ b/addon/validators/has-many.js @@ -52,12 +52,14 @@ import { isPromise } from 'ember-cp-validations/utils/utils'; * @extends Base */ const HasMany = Base.extend({ + _type: 'has-many', + validate(value) { if (value) { if (isPromise(value)) { - return value.then(models => models ? models.map(m => m.get('validations')) : true); + return value.then((models) => models ? models.map((m) => m.get('validations')) : true); } - return value.map(m => m.get('validations')); + return value.map((m) => m.get('validations')); } return true; diff --git a/addon/validators/inclusion.js b/addon/validators/inclusion.js index c3dc75af..5facbda3 100755 --- a/addon/validators/inclusion.js +++ b/addon/validators/inclusion.js @@ -3,16 +3,7 @@ * Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms. */ -import Ember from 'ember'; -import Base from 'ember-cp-validations/validators/base'; - -const { - get, - typeOf, - assert, - isEmpty, - getProperties -} = Ember; +import EmberValidator from 'ember-cp-validations/-private/ember-validator'; /** * Validates that the attributes’ values are included in a given list. All comparisons are done using strict equality so type matters! @@ -52,31 +43,6 @@ const { * @module Validators * @extends Base */ -export default Base.extend({ - validate(value, options, model, attribute) { - const array = get(options, 'in'); - const { range, allowBlank } = getProperties(options, ['range', 'allowBlank']); - - assert(`[ember-cp-validations] [validator:inclusion] [${attribute}] no options were passed in`, !isEmpty(Object.keys(options))); - - if (allowBlank && isEmpty(value)) { - return true; - } - - if (array && array.indexOf(value) === -1) { - return this.createErrorMessage('inclusion', value, options); - } - - if (range && range.length === 2) { - const min = range[0]; - const max = range[1]; - const equalType = typeOf(value) === typeOf(min) && typeOf(value) === typeOf(max); - - if (!equalType || min > value || value > max) { - return this.createErrorMessage('inclusion', value, options); - } - } - - return true; - } +export default EmberValidator.extend({ + _type: 'inclusion' }); diff --git a/addon/validators/length.js b/addon/validators/length.js index 70bf9fb1..32bacd3a 100755 --- a/addon/validators/length.js +++ b/addon/validators/length.js @@ -4,14 +4,11 @@ */ import Ember from 'ember'; -import Base from 'ember-cp-validations/validators/base'; +import EmberValidator from 'ember-cp-validations/-private/ember-validator'; const { get, - assert, - isNone, - isEmpty, - getProperties + isNone } = Ember; /** @@ -39,7 +36,9 @@ const { * @module Validators * @extends Base */ -export default Base.extend({ +export default EmberValidator.extend({ + _type: 'length', + /** * Default allowNone to true * @@ -53,33 +52,5 @@ export default Base.extend({ options.allowNone = isNone(get(options, 'allowNone')) ? true : get(options, 'allowNone'); return this._super(options, defaultOptions, globalOptions); - }, - - validate(value, options, model, attribute) { - const { allowNone, allowBlank, is, min, max } = getProperties(options, [ 'allowNone', 'allowBlank', 'is', 'min', 'max' ]); - - assert(`[ember-cp-validations] [validator:length] [${attribute}] no options were passed in`, !isEmpty(Object.keys(options))); - - if (isNone(value)) { - return allowNone ? true : this.createErrorMessage('invalid', value, options); - } - - if (allowBlank && isEmpty(value)) { - return true; - } - - if (!isNone(is) && is !== get(value, 'length')) { - return this.createErrorMessage('wrongLength', value, options); - } - - if (!isNone(min) && min > get(value, 'length')) { - return this.createErrorMessage('tooShort', value, options); - } - - if (!isNone(max) && max < get(value, 'length')) { - return this.createErrorMessage('tooLong', value, options); - } - - return true; } }); diff --git a/addon/validators/messages.js b/addon/validators/messages.js index 011dca47..ddbcecc3 100644 --- a/addon/validators/messages.js +++ b/addon/validators/messages.js @@ -3,11 +3,7 @@ * Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms. */ import Ember from 'ember'; - -const { - get, - isNone -} = Ember; +import Messages from 'ember-validators/messages'; /** * The default validation error messages are imported in your app's `validators` folder. @@ -30,96 +26,4 @@ const { * @class Messages * @module Validators */ -export default Ember.Object.extend({ - - /** - * Regex for matching error message placeholders - * @private - * @property _regex - * @type {RegExp} - */ - _regex: /\{(\w+)\}/g, - - /** - * Default attribute description if one isn't passed into a validator's options - * @property defaultDescription - * @type {String} - */ - defaultDescription: 'This field', - - /** - * Get a description for a specific attribute. This is a hook - * for i18n solutions to retrieve attribute descriptions from a translation - * @method getDescriptionFor - * @param {String} attribute - * @param {Object} options - * @return {String} - */ - getDescriptionFor(attribute, options = {}) { - return get(options, 'description') || get(this, 'defaultDescription'); - }, - - /** - * Get a message with a given type - * @method getMessageFor - * @param {String} type - * @param {Object} context - * @return {String} - */ - getMessageFor(type, context = {}) { - return this.formatMessage(get(this, type), context); - }, - - /** - * Regex replace all placeholders with their given context - * @method formatMessage - * @param {String} message - * @param {Object} context - * @return {String} - */ - formatMessage(message, context = {}) { - let m = message; - - if (isNone(m) || typeof m !== 'string') { - m = get(this, 'invalid'); - } - return m.replace(get(this, '_regex'), (s, attr) => get(context, attr)); - }, - - /** - * Default validation error message strings - */ - inclusion: '{description} is not included in the list', - exclusion: '{description} is reserved', - invalid: '{description} is invalid', - confirmation: '{description} doesn\'t match {on}', - accepted: '{description} must be accepted', - empty: '{description} can\'t be empty', - blank: '{description} can\'t be blank', - present: '{description} must be blank', - collection: '{description} must be a collection', - singular: '{description} can\'t be a collection', - tooLong: '{description} is too long (maximum is {max} characters)', - tooShort: '{description} is too short (minimum is {min} characters)', - before: '{description} must be before {before}', - onOrBefore: '{description} must be on or before {onOrBefore}', - after: '{description} must be after {after}', - onOrAfter: '{description} must be on or after {onOrAfter}', - wrongDateFormat: '{description} must be in the format of {format}', - wrongLength: '{description} is the wrong length (should be {is} characters)', - notANumber: '{description} must be a number', - notAnInteger: '{description} must be an integer', - greaterThan: '{description} must be greater than {gt}', - greaterThanOrEqualTo: '{description} must be greater than or equal to {gte}', - equalTo: '{description} must be equal to {is}', - lessThan: '{description} must be less than {lt}', - lessThanOrEqualTo: '{description} must be less than or equal to {lte}', - otherThan: '{description} must be other than {value}', - odd: '{description} must be odd', - even: '{description} must be even', - positive: '{description} must be positive', - date: '{description} must be a valid date', - email: '{description} must be a valid email address', - phone: '{description} must be a valid phone number', - url: '{description} must be a valid url' -}); +export default Ember.Object.extend(Messages); diff --git a/addon/validators/number.js b/addon/validators/number.js index cc1fc5d7..1b759885 100755 --- a/addon/validators/number.js +++ b/addon/validators/number.js @@ -3,14 +3,7 @@ * Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms. */ -import Ember from 'ember'; -import Base from 'ember-cp-validations/validators/base'; - -const { - get, - isEmpty, - getProperties -} = Ember; +import EmberValidator from 'ember-cp-validations/-private/ember-validator'; /** * Validates that your attributes have only numeric values. @@ -43,72 +36,6 @@ const { * @module Validators * @extends Base */ -export default Base.extend({ - validate(value, options) { - const numValue = Number(value); - const optionKeys = Object.keys(options); - const { allowBlank, allowString, integer } = getProperties(options, ['allowBlank', 'allowString', 'integer']); - - if (allowBlank && isEmpty(value)) { - return true; - } - - if (typeof value === 'string' && (isEmpty(value) || !allowString)) { - return this.createErrorMessage('notANumber', value, options); - } - - if (!this.isNumber(numValue)) { - return this.createErrorMessage('notANumber', value, options); - } - - if (integer && !this.isInteger(numValue)) { - return this.createErrorMessage('notAnInteger', value, options); - } - - for (let i = 0; i < optionKeys.length; i++) { - const type = optionKeys[i]; - const m = this._validateType(type, options, numValue); - - if (typeof m === 'string') { - return m; - } - } - - return true; - }, - - _validateType(type, options, value) { - const expected = get(options, type); - const actual = value; - - if (type === 'is' && actual !== expected) { - return this.createErrorMessage('equalTo', value, options); - } else if (type === 'lt' && actual >= expected) { - return this.createErrorMessage('lessThan', value, options); - } else if (type === 'lte' && actual > expected) { - return this.createErrorMessage('lessThanOrEqualTo', value, options); - } else if (type === 'gt' && actual <= expected) { - return this.createErrorMessage('greaterThan', value, options); - } else if (type === 'gte' && actual < expected) { - return this.createErrorMessage('greaterThanOrEqualTo', value, options); - } else if (type === 'positive' && actual < 0) { - return this.createErrorMessage('positive', value, options); - } else if (type === 'odd' && actual % 2 === 0) { - return this.createErrorMessage('odd', value, options); - } else if (type === 'even' && actual % 2 !== 0) { - return this.createErrorMessage('even', value, options); - } - - return true; - }, - - /* Use polyfills instead of Number.isNaN or Number.isInteger to support IE & Safari */ - - isNumber(value) { - return typeof value === 'number' && !isNaN(value); - }, - - isInteger(value) { - return typeof value === 'number' && isFinite(value) && Math.floor(value) === value; - } +export default EmberValidator.extend({ + _type: 'number' }); diff --git a/addon/validators/presence.js b/addon/validators/presence.js index 3d77bb2a..9ccae669 100755 --- a/addon/validators/presence.js +++ b/addon/validators/presence.js @@ -3,16 +3,7 @@ * Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms. */ -import Ember from 'ember'; -import Base from 'ember-cp-validations/validators/base'; -import { unwrapProxy } from 'ember-cp-validations/utils/utils'; - -const { - assert, - isEmpty, - isPresent, - getProperties -} = Ember; +import EmberValidator from 'ember-cp-validations/-private/ember-validator'; /** * If `true` validates that the given value is not empty, if `false`, validates that the given value is empty. @@ -40,7 +31,8 @@ const { * @module Validators * @extends Base */ -export default Base.extend({ +export default EmberValidator.extend({ + _type: 'presence', /** * Normalized options passed in. @@ -67,29 +59,5 @@ export default Base.extend({ }; } return this._super(opts, defaultOptions, globalOptions); - }, - - validate(value, options, model, attribute) { - const { presence, ignoreBlank } = getProperties(options, ['presence', 'ignoreBlank']); - - assert(`[ember-cp-validations] [validator:presence] [${attribute}] option 'presence' is required`, !isEmpty(presence)); - - if (presence === true && !this._isPresent(value, ignoreBlank)) { - return this.createErrorMessage('blank', value, options); - } - - if (presence === false && this._isPresent(value, ignoreBlank)) { - return this.createErrorMessage('present', value, options); - } - - return true; - }, - - /** - * Handle presence of ember proxy based instances - */ - _isPresent(value, ignoreBlank = false) { - const v = unwrapProxy(value); - return ignoreBlank ? isPresent(v) : !isEmpty(v); } }); diff --git a/jsconfig.json b/jsconfig.json new file mode 100644 index 00000000..f408cac8 --- /dev/null +++ b/jsconfig.json @@ -0,0 +1 @@ +{"compilerOptions":{"target":"es6","experimentalDecorators":true},"exclude":["node_modules","bower_components","tmp","vendor",".git","dist"]} \ No newline at end of file diff --git a/package.json b/package.json index ad734ab4..dc5781fa 100644 --- a/package.json +++ b/package.json @@ -64,6 +64,7 @@ "ember-load-initializers": "^0.5.1", "ember-moment": "6.1.0", "ember-resolver": "^2.0.3", + "ember-suave": "4.0.1", "ember-truth-helpers": "1.2.0", "loader.js": "^4.0.11", "moment": "2.15.1", @@ -82,6 +83,7 @@ "ember-cli-babel": "^5.1.8", "ember-cli-version-checker": "^1.1.4", "ember-getowner-polyfill": "^1.0.1", + "ember-validators": "~0.1.0", "ember-string-ishtmlsafe-polyfill": "1.0.1", "exists-sync": "0.0.3", "walk-sync": "^0.3.1" diff --git a/tests/acceptance/dummy-index-test.js b/tests/acceptance/dummy-index-test.js index aa40a234..4cb015de 100644 --- a/tests/acceptance/dummy-index-test.js +++ b/tests/acceptance/dummy-index-test.js @@ -1,40 +1,37 @@ -import Ember from "ember"; -import { - module, test -} -from 'qunit'; +import Ember from 'ember'; +import { module, test } from 'qunit'; import startApp from '../helpers/start-app'; -var App; +let App; -var validInputValues = { - username: "offirgolan", - password: "Pass123", - email: "offirgolan@gmail.com", - emailConfirmation: "offirgolan@gmail.com", - firstName: "Offir", - lastName: "Golan", - dob: "1/1/1930" +const validInputValues = { + username: 'offirgolan', + password: 'Pass123', + email: 'offirgolan@gmail.com', + emailConfirmation: 'offirgolan@gmail.com', + firstName: 'Offir', + lastName: 'Golan', + dob: '1/1/1930' }; module('Acceptance | Dummy | index', { - beforeEach: function() { + beforeEach() { App = startApp(); }, - afterEach: function() { + afterEach() { Ember.run(App, App.destroy); } }); -test("Page Loads", function(assert) { +test('Page Loads', function(assert) { assert.expect(2); visit('/'); andThen(function() { - assert.equal(find('a.navbar-brand').text().trim(), "CP Validations"); - assert.equal(find('.form .register h2').text(), "Create an Account"); + assert.equal(find('a.navbar-brand').text().trim(), 'CP Validations'); + assert.equal(find('.form .register h2').text(), 'Create an Account'); }); }); -test("Helper tooltips", function(assert) { +test('Helper tooltips', function(assert) { assert.expect(2); visit('/'); andThen(function() { @@ -43,7 +40,7 @@ test("Helper tooltips", function(assert) { }); }); -test("Invalid form submit", function(assert) { +test('Invalid form submit', function(assert) { visit('/'); andThen(function() { click('#signup'); @@ -54,11 +51,11 @@ test("Invalid form submit", function(assert) { }); }); -test("Valid form submit", function(assert) { +test('Valid form submit', function(assert) { visit('/'); andThen(function() { Object.keys(validInputValues).forEach((input) => { - let $input = find(`.validated-input input[name="${input}"]`); + let $input = find(`.validated-input input[name='${input}']`); assert.ok($input, `${input} found`); fillIn($input, validInputValues[input]); assert.ok($input.parent('.validated-input.has-success'), `${input} success`); @@ -72,10 +69,10 @@ test("Valid form submit", function(assert) { }); }); -test("Invalid to valid email", function(assert) { +test('Invalid to valid email', function(assert) { assert.expect(4); visit('/'); - var $input; + let $input; andThen(function() { $input = find('.validated-input input[name="email"]'); assert.ok($input); @@ -86,7 +83,7 @@ test("Invalid to valid email", function(assert) { assert.equal($input.parent('.form-group').find('.input-error .error').text().trim(), 'This field must be a valid email address'); assert.ok($input.parent('.validated-input.has-error')); - fillIn($input, validInputValues['email']); + fillIn($input, validInputValues.email); assert.ok($input.parent('.validated-input.has-success')); }); }); diff --git a/tests/dummy/app/components/validated-input.js b/tests/dummy/app/components/validated-input.js index 32e4cd06..251b4a3a 100644 --- a/tests/dummy/app/components/validated-input.js +++ b/tests/dummy/app/components/validated-input.js @@ -35,7 +35,7 @@ export default Ember.Component.extend({ init() { this._super(...arguments); - const valuePath = this.get('valuePath'); + let valuePath = this.get('valuePath'); defineProperty(this, 'validation', computed.readOnly(`model.validations.attrs.${valuePath}`)); defineProperty(this, 'value', computed.alias(`model.${valuePath}`)); diff --git a/tests/dummy/app/controllers/index.js b/tests/dummy/app/controllers/index.js index 1b1e4163..5e4785ed 100644 --- a/tests/dummy/app/controllers/index.js +++ b/tests/dummy/app/controllers/index.js @@ -16,7 +16,7 @@ export default Ember.Controller.extend({ this.get('model').validate().then(({ validations }) => { this.set('didValidate', true); - if(validations.get('isValid')) { + if (validations.get('isValid')) { this.setProperties({ showAlert: false, isRegistered: true, diff --git a/tests/dummy/app/index.html b/tests/dummy/app/index.html index f4e263bd..0b9625ad 100644 --- a/tests/dummy/app/index.html +++ b/tests/dummy/app/index.html @@ -1,27 +1,27 @@ - - + + Ember CP Validations - - + + {{content-for 'head'}} - - + + - + {{content-for 'head-footer'}} {{content-for 'body'}} - - - + + + {{content-for 'body-footer'}} diff --git a/tests/dummy/app/models/order-line.js b/tests/dummy/app/models/order-line.js index 02b689b1..8ede276e 100644 --- a/tests/dummy/app/models/order-line.js +++ b/tests/dummy/app/models/order-line.js @@ -6,21 +6,21 @@ from 'ember-cp-validations'; const Validations = buildValidations({ type: { - description: "Order Line Type", + description: 'Order Line Type', validators: [ validator('ds-error'), validator('presence', true) ] }, order: { - description: "Order", + description: 'Order', validators: [ validator('ds-error'), validator('presence', true) ] }, selections: { - description: "Order Selections", + description: 'Order Selections', validators: [ validator('ds-error'), validator('has-many'), @@ -33,4 +33,4 @@ export default DS.Model.extend(Validations, { order: DS.belongsTo('order', { async: true }), selections: DS.hasMany('order-selection', { async: true }), type: DS.attr('string') -}); \ No newline at end of file +}); diff --git a/tests/dummy/app/models/order-selection-question.js b/tests/dummy/app/models/order-selection-question.js index e8d5dc41..f3604bce 100644 --- a/tests/dummy/app/models/order-selection-question.js +++ b/tests/dummy/app/models/order-selection-question.js @@ -6,21 +6,21 @@ from 'ember-cp-validations'; const Validations = buildValidations({ order: { - description: "Order", + description: 'Order', validators: [ validator('ds-error'), validator('presence', true) ] }, selection: { - description: "Order Selection", + description: 'Order Selection', validators: [ validator('ds-error'), validator('presence', true) ] }, text: { - description: "Question Text", + description: 'Question Text', validators: [ validator('ds-error'), validator('presence', true) diff --git a/tests/dummy/app/models/order-selection.js b/tests/dummy/app/models/order-selection.js index 3f773a2a..06926f49 100644 --- a/tests/dummy/app/models/order-selection.js +++ b/tests/dummy/app/models/order-selection.js @@ -6,7 +6,7 @@ from 'ember-cp-validations'; const Validations = buildValidations({ quantity: { - description: "Quantity", + description: 'Quantity', validators: [ validator('ds-error'), validator('number', { @@ -15,7 +15,7 @@ const Validations = buildValidations({ ] }, order: { - description: "Order", + description: 'Order', validators: [ validator('ds-error'), validator('belongs-to'), @@ -23,14 +23,14 @@ const Validations = buildValidations({ ] }, line: { - description: "Order Line", + description: 'Order Line', validators: [ validator('ds-error'), validator('presence', true) ] }, questions: { - description: "Order Selection Questions", + description: 'Order Selection Questions', validators: [ validator('ds-error'), validator('has-many'), diff --git a/tests/dummy/app/models/order.js b/tests/dummy/app/models/order.js index ebbae133..6e8a0473 100644 --- a/tests/dummy/app/models/order.js +++ b/tests/dummy/app/models/order.js @@ -6,14 +6,14 @@ from 'ember-cp-validations'; const Validations = buildValidations({ source: { - description: "Order Source", + description: 'Order Source', validators: [ validator('ds-error'), validator('presence', true) ] }, lines: { - description: "Order Lines", + description: 'Order Lines', validators: [ validator('ds-error'), validator('has-many'), diff --git a/tests/dummy/app/models/signup.js b/tests/dummy/app/models/signup.js index 75ebafbf..8f05855d 100644 --- a/tests/dummy/app/models/signup.js +++ b/tests/dummy/app/models/signup.js @@ -1,19 +1,14 @@ import DS from 'ember-data'; -import { - validator, buildValidations -} -from 'ember-cp-validations'; - +import { validator, buildValidations } from 'ember-cp-validations'; let Validations = buildValidations({ name: validator('presence', true), acceptTerms: validator((value) => { return value || 'You must accept the terms.'; - }), + }) }); - export default DS.Model.extend(Validations, { name: DS.attr('string', { defaultValue: '' }), - acceptTerms: DS.attr('boolean', { defaultValue: false }), + acceptTerms: DS.attr('boolean', { defaultValue: false }) }); diff --git a/tests/dummy/app/models/user-detail.js b/tests/dummy/app/models/user-detail.js index 69708fc5..12441aac 100644 --- a/tests/dummy/app/models/user-detail.js +++ b/tests/dummy/app/models/user-detail.js @@ -3,7 +3,7 @@ * Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms. */ - // BEGIN-SNIPPET user-detail-model +// BEGIN-SNIPPET user-detail-model import Ember from 'ember'; import DS from 'ember-data'; import moment from 'moment'; @@ -12,7 +12,7 @@ import { validator, buildValidations } from 'ember-cp-validations'; const { computed } = Ember; const { attr } = DS; -var Validations = buildValidations({ +const Validations = buildValidations({ firstName: validator('presence', true), lastName: validator('presence', true), dob: { @@ -25,7 +25,7 @@ var Validations = buildValidations({ return moment().subtract(120, 'years').format('M/D/YYYY'); }).volatile(), format: 'M/D/YYYY', - message: function(type, value /*, context */) { + message(type, value /*, context */) { if (type === 'before') { return 'Are you from the future?'; } diff --git a/tests/dummy/app/models/user.js b/tests/dummy/app/models/user.js index 1d12de8f..5d703108 100644 --- a/tests/dummy/app/models/user.js +++ b/tests/dummy/app/models/user.js @@ -3,13 +3,13 @@ * Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms. */ - // BEGIN-SNIPPET user-model +// BEGIN-SNIPPET user-model import DS from 'ember-data'; import { validator, buildValidations } from 'ember-cp-validations'; const { attr } = DS; -var Validations = buildValidations({ +const Validations = buildValidations({ username: { description: 'Username', validators: [ diff --git a/tests/dummy/app/styles/app.less b/tests/dummy/app/styles/app.less index 8edc4f20..59025da8 100644 --- a/tests/dummy/app/styles/app.less +++ b/tests/dummy/app/styles/app.less @@ -1,8 +1,8 @@ @accent-color: #f23818; -@import "./navbar.less"; -@import "./form.less"; -@import "./code-snippet.less"; +@import './navbar.less'; +@import './form.less'; +@import './code-snippet.less'; body, html { diff --git a/tests/dummy/app/styles/form.less b/tests/dummy/app/styles/form.less index c53ca9d2..1333e426 100644 --- a/tests/dummy/app/styles/form.less +++ b/tests/dummy/app/styles/form.less @@ -23,7 +23,7 @@ -webkti-box-sizing: border-box; box-sizing: border-box; font-size: 14px; - font-wieght: 400; + font-weight: 400; -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; transition: all 0.3s linear 0s; @@ -108,7 +108,7 @@ input.form-control:focus { border: 0; border-radius: 3px; color: #ffffff; - font-family: "Roboto"; + font-family: 'Roboto'; font-size: 14px; font-weight: 400; -webkit-font-smoothing: antialiased; diff --git a/tests/dummy/app/templates/application.hbs b/tests/dummy/app/templates/application.hbs index b540178a..e5075e03 100644 --- a/tests/dummy/app/templates/application.hbs +++ b/tests/dummy/app/templates/application.hbs @@ -1,38 +1,38 @@ - -
+
{{outlet}}
diff --git a/tests/dummy/app/templates/components/validated-input.hbs b/tests/dummy/app/templates/components/validated-input.hbs index 8dbcfa96..006d39c2 100644 --- a/tests/dummy/app/templates/components/validated-input.hbs +++ b/tests/dummy/app/templates/components/validated-input.hbs @@ -1,19 +1,19 @@ {{!-- BEGIN-SNIPPET validated-input --}} -
- {{input type=type value=value placeholder=placeholder class="form-control" name=valuePath}} +
+ {{input type=type value=value placeholder=placeholder class='form-control' name=valuePath}} {{#if isValid}} - + {{/if}} -
+
{{#if showErrorMessage}} -
+
{{v-get model valuePath 'message'}}
{{/if}} {{#if showWarningMessage}} -
+
{{v-get model valuePath 'warningMessage'}}
{{/if}} diff --git a/tests/dummy/app/templates/index.hbs b/tests/dummy/app/templates/index.hbs index 26311c00..227adbdb 100644 --- a/tests/dummy/app/templates/index.hbs +++ b/tests/dummy/app/templates/index.hbs @@ -1,18 +1,18 @@ -
+
{{#unless isRegistered}} -
+

Create an Account

{{#if showAlert}} -
-
+
+
Please fix all the errors below before continuing.
{{/if}}
-
+
{{#unless showCode}} -