UNPKG

lara-validator

Version:

Validating data based on Laravel validation style

228 lines (194 loc) 7.58 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _index = require('./index'); var _index2 = _interopRequireDefault(_index); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } var RuleMeta = function () { function RuleMeta(data) { _classCallCheck(this, RuleMeta); // origin data this.data = data; // field & path: this.fieldParentPath = []; this.fieldName = null; this.fieldPath = []; this.fieldPathLength = null; this.fieldIsInArray = false; // rule: this.rule = null; this.ruleName = null; this.ruleOptions = null; this.isNullable = false; this.presentOnly = false; // values: this.parentValues = []; } /** * * @param {Boolean} throwError * @returns {Boolean} */ _createClass(RuleMeta, [{ key: 'isInit', value: function isInit() { var throwError = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true; var hasPath = _index2.default.required(this.fieldPath); var hasRule = _index2.default.required(this.ruleName); if (throwError && !(hasPath && hasRule)) { throw Error('RuleMeta has not been initialized, please invoke "setFieldPath" and "setRule"'); } return hasPath && hasRule; } /** * * @param {Array} parentPath * @param {String} fieldName * @returns {RuleMeta} */ }, { key: 'setFieldPath', value: function setFieldPath(parentPath, fieldName) { this.fieldParentPath = parentPath; this.fieldName = fieldName; this.fieldPath = [].concat(_toConsumableArray(this.fieldParentPath), [this.fieldName]); this.fieldPathLength = this.fieldPath.length; this.fieldIsInArray = this.fieldPath.includes('*'); return this; } /** * * @return {RuleMeta} */ }, { key: 'resetRule', value: function resetRule() { this.rule = null; this.ruleName = null; this.ruleOptions = null; this.isNullable = false; this.presentOnly = false; return this; } /** * * @param {String} rule * @param {Boolean} isNullable * @param {Boolean} presentOnly * @param {Function} ruleOptionProcessCallback * @returns {RuleMeta} */ }, { key: 'setRule', value: function setRule(rule) { var isNullable = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false; var presentOnly = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false; var ruleOptionProcessCallback = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : undefined; var ruleInfo = rule.split(':'); this.rule = rule; this.ruleName = ruleInfo[0]; this.ruleOptions = ruleOptionProcessCallback ? ruleOptionProcessCallback(ruleInfo[1]) : ruleInfo[1]; this.isNullable = isNullable; this.presentOnly = presentOnly; return this; } /** * * @param {Array|undefined} values * @returns {RuleMeta} */ }, { key: 'setParentValues', value: function setParentValues() { var values = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : undefined; this.parentValues = values || RuleMeta.getValuesByPaths(this.fieldParentPath, this.data); return this; } /** * * @param {*} value * @return {boolean} */ }, { key: 'needToValidate', value: function needToValidate(value) { if (this.presentOnly && (value === null || value === undefined)) { return false; } else if (this.isNullable && value === null) { return false; } return true; } }]); return RuleMeta; }(); /** * * @param {Array} path * @param {Object} data * @param {Boolean} isRequired * @return {undefined[]} */ exports.default = RuleMeta; RuleMeta.getValuesByPaths = function (path, data) { var isRequired = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true; try { var values = [{ path: [], value: data }]; path.forEach(function (nodeName) { var nextIteratorValues = []; var isArrayNode = nodeName === '*'; values.forEach(function (value) { if (!value.value && !isRequired) { nextIteratorValues.push({ path: [].concat(_toConsumableArray(value.path), [nodeName]), value: undefined }); } else { if (isArrayNode) { value.value.forEach(function (element, index) { nextIteratorValues.push({ path: [].concat(_toConsumableArray(value.path), [index.toString()]), value: element }); }); } else { nextIteratorValues.push({ path: [].concat(_toConsumableArray(value.path), [nodeName]), value: value.value[nodeName] }); } } }); values = nextIteratorValues; }); return values; } catch (error) { if (!isRequired) { return [undefined]; } throw error; } }; /** * * @param {string} stringPath0 * @param {string} stringPath1 * @return {string} */ RuleMeta.pathType = function (stringPath0, stringPath1) { var path0IsInArray = stringPath0.includes('*'); var path1IsInArray = stringPath1.includes('*'); if (!path0IsInArray && !path1IsInArray) { return 'ONE_COMPARE_WITH_ONE'; } else if (!path0IsInArray && path1IsInArray) { return 'ONE_COMPARE_WITH_MULTI'; } else if (path0IsInArray && !path1IsInArray) { return 'MULTI_COMPARE_WITH_ONE'; } else if (path0IsInArray && path1IsInArray) { return 'MULTI_COMPARE_WITH_MULTI'; } };