raw.mjs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. import { unref } from 'vue-demi';
  2. function ownKeys(object, enumerableOnly) {
  3. var keys = Object.keys(object);
  4. if (Object.getOwnPropertySymbols) {
  5. var symbols = Object.getOwnPropertySymbols(object);
  6. enumerableOnly && (symbols = symbols.filter(function (sym) {
  7. return Object.getOwnPropertyDescriptor(object, sym).enumerable;
  8. })), keys.push.apply(keys, symbols);
  9. }
  10. return keys;
  11. }
  12. function _objectSpread2(target) {
  13. for (var i = 1; i < arguments.length; i++) {
  14. var source = null != arguments[i] ? arguments[i] : {};
  15. i % 2 ? ownKeys(Object(source), !0).forEach(function (key) {
  16. _defineProperty(target, key, source[key]);
  17. }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) {
  18. Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
  19. });
  20. }
  21. return target;
  22. }
  23. function _defineProperty(obj, key, value) {
  24. if (key in obj) {
  25. Object.defineProperty(obj, key, {
  26. value: value,
  27. enumerable: true,
  28. configurable: true,
  29. writable: true
  30. });
  31. } else {
  32. obj[key] = value;
  33. }
  34. return obj;
  35. }
  36. function isFunction(val) {
  37. return typeof val === 'function';
  38. }
  39. function isObject(o) {
  40. return o !== null && typeof o === 'object' && !Array.isArray(o);
  41. }
  42. function normalizeValidatorObject(validator) {
  43. return isFunction(validator.$validator) ? _objectSpread2({}, validator) : {
  44. $validator: validator
  45. };
  46. }
  47. function isPromise(object) {
  48. return isObject(object) && isFunction(object.then);
  49. }
  50. function unwrapValidatorResponse(result) {
  51. if (typeof result === 'object') return result.$valid;
  52. return result;
  53. }
  54. function unwrapNormalizedValidator(validator) {
  55. return validator.$validator || validator;
  56. }
  57. function withParams($params, $validator) {
  58. if (!isObject($params)) throw new Error(`[@vuelidate/validators]: First parameter to "withParams" should be an object, provided ${typeof $params}`);
  59. if (!isObject($validator) && !isFunction($validator)) throw new Error(`[@vuelidate/validators]: Validator must be a function or object with $validator parameter`);
  60. const validatorObj = normalizeValidatorObject($validator);
  61. validatorObj.$params = _objectSpread2(_objectSpread2({}, validatorObj.$params || {}), $params);
  62. return validatorObj;
  63. }
  64. function withMessage($message, $validator) {
  65. 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}`);
  66. if (!isObject($validator) && !isFunction($validator)) throw new Error(`[@vuelidate/validators]: Validator must be a function or object with $validator parameter`);
  67. const validatorObj = normalizeValidatorObject($validator);
  68. validatorObj.$message = $message;
  69. return validatorObj;
  70. }
  71. function withAsync($validator) {
  72. let $watchTargets = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
  73. const validatorObj = normalizeValidatorObject($validator);
  74. return _objectSpread2(_objectSpread2({}, validatorObj), {}, {
  75. $async: true,
  76. $watchTargets
  77. });
  78. }
  79. function forEach(validators) {
  80. return {
  81. $validator(collection) {
  82. for (var _len = arguments.length, others = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
  83. others[_key - 1] = arguments[_key];
  84. }
  85. return unref(collection).reduce((previous, collectionItem, index) => {
  86. const collectionEntryResult = Object.entries(collectionItem).reduce((all, _ref) => {
  87. let [property, $model] = _ref;
  88. const innerValidators = validators[property] || {};
  89. const propertyResult = Object.entries(innerValidators).reduce((all, _ref2) => {
  90. let [validatorName, currentValidator] = _ref2;
  91. const validatorFunction = unwrapNormalizedValidator(currentValidator);
  92. const $response = validatorFunction.call(this, $model, collectionItem, index, ...others);
  93. const $valid = unwrapValidatorResponse($response);
  94. all.$data[validatorName] = $response;
  95. all.$data.$invalid = !$valid || !!all.$data.$invalid;
  96. all.$data.$error = all.$data.$invalid;
  97. if (!$valid) {
  98. let $message = currentValidator.$message || '';
  99. const $params = currentValidator.$params || {};
  100. if (typeof $message === 'function') {
  101. $message = $message({
  102. $pending: false,
  103. $invalid: !$valid,
  104. $params,
  105. $model,
  106. $response
  107. });
  108. }
  109. all.$errors.push({
  110. $property: property,
  111. $message,
  112. $params,
  113. $response,
  114. $model,
  115. $pending: false,
  116. $validator: validatorName
  117. });
  118. }
  119. return {
  120. $valid: all.$valid && $valid,
  121. $data: all.$data,
  122. $errors: all.$errors
  123. };
  124. }, {
  125. $valid: true,
  126. $data: {},
  127. $errors: []
  128. });
  129. all.$data[property] = propertyResult.$data;
  130. all.$errors[property] = propertyResult.$errors;
  131. return {
  132. $valid: all.$valid && propertyResult.$valid,
  133. $data: all.$data,
  134. $errors: all.$errors
  135. };
  136. }, {
  137. $valid: true,
  138. $data: {},
  139. $errors: {}
  140. });
  141. return {
  142. $valid: previous.$valid && collectionEntryResult.$valid,
  143. $data: previous.$data.concat(collectionEntryResult.$data),
  144. $errors: previous.$errors.concat(collectionEntryResult.$errors)
  145. };
  146. }, {
  147. $valid: true,
  148. $data: [],
  149. $errors: []
  150. });
  151. },
  152. $message: _ref3 => {
  153. let {
  154. $response
  155. } = _ref3;
  156. return $response ? $response.$errors.map(context => {
  157. return Object.values(context).map(errors => errors.map(error => error.$message)).reduce((a, b) => a.concat(b), []);
  158. }) : [];
  159. }
  160. };
  161. }
  162. const req = value => {
  163. value = unref(value);
  164. if (Array.isArray(value)) return !!value.length;
  165. if (value === undefined || value === null) {
  166. return false;
  167. }
  168. if (value === false) {
  169. return true;
  170. }
  171. if (value instanceof Date) {
  172. return !isNaN(value.getTime());
  173. }
  174. if (typeof value === 'object') {
  175. for (let _ in value) return true;
  176. return false;
  177. }
  178. return !!String(value).length;
  179. };
  180. const len = value => {
  181. value = unref(value);
  182. if (Array.isArray(value)) return value.length;
  183. if (typeof value === 'object') {
  184. return Object.keys(value).length;
  185. }
  186. return String(value).length;
  187. };
  188. function regex() {
  189. for (var _len = arguments.length, expr = new Array(_len), _key = 0; _key < _len; _key++) {
  190. expr[_key] = arguments[_key];
  191. }
  192. return value => {
  193. value = unref(value);
  194. return !req(value) || expr.every(reg => {
  195. reg.lastIndex = 0;
  196. return reg.test(value);
  197. });
  198. };
  199. }
  200. var common = /*#__PURE__*/Object.freeze({
  201. __proto__: null,
  202. forEach: forEach,
  203. len: len,
  204. normalizeValidatorObject: normalizeValidatorObject,
  205. regex: regex,
  206. req: req,
  207. unwrap: unref,
  208. unwrapNormalizedValidator: unwrapNormalizedValidator,
  209. unwrapValidatorResponse: unwrapValidatorResponse,
  210. withAsync: withAsync,
  211. withMessage: withMessage,
  212. withParams: withParams
  213. });
  214. var alpha = regex(/^[a-zA-Z]*$/);
  215. var alphaNum = regex(/^[a-zA-Z0-9]*$/);
  216. var numeric = regex(/^\d*(\.\d+)?$/);
  217. function between (min, max) {
  218. return value => !req(value) || (!/\s/.test(value) || value instanceof Date) && +unref(min) <= +value && +unref(max) >= +value;
  219. }
  220. 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;
  221. var email = regex(emailRegex);
  222. function ipAddress (value) {
  223. if (!req(value)) {
  224. return true;
  225. }
  226. if (typeof value !== 'string') {
  227. return false;
  228. }
  229. const nibbles = value.split('.');
  230. return nibbles.length === 4 && nibbles.every(nibbleValid);
  231. }
  232. const nibbleValid = nibble => {
  233. if (nibble.length > 3 || nibble.length === 0) {
  234. return false;
  235. }
  236. if (nibble[0] === '0' && nibble !== '0') {
  237. return false;
  238. }
  239. if (!nibble.match(/^\d+$/)) {
  240. return false;
  241. }
  242. const numeric = +nibble | 0;
  243. return numeric >= 0 && numeric <= 255;
  244. };
  245. function macAddress () {
  246. let separator = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : ':';
  247. return value => {
  248. separator = unref(separator);
  249. if (!req(value)) {
  250. return true;
  251. }
  252. if (typeof value !== 'string') {
  253. return false;
  254. }
  255. const parts = typeof separator === 'string' && separator !== '' ? value.split(separator) : value.length === 12 || value.length === 16 ? value.match(/.{2}/g) : null;
  256. return parts !== null && (parts.length === 6 || parts.length === 8) && parts.every(hexValid);
  257. };
  258. }
  259. const hexValid = hex => hex.toLowerCase().match(/^[0-9a-f]{2}$/);
  260. function maxLength (length) {
  261. return value => !req(value) || len(value) <= unref(length);
  262. }
  263. function minLength (length) {
  264. return value => !req(value) || len(value) >= unref(length);
  265. }
  266. function required (value) {
  267. if (typeof value === 'string') {
  268. value = value.trim();
  269. }
  270. return req(value);
  271. }
  272. const validate$1 = (prop, val) => prop ? req(typeof val === 'string' ? val.trim() : val) : true;
  273. function requiredIf(propOrFunction) {
  274. return function (value, parentVM) {
  275. if (typeof propOrFunction !== 'function') {
  276. return validate$1(unref(propOrFunction), value);
  277. }
  278. const result = propOrFunction.call(this, value, parentVM);
  279. return validate$1(result, value);
  280. };
  281. }
  282. const validate = (prop, val) => !prop ? req(typeof val === 'string' ? val.trim() : val) : true;
  283. function requiredUnless(propOrFunction) {
  284. return function (value, parentVM) {
  285. if (typeof propOrFunction !== 'function') {
  286. return validate(unref(propOrFunction), value);
  287. }
  288. const result = propOrFunction.call(this, value, parentVM);
  289. return validate(result, value);
  290. };
  291. }
  292. function sameAs (equalTo) {
  293. return value => unref(value) === unref(equalTo);
  294. }
  295. 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;
  296. var url = regex(urlRegex);
  297. function syncOr(validators) {
  298. return function () {
  299. for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
  300. args[_key] = arguments[_key];
  301. }
  302. return validators.reduce((valid, fn) => {
  303. if (unwrapValidatorResponse(valid)) return valid;
  304. return unwrapNormalizedValidator(fn).apply(this, args);
  305. }, false);
  306. };
  307. }
  308. function asyncOr(validators) {
  309. return function () {
  310. for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
  311. args[_key2] = arguments[_key2];
  312. }
  313. return validators.reduce(async (valid, fn) => {
  314. const r = await valid;
  315. if (unwrapValidatorResponse(r)) return r;
  316. return unwrapNormalizedValidator(fn).apply(this, args);
  317. }, Promise.resolve(false));
  318. };
  319. }
  320. function or() {
  321. for (var _len3 = arguments.length, validators = new Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
  322. validators[_key3] = arguments[_key3];
  323. }
  324. const $async = validators.some(v => v.$async);
  325. const $watchTargets = validators.reduce((all, v) => {
  326. if (!v.$watchTargets) return all;
  327. return all.concat(v.$watchTargets);
  328. }, []);
  329. let $validator = () => false;
  330. if (validators.length) $validator = $async ? asyncOr(validators) : syncOr(validators);
  331. return {
  332. $async,
  333. $validator,
  334. $watchTargets
  335. };
  336. }
  337. function syncAnd(validators) {
  338. return function () {
  339. for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
  340. args[_key] = arguments[_key];
  341. }
  342. return validators.reduce((valid, fn) => {
  343. if (!unwrapValidatorResponse(valid)) return valid;
  344. return unwrapNormalizedValidator(fn).apply(this, args);
  345. }, true);
  346. };
  347. }
  348. function asyncAnd(validators) {
  349. return function () {
  350. for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
  351. args[_key2] = arguments[_key2];
  352. }
  353. return validators.reduce(async (valid, fn) => {
  354. const r = await valid;
  355. if (!unwrapValidatorResponse(r)) return r;
  356. return unwrapNormalizedValidator(fn).apply(this, args);
  357. }, Promise.resolve(true));
  358. };
  359. }
  360. function and() {
  361. for (var _len3 = arguments.length, validators = new Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
  362. validators[_key3] = arguments[_key3];
  363. }
  364. const $async = validators.some(v => v.$async);
  365. const $watchTargets = validators.reduce((all, v) => {
  366. if (!v.$watchTargets) return all;
  367. return all.concat(v.$watchTargets);
  368. }, []);
  369. let $validator = () => false;
  370. if (validators.length) $validator = $async ? asyncAnd(validators) : syncAnd(validators);
  371. return {
  372. $async,
  373. $validator,
  374. $watchTargets
  375. };
  376. }
  377. function not (validator) {
  378. return function (value, vm) {
  379. if (!req(value)) return true;
  380. const response = unwrapNormalizedValidator(validator).call(this, value, vm);
  381. if (!isPromise(response)) return !unwrapValidatorResponse(response);
  382. return response.then(r => !unwrapValidatorResponse(r));
  383. };
  384. }
  385. function minValue (min) {
  386. return value => !req(value) || (!/\s/.test(value) || value instanceof Date) && +value >= +unref(min);
  387. }
  388. function maxValue (max) {
  389. return value => !req(value) || (!/\s/.test(value) || value instanceof Date) && +value <= +unref(max);
  390. }
  391. var integer = regex(/(^[0-9]*$)|(^-[0-9]+$)/);
  392. var decimal = regex(/^[-]?\d*(\.\d+)?$/);
  393. 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 };