-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* adds model-validations decorator and validators util * converts key-mixin to decorator * updates models to use validations decorator instead of ember-cp-validations * updates invocation of model validations * removes ember-cp-validations * reverts secret-v2 model updates * adds initials to TODO comment
- Loading branch information
Showing
15 changed files
with
292 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* eslint-disable no-console */ | ||
import validators from 'vault/utils/validators'; | ||
|
||
export function withModelValidations(validations) { | ||
return function decorator(SuperClass) { | ||
return class ModelValidations extends SuperClass { | ||
static _validations; | ||
|
||
constructor() { | ||
super(...arguments); | ||
if (!validations || typeof validations !== 'object') { | ||
throw new Error('Validations object must be provided to constructor for setup'); | ||
} | ||
this._validations = validations; | ||
} | ||
|
||
validate() { | ||
let isValid = true; | ||
const state = {}; | ||
|
||
for (const key in this._validations) { | ||
const rules = this._validations[key]; | ||
|
||
if (!Array.isArray(rules)) { | ||
console.error( | ||
`Must provide validations as an array for property "${key}" on ${this.modelName} model` | ||
); | ||
continue; | ||
} | ||
|
||
state[key] = { errors: [] }; | ||
|
||
for (const rule of rules) { | ||
const { type, options, message } = rule; | ||
if (!validators[type]) { | ||
console.error( | ||
`Validator type: "${type}" not found. Available validators: ${Object.keys(validators).join( | ||
', ' | ||
)}` | ||
); | ||
continue; | ||
} | ||
if (!validators[type](this[key], options)) { | ||
// consider setting a prop like validationErrors directly on the model | ||
// for now return an errors object | ||
state[key].errors.push(message); | ||
if (isValid) { | ||
isValid = false; | ||
} | ||
} | ||
} | ||
state[key].isValid = !state[key].errors.length; | ||
} | ||
return { isValid, state }; | ||
} | ||
}; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { isPresent } from '@ember/utils'; | ||
|
||
export const presence = (value) => isPresent(value); | ||
|
||
export const length = (value, { nullable = false, min, max } = {}) => { | ||
let isValid = nullable; | ||
if (typeof value === 'string') { | ||
const underMin = min && value.length < min; | ||
const overMax = max && value.length > max; | ||
isValid = underMin || overMax ? false : true; | ||
} | ||
return isValid; | ||
}; | ||
|
||
export const number = (value, { nullable = false, asString } = {}) => { | ||
if (!value) return nullable; | ||
if (typeof value === 'string' && !asString) { | ||
return false; | ||
} | ||
return !isNaN(value); | ||
}; | ||
|
||
export default { presence, length, number }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.