版博士V2.0程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

useAsyncValidator.mjs 2.3 KiB

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