版博士V2.0程序
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

useAsyncValidator.cjs 2.4 KiB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. 'use strict';
  2. var shared = require('@vueuse/shared');
  3. var Schema = require('async-validator');
  4. var vueDemi = require('vue-demi');
  5. var __defProp = Object.defineProperty;
  6. var __defProps = Object.defineProperties;
  7. var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
  8. var __getOwnPropSymbols = Object.getOwnPropertySymbols;
  9. var __hasOwnProp = Object.prototype.hasOwnProperty;
  10. var __propIsEnum = Object.prototype.propertyIsEnumerable;
  11. var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
  12. var __spreadValues = (a, b) => {
  13. for (var prop in b || (b = {}))
  14. if (__hasOwnProp.call(b, prop))
  15. __defNormalProp(a, prop, b[prop]);
  16. if (__getOwnPropSymbols)
  17. for (var prop of __getOwnPropSymbols(b)) {
  18. if (__propIsEnum.call(b, prop))
  19. __defNormalProp(a, prop, b[prop]);
  20. }
  21. return a;
  22. };
  23. var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
  24. const AsyncValidatorSchema = Schema || Schema.default;
  25. function useAsyncValidator(value, rules, options = {}) {
  26. const errorInfo = vueDemi.ref();
  27. const isFinished = vueDemi.ref(false);
  28. const pass = vueDemi.ref(false);
  29. const errors = vueDemi.computed(() => {
  30. var _a;
  31. return ((_a = errorInfo.value) == null ? void 0 : _a.errors) || [];
  32. });
  33. const errorFields = vueDemi.computed(() => {
  34. var _a;
  35. return ((_a = errorInfo.value) == null ? void 0 : _a.fields) || {};
  36. });
  37. const { validateOption = {} } = options;
  38. vueDemi.watchEffect(async () => {
  39. isFinished.value = false;
  40. pass.value = false;
  41. const validator = new AsyncValidatorSchema(shared.resolveUnref(rules));
  42. try {
  43. await validator.validate(shared.resolveUnref(value), validateOption);
  44. pass.value = true;
  45. errorInfo.value = null;
  46. } catch (err) {
  47. errorInfo.value = err;
  48. } finally {
  49. isFinished.value = true;
  50. }
  51. });
  52. const shell = {
  53. pass,
  54. isFinished,
  55. errorInfo,
  56. errors,
  57. errorFields
  58. };
  59. function waitUntilFinished() {
  60. return new Promise((resolve, reject) => {
  61. shared.until(isFinished).toBe(true).then(() => resolve(shell)).catch((error) => reject(error));
  62. });
  63. }
  64. return __spreadProps(__spreadValues({}, shell), {
  65. then(onFulfilled, onRejected) {
  66. return waitUntilFinished().then(onFulfilled, onRejected);
  67. }
  68. });
  69. }
  70. exports.useAsyncValidator = useAsyncValidator;