Last active
July 28, 2019 18:47
-
-
Save bayareawebpro/3cf419f7e33dc76bb61aa3e19b83fc47 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| data: ()=>({ | |
| validator: new Validator | |
| }), | |
| /** | |
| * Validator API | |
| */ | |
| this.validator.clear() | |
| this.validator.all() | |
| this.validator.has('myField') | |
| this.validator.get('myField', ['Please enter a default value.', 'Also do something else.']) | |
| this.validator.first('myField', 'Please enter a default value.') | |
| this.validator.in(['email', 'password']) | |
| this.validator.firstEntry | |
| this.validator.isInvalid | |
| this.validator.sync(error) | |
| this.validator.setMessage('Invalid Request.') | |
| this.validator.setErrors({ | |
| myField: ["Min length 5 chars."] | |
| }) | |
| export default class Validator{ | |
| constructor(){ | |
| this.message = null | |
| this.messageBag = {} | |
| } | |
| /** | |
| * (Method) clearErrors | |
| * @return void | |
| */ | |
| clear() { | |
| this.setErrors({}) | |
| } | |
| /** | |
| * All Errors | |
| * @return {Object} | |
| */ | |
| all() { | |
| return this.messageBag | |
| } | |
| /** | |
| * Sync State | |
| * @param error {AxiosError} | |
| */ | |
| sync(error) { | |
| if( | |
| error.response && | |
| error.response.data && | |
| error.response.data.errors | |
| ){ | |
| const errors = error.response.data.errors | |
| for (let key of Object.keys(errors)) { | |
| errors[key].map((error, index) => { | |
| //Strip Dot Syntax from Field Names in Messages (for nested array fields) | |
| errors[key][index] = error.replace( | |
| key.replace(/_/g, ' '), | |
| key.replace(/[._]/g, ' ') | |
| ) | |
| }) | |
| } | |
| this.messageBag = errors | |
| this.setMessage(error.response.data.message || error.message) | |
| } | |
| return this | |
| } | |
| /** | |
| * Set Response Message | |
| * @param message @type {String} | |
| */ | |
| setMessage(message = null){ | |
| this.message = message | |
| } | |
| /** | |
| * Set Errors | |
| * @param messageBag | |
| */ | |
| setErrors(messageBag = {}){ | |
| this.messageBag = messageBag | |
| } | |
| /** | |
| * (Conditional) hasErrors | |
| * @param field @type {String} | |
| * @return {boolean} | |
| */ | |
| has(field = null) { | |
| return field ? this.messageBag.hasOwnProperty(field) : false | |
| } | |
| /** | |
| * (Method) getErrors | |
| * @param field @type {String} | |
| * @param fallback @type {*} | |
| * @return {*} | |
| */ | |
| get(field, fallback = []) { | |
| return this.messageBag.hasOwnProperty(field) ? this.messageBag[field] : fallback ? fallback : null | |
| } | |
| /** | |
| * (Method) getError | |
| * @param field @type {String} | |
| * @param fallback @type {*} | |
| * @return {String|null} | |
| */ | |
| first(field = null, fallback = false) { | |
| return this.messageBag.hasOwnProperty(field) ? this.messageBag[field][0] : fallback ? fallback : null | |
| } | |
| /** | |
| * (Conditional) hasErrorsIn | |
| * @param fields @type {Array} | |
| * @return {Boolean} | |
| */ | |
| in(...fields) { | |
| let hasErrors = false | |
| fields.some((field) => { | |
| hasErrors = this.messageBag.hasOwnProperty(field) | |
| return hasErrors | |
| }) | |
| return hasErrors | |
| } | |
| /** | |
| * (Method) firstEntry | |
| * @return {String|null} | |
| */ | |
| get firstEntry() { | |
| const errors = Object.values(this.messageBag).flat(2); | |
| return errors.length > 0 ? errors[0] : null | |
| } | |
| /** | |
| * In Invalid | |
| * @return {boolean} | |
| */ | |
| get isInvalid(){ | |
| return Object.entries(this.messageBag).length > 0 | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment