UNPKG

laravel-jstools

Version:

JS tools for building front-side of Laravel applications

108 lines (107 loc) 4.73 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ValidateService = void 0; const laravel_jstools_di_1 = require("laravel-jstools-di"); const helpers_1 = require("../../scripts/helpers"); const ValidateError_1 = require("./ValidateError"); class ValidateService extends laravel_jstools_di_1.Service { validate(data, ruleList) { const errorList = {}; for (const field of Object.keys(ruleList)) { ruleList[field].split('|').forEach((item, i, arr) => { const ruleData = item.split(':'); const rule = ruleData[0]; const paramList = ruleData[1]; const validationResult = this.check(ruleList[field], data, field, rule, paramList); if (validationResult !== true) { if (!errorList.hasOwnProperty(field)) { errorList[field] = {}; } errorList[field][rule] = validationResult; } }); } return (0, helpers_1.isEmpty)(errorList) ? true : errorList; } check(ruleList, data, field, rule, paramList) { return this.getRuleChecker(rule)(ruleList, data, field, paramList); } getRuleChecker(rule) { switch (rule) { case 'confirmed': return this.checkConfirmed; case 'email': return this.checkEmail; case 'integer': return this.checkInteger; case 'max': return this.checkMax; case 'min': return this.checkMin; case 'notIn': return this.checkNotIn; case 'nullable': return this.checkNullable; case 'numeric': return this.checkNumeric; case 'phone': return this.checkPhone; case 'required': return this.checkRequired; } return this.fakeCheck; } fakeCheck(ruleList, data, field, paramList) { return true; } checkConfirmed(ruleList, data, field, paramList) { const value = data[field]; const valueConfirmation = data[`${field}_confirmation`]; return (value && value === valueConfirmation) || new ValidateError_1.ValidateError(field, 'confirmed'); } checkEmail(ruleList, data, field, paramList) { const value = data[field]; if (ruleList.split('|').includes('nullable') && !value) { return true; } return (value && /^\w+([.-]?\w+)*@\w+([.-]?\w+)*(\.\w{2,3})+$/.test(value)) || new ValidateError_1.ValidateError(field, 'email'); } checkInteger(ruleList, data, field, paramList) { const value = data[field]; return (!!value && Number.isInteger(value)) || new ValidateError_1.ValidateError(field, 'integer'); } checkMax(ruleList, data, field, paramList) { const value = data[field]; const type = (0, helpers_1.isNumeric)(value) ? 'numeric' : 'string'; // TODO omadonex: file, array return (value && value.length <= paramList) || new ValidateError_1.ValidateError(field, `max.${type}`, { max: paramList }); } checkMin(ruleList, data, field, paramList) { const value = data[field]; const type = (0, helpers_1.isNumeric)(value) ? 'numeric' : 'string'; // TODO omadonex: file, array return (value && value.length >= paramList) || new ValidateError_1.ValidateError(field, `min.${type}`, { min: paramList }); } checkNotIn(ruleList, data, field, paramList) { const value = data[field]; const valueForCheck = data[paramList]; return (value && value !== valueForCheck) || new ValidateError_1.ValidateError(field, 'not_in'); } checkNullable(ruleList, data, field, paramList) { return true; } checkNumeric(ruleList, data, field, paramList) { const value = data[field]; return (!!value && (0, helpers_1.isNumeric)(value)) || new ValidateError_1.ValidateError(field, 'numeric'); } checkPhone(ruleList, data, field, paramList) { const value = data[field]; if (ruleList.split('|').includes('nullable') && !value) { return true; } return (value && /^[0-9]{10}$/.test(value)) || new ValidateError_1.ValidateError(field, 'phone'); } checkRequired(ruleList, data, field, paramList) { const value = data[field]; return !!value || new ValidateError_1.ValidateError(field, 'required'); } } exports.ValidateService = ValidateService;