123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473 |
- import { unref } from 'vue-demi';
- function ownKeys(object, enumerableOnly) {
- var keys = Object.keys(object);
- if (Object.getOwnPropertySymbols) {
- var symbols = Object.getOwnPropertySymbols(object);
- enumerableOnly && (symbols = symbols.filter(function (sym) {
- return Object.getOwnPropertyDescriptor(object, sym).enumerable;
- })), keys.push.apply(keys, symbols);
- }
- return keys;
- }
- function _objectSpread2(target) {
- for (var i = 1; i < arguments.length; i++) {
- var source = null != arguments[i] ? arguments[i] : {};
- i % 2 ? ownKeys(Object(source), !0).forEach(function (key) {
- _defineProperty(target, key, source[key]);
- }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) {
- Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
- });
- }
- return target;
- }
- function _defineProperty(obj, key, value) {
- if (key in obj) {
- Object.defineProperty(obj, key, {
- value: value,
- enumerable: true,
- configurable: true,
- writable: true
- });
- } else {
- obj[key] = value;
- }
- return obj;
- }
- function isFunction(val) {
- return typeof val === 'function';
- }
- function isObject(o) {
- return o !== null && typeof o === 'object' && !Array.isArray(o);
- }
- function normalizeValidatorObject(validator) {
- return isFunction(validator.$validator) ? _objectSpread2({}, validator) : {
- $validator: validator
- };
- }
- function isPromise(object) {
- return isObject(object) && isFunction(object.then);
- }
- function unwrapValidatorResponse(result) {
- if (typeof result === 'object') return result.$valid;
- return result;
- }
- function unwrapNormalizedValidator(validator) {
- return validator.$validator || validator;
- }
- function withParams($params, $validator) {
- if (!isObject($params)) throw new Error(`[@vuelidate/validators]: First parameter to "withParams" should be an object, provided ${typeof $params}`);
- if (!isObject($validator) && !isFunction($validator)) throw new Error(`[@vuelidate/validators]: Validator must be a function or object with $validator parameter`);
- const validatorObj = normalizeValidatorObject($validator);
- validatorObj.$params = _objectSpread2(_objectSpread2({}, validatorObj.$params || {}), $params);
- return validatorObj;
- }
- function withMessage($message, $validator) {
- if (!isFunction($message) && typeof unref($message) !== 'string') throw new Error(`[@vuelidate/validators]: First parameter to "withMessage" should be string or a function returning a string, provided ${typeof $message}`);
- if (!isObject($validator) && !isFunction($validator)) throw new Error(`[@vuelidate/validators]: Validator must be a function or object with $validator parameter`);
- const validatorObj = normalizeValidatorObject($validator);
- validatorObj.$message = $message;
- return validatorObj;
- }
- function withAsync($validator) {
- let $watchTargets = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
- const validatorObj = normalizeValidatorObject($validator);
- return _objectSpread2(_objectSpread2({}, validatorObj), {}, {
- $async: true,
- $watchTargets
- });
- }
- function forEach(validators) {
- return {
- $validator(collection) {
- for (var _len = arguments.length, others = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
- others[_key - 1] = arguments[_key];
- }
- return unref(collection).reduce((previous, collectionItem, index) => {
- const collectionEntryResult = Object.entries(collectionItem).reduce((all, _ref) => {
- let [property, $model] = _ref;
- const innerValidators = validators[property] || {};
- const propertyResult = Object.entries(innerValidators).reduce((all, _ref2) => {
- let [validatorName, currentValidator] = _ref2;
- const validatorFunction = unwrapNormalizedValidator(currentValidator);
- const $response = validatorFunction.call(this, $model, collectionItem, index, ...others);
- const $valid = unwrapValidatorResponse($response);
- all.$data[validatorName] = $response;
- all.$data.$invalid = !$valid || !!all.$data.$invalid;
- all.$data.$error = all.$data.$invalid;
- if (!$valid) {
- let $message = currentValidator.$message || '';
- const $params = currentValidator.$params || {};
- if (typeof $message === 'function') {
- $message = $message({
- $pending: false,
- $invalid: !$valid,
- $params,
- $model,
- $response
- });
- }
- all.$errors.push({
- $property: property,
- $message,
- $params,
- $response,
- $model,
- $pending: false,
- $validator: validatorName
- });
- }
- return {
- $valid: all.$valid && $valid,
- $data: all.$data,
- $errors: all.$errors
- };
- }, {
- $valid: true,
- $data: {},
- $errors: []
- });
- all.$data[property] = propertyResult.$data;
- all.$errors[property] = propertyResult.$errors;
- return {
- $valid: all.$valid && propertyResult.$valid,
- $data: all.$data,
- $errors: all.$errors
- };
- }, {
- $valid: true,
- $data: {},
- $errors: {}
- });
- return {
- $valid: previous.$valid && collectionEntryResult.$valid,
- $data: previous.$data.concat(collectionEntryResult.$data),
- $errors: previous.$errors.concat(collectionEntryResult.$errors)
- };
- }, {
- $valid: true,
- $data: [],
- $errors: []
- });
- },
- $message: _ref3 => {
- let {
- $response
- } = _ref3;
- return $response ? $response.$errors.map(context => {
- return Object.values(context).map(errors => errors.map(error => error.$message)).reduce((a, b) => a.concat(b), []);
- }) : [];
- }
- };
- }
- const req = value => {
- value = unref(value);
- if (Array.isArray(value)) return !!value.length;
- if (value === undefined || value === null) {
- return false;
- }
- if (value === false) {
- return true;
- }
- if (value instanceof Date) {
- return !isNaN(value.getTime());
- }
- if (typeof value === 'object') {
- for (let _ in value) return true;
- return false;
- }
- return !!String(value).length;
- };
- const len = value => {
- value = unref(value);
- if (Array.isArray(value)) return value.length;
- if (typeof value === 'object') {
- return Object.keys(value).length;
- }
- return String(value).length;
- };
- function regex() {
- for (var _len = arguments.length, expr = new Array(_len), _key = 0; _key < _len; _key++) {
- expr[_key] = arguments[_key];
- }
- return value => {
- value = unref(value);
- return !req(value) || expr.every(reg => {
- reg.lastIndex = 0;
- return reg.test(value);
- });
- };
- }
- var common = /*#__PURE__*/Object.freeze({
- __proto__: null,
- forEach: forEach,
- len: len,
- normalizeValidatorObject: normalizeValidatorObject,
- regex: regex,
- req: req,
- unwrap: unref,
- unwrapNormalizedValidator: unwrapNormalizedValidator,
- unwrapValidatorResponse: unwrapValidatorResponse,
- withAsync: withAsync,
- withMessage: withMessage,
- withParams: withParams
- });
- var alpha = regex(/^[a-zA-Z]*$/);
- var alphaNum = regex(/^[a-zA-Z0-9]*$/);
- var numeric = regex(/^\d*(\.\d+)?$/);
- function between (min, max) {
- return value => !req(value) || (!/\s/.test(value) || value instanceof Date) && +unref(min) <= +value && +unref(max) >= +value;
- }
- const emailRegex = /^(?:[A-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[A-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9]{2,}(?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])$/i;
- var email = regex(emailRegex);
- function ipAddress (value) {
- if (!req(value)) {
- return true;
- }
- if (typeof value !== 'string') {
- return false;
- }
- const nibbles = value.split('.');
- return nibbles.length === 4 && nibbles.every(nibbleValid);
- }
- const nibbleValid = nibble => {
- if (nibble.length > 3 || nibble.length === 0) {
- return false;
- }
- if (nibble[0] === '0' && nibble !== '0') {
- return false;
- }
- if (!nibble.match(/^\d+$/)) {
- return false;
- }
- const numeric = +nibble | 0;
- return numeric >= 0 && numeric <= 255;
- };
- function macAddress () {
- let separator = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : ':';
- return value => {
- separator = unref(separator);
- if (!req(value)) {
- return true;
- }
- if (typeof value !== 'string') {
- return false;
- }
- const parts = typeof separator === 'string' && separator !== '' ? value.split(separator) : value.length === 12 || value.length === 16 ? value.match(/.{2}/g) : null;
- return parts !== null && (parts.length === 6 || parts.length === 8) && parts.every(hexValid);
- };
- }
- const hexValid = hex => hex.toLowerCase().match(/^[0-9a-f]{2}$/);
- function maxLength (length) {
- return value => !req(value) || len(value) <= unref(length);
- }
- function minLength (length) {
- return value => !req(value) || len(value) >= unref(length);
- }
- function required (value) {
- if (typeof value === 'string') {
- value = value.trim();
- }
- return req(value);
- }
- const validate$1 = (prop, val) => prop ? req(typeof val === 'string' ? val.trim() : val) : true;
- function requiredIf(propOrFunction) {
- return function (value, parentVM) {
- if (typeof propOrFunction !== 'function') {
- return validate$1(unref(propOrFunction), value);
- }
- const result = propOrFunction.call(this, value, parentVM);
- return validate$1(result, value);
- };
- }
- const validate = (prop, val) => !prop ? req(typeof val === 'string' ? val.trim() : val) : true;
- function requiredUnless(propOrFunction) {
- return function (value, parentVM) {
- if (typeof propOrFunction !== 'function') {
- return validate(unref(propOrFunction), value);
- }
- const result = propOrFunction.call(this, value, parentVM);
- return validate(result, value);
- };
- }
- function sameAs (equalTo) {
- return value => unref(value) === unref(equalTo);
- }
- const urlRegex = /^(?:(?:(?:https?|ftp):)?\/\/)(?:\S+(?::\S*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z0-9\u00a1-\uffff][a-z0-9\u00a1-\uffff_-]{0,62})?[a-z0-9\u00a1-\uffff]\.)+(?:[a-z\u00a1-\uffff]{2,}\.?))(?::\d{2,5})?(?:[/?#]\S*)?$/i;
- var url = regex(urlRegex);
- function syncOr(validators) {
- return function () {
- for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
- args[_key] = arguments[_key];
- }
- return validators.reduce((valid, fn) => {
- if (unwrapValidatorResponse(valid)) return valid;
- return unwrapNormalizedValidator(fn).apply(this, args);
- }, false);
- };
- }
- function asyncOr(validators) {
- return function () {
- for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
- args[_key2] = arguments[_key2];
- }
- return validators.reduce(async (valid, fn) => {
- const r = await valid;
- if (unwrapValidatorResponse(r)) return r;
- return unwrapNormalizedValidator(fn).apply(this, args);
- }, Promise.resolve(false));
- };
- }
- function or() {
- for (var _len3 = arguments.length, validators = new Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
- validators[_key3] = arguments[_key3];
- }
- const $async = validators.some(v => v.$async);
- const $watchTargets = validators.reduce((all, v) => {
- if (!v.$watchTargets) return all;
- return all.concat(v.$watchTargets);
- }, []);
- let $validator = () => false;
- if (validators.length) $validator = $async ? asyncOr(validators) : syncOr(validators);
- return {
- $async,
- $validator,
- $watchTargets
- };
- }
- function syncAnd(validators) {
- return function () {
- for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
- args[_key] = arguments[_key];
- }
- return validators.reduce((valid, fn) => {
- if (!unwrapValidatorResponse(valid)) return valid;
- return unwrapNormalizedValidator(fn).apply(this, args);
- }, true);
- };
- }
- function asyncAnd(validators) {
- return function () {
- for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
- args[_key2] = arguments[_key2];
- }
- return validators.reduce(async (valid, fn) => {
- const r = await valid;
- if (!unwrapValidatorResponse(r)) return r;
- return unwrapNormalizedValidator(fn).apply(this, args);
- }, Promise.resolve(true));
- };
- }
- function and() {
- for (var _len3 = arguments.length, validators = new Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
- validators[_key3] = arguments[_key3];
- }
- const $async = validators.some(v => v.$async);
- const $watchTargets = validators.reduce((all, v) => {
- if (!v.$watchTargets) return all;
- return all.concat(v.$watchTargets);
- }, []);
- let $validator = () => false;
- if (validators.length) $validator = $async ? asyncAnd(validators) : syncAnd(validators);
- return {
- $async,
- $validator,
- $watchTargets
- };
- }
- function not (validator) {
- return function (value, vm) {
- if (!req(value)) return true;
- const response = unwrapNormalizedValidator(validator).call(this, value, vm);
- if (!isPromise(response)) return !unwrapValidatorResponse(response);
- return response.then(r => !unwrapValidatorResponse(r));
- };
- }
- function minValue (min) {
- return value => !req(value) || (!/\s/.test(value) || value instanceof Date) && +value >= +unref(min);
- }
- function maxValue (max) {
- return value => !req(value) || (!/\s/.test(value) || value instanceof Date) && +value <= +unref(max);
- }
- var integer = regex(/(^[0-9]*$)|(^-[0-9]+$)/);
- var decimal = regex(/^[-]?\d*(\.\d+)?$/);
- export { alpha, alphaNum, and, between, decimal, email, common as helpers, integer, ipAddress, macAddress, maxLength, maxValue, minLength, minValue, not, numeric, or, required, requiredIf, requiredUnless, sameAs, url };
|