|
| 1 | +"use strict"; |
| 2 | +var __assign = (this && this.__assign) || function () { |
| 3 | + __assign = Object.assign || function(t) { |
| 4 | + for (var s, i = 1, n = arguments.length; i < n; i++) { |
| 5 | + s = arguments[i]; |
| 6 | + for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) |
| 7 | + t[p] = s[p]; |
| 8 | + } |
| 9 | + return t; |
| 10 | + }; |
| 11 | + return __assign.apply(this, arguments); |
| 12 | +}; |
| 13 | +exports.__esModule = true; |
| 14 | +exports.useValidateObject = exports.useValidate = exports.rulesList = void 0; |
| 15 | +var vue_1 = require("vue"); |
| 16 | +exports.rulesList = { |
| 17 | + /** |
| 18 | + * Check if field is required (is not empty) |
| 19 | + */ |
| 20 | + required: function (value) { return !!value; }, |
| 21 | + /** |
| 22 | + * Check if value is not less than minimal length |
| 23 | + */ |
| 24 | + minLength: function (value, minLength) { return value.length >= minLength; }, |
| 25 | + /** |
| 26 | + * Check if value is not more than max length |
| 27 | + */ |
| 28 | + maxLength: function (value, maxLength) { return value.length <= maxLength; }, |
| 29 | + /** |
| 30 | + * Check if value is not more then max value |
| 31 | + */ |
| 32 | + maxValue: function (value, maxValue) { return !(maxValue < value); }, |
| 33 | + /** |
| 34 | + * Check if value is not less then min value |
| 35 | + */ |
| 36 | + minValue: function (value, minValue) { return !(minValue > value); }, |
| 37 | + /** |
| 38 | + * Test value for email by regular expression |
| 39 | + */ |
| 40 | + email: function (value) { return /^[a-zA-Z0-9._-]+@[a-zA-Z_]+?\.?[a-z]+\.[a-zA-Z]{2,10}$/.test(value); }, |
| 41 | + /** |
| 42 | + * Check if value is a number |
| 43 | + */ |
| 44 | + numeric: function (value) { return !isNaN(Number(value)); }, |
| 45 | + /** |
| 46 | + * Check if value is alphabetic |
| 47 | + */ |
| 48 | + alpha: function (value) { return /^[a-zA-Z ]+$/.test(value); }, |
| 49 | + /** |
| 50 | + * Check if value is alphabetic or numeric |
| 51 | + */ |
| 52 | + alphaNum: function (value) { return /^[a-zA-Z0-9 ]+$/.test(value); }, |
| 53 | + /** |
| 54 | + * Check if value is not less then some number and not more then another |
| 55 | + */ |
| 56 | + between: function (value, between) { return value >= between[0] && value <= between[1]; }, |
| 57 | + /** |
| 58 | + * Check if a value is ip address |
| 59 | + */ |
| 60 | + ipAddress: function (value) { return /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/.test(value); } |
| 61 | +}; |
| 62 | +/** |
| 63 | + * Validation hook for single variable |
| 64 | + */ |
| 65 | +function useValidate(model, rules) { |
| 66 | + var localRules = {}; |
| 67 | + // Get input rules from global rules list and copy them to local rules |
| 68 | + rules.forEach(function (rule) { |
| 69 | + if (typeof rule === 'string') { |
| 70 | + localRules[rule] = exports.rulesList[rule]; |
| 71 | + } |
| 72 | + else { |
| 73 | + localRules[rule.type] = { |
| 74 | + type: exports.rulesList[rule.type], |
| 75 | + rule: rule.value |
| 76 | + }; |
| 77 | + } |
| 78 | + }); |
| 79 | + // v-model will be bind with this variable |
| 80 | + var value = (0, vue_1.ref)(model); |
| 81 | + // Rules object that returns key as rule name and value as boolean |
| 82 | + var modelRules = (0, vue_1.reactive)({}); |
| 83 | + // Is variable valid or not |
| 84 | + var isValid = (0, vue_1.ref)(false); |
| 85 | + (0, vue_1.watch)(value, function () { |
| 86 | + // Loop through local rules array and run validation functions |
| 87 | + for (var rule in localRules) { |
| 88 | + if (typeof localRules[rule] === 'object') { |
| 89 | + modelRules[rule] = localRules[rule].type(value.value, localRules[rule].rule); |
| 90 | + } |
| 91 | + else { |
| 92 | + modelRules[rule] = localRules[rule](value.value); |
| 93 | + } |
| 94 | + } |
| 95 | + // Check if all rules are valid |
| 96 | + isValid.value = Object.values(modelRules).every(function (rule) { return rule; }); |
| 97 | + }, { immediate: true }); |
| 98 | + return (0, vue_1.reactive)({ |
| 99 | + model: value, |
| 100 | + isValid: isValid, |
| 101 | + modelRules: modelRules |
| 102 | + }); |
| 103 | +} |
| 104 | +exports.useValidate = useValidate; |
| 105 | +/** |
| 106 | + * Validation hook for object |
| 107 | + */ |
| 108 | +function useValidateObject(model) { |
| 109 | + // Keys of model object |
| 110 | + var modelKeys = Object.keys(model); |
| 111 | + var validatedFields = (0, vue_1.reactive)({}); |
| 112 | + for (var keyIndex = 0; keyIndex < modelKeys.length; ++keyIndex) { |
| 113 | + var key = modelKeys[keyIndex]; |
| 114 | + validatedFields[key] = useValidate(model[key].model, model[key].rules); |
| 115 | + } |
| 116 | + var cleanObject = (0, vue_1.ref)({}); |
| 117 | + // Clean model object returns without validations |
| 118 | + var clean = (0, vue_1.computed)(function () { |
| 119 | + Object.keys(validatedFields).forEach(function (key) { |
| 120 | + cleanObject.value[key] = validatedFields[key].model; |
| 121 | + }); |
| 122 | + return cleanObject.value; |
| 123 | + }); |
| 124 | + var isValid = (0, vue_1.computed)(function () { return modelKeys.every(function (key) { return validatedFields[key].isValid; }); }); |
| 125 | + return (0, vue_1.reactive)(__assign(__assign({}, validatedFields), { __isValid: isValid, __clean: clean })); |
| 126 | +} |
| 127 | +exports.useValidateObject = useValidateObject; |
0 commit comments