Skip to content

Commit

Permalink
Ember-Validators (#363)
Browse files Browse the repository at this point in the history
* Use ember validators

* Abstract validate method for ember-validators

* Use provided _type

* Massive code cleanup via ember-suave

* Remove excludedFiles from jscs

* Fix package version
  • Loading branch information
offirgolan authored Oct 3, 2016
1 parent b949d46 commit c5739fd
Show file tree
Hide file tree
Showing 71 changed files with 3,975 additions and 1,205 deletions.
5 changes: 5 additions & 0 deletions .jscsrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"preset": "ember-suave",
"disallowDirectPropertyAccess": false,
"requireCommentsToIncludeAccess": false
}
10 changes: 10 additions & 0 deletions .vscodeignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
dist/**
tmp/**
build/**
cache/**
node_modules/**
bower_components/**
.sass-cache/**
connect.lock/**
coverage/*/**
libpeerconnection.log
14 changes: 14 additions & 0 deletions addon/-private/ember-validator.js
Original file line number Diff line number Diff line change
@@ -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;
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/

import Ember from 'ember';
import ValidationError from './error';
import ValidationError from '../validations/error';
import { isDsModel, isPromise } from '../utils/utils';

const {
Expand All @@ -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();
}
},
Expand All @@ -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;
Expand All @@ -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')
});
Expand All @@ -85,7 +86,7 @@ export default Ember.Object.extend({
return null;
}),

errors: computed('error', function () {
errors: computed('error', function() {
return makeArray(get(this, 'error'));
}),

Expand Down
26 changes: 13 additions & 13 deletions addon/validations/result.js → addon/-private/result.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -14,7 +14,7 @@ const {
isArray,
computed,
setProperties,
getProperties,
getProperties
} = Ember;

const {
Expand Down Expand Up @@ -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(),

/**
Expand Down Expand Up @@ -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']));
Expand Down Expand Up @@ -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);
Expand All @@ -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,
Expand Down Expand Up @@ -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;
});
Expand Down
6 changes: 3 additions & 3 deletions addon/utils/assign.js
Original file line number Diff line number Diff line change
Expand Up @@ -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))) {
Expand Down
4 changes: 2 additions & 2 deletions addon/utils/cycle-breaker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion addon/utils/flatten.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
8 changes: 4 additions & 4 deletions addon/utils/meta-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ 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];
}
}

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;
}
Expand Down
20 changes: 7 additions & 13 deletions addon/utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand All @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down
Loading

0 comments on commit c5739fd

Please sign in to comment.