All Downloads are FREE. Search and download functionalities are using the official Maven repository.

package.lib.composables.validation.mjs.map Maven / Gradle / Ivy

{"version":3,"file":"validation.mjs","names":["makeFocusProps","useForm","useProxiedModel","useToggleScope","computed","nextTick","onBeforeMount","onBeforeUnmount","onMounted","ref","shallowRef","unref","watch","getCurrentInstance","getCurrentInstanceName","getUid","propsFactory","wrapInArray","makeValidationProps","disabled","type","Boolean","default","error","errorMessages","Array","String","maxErrors","Number","name","label","readonly","rules","modelValue","validateOn","validationValue","useValidation","props","arguments","length","undefined","id","model","validationModel","value","form","internalErrorMessages","isPristine","isDirty","isDisabled","isReadonly","concat","slice","Math","max","set","Set","split","blur","has","input","submit","lazy","isValid","isValidating","validationClasses","vm","uid","register","validate","reset","resetValidation","unregister","update","focused","unwatch","val","silent","results","rule","handler","result","console","warn","push"],"sources":["../../src/composables/validation.ts"],"sourcesContent":["// Composables\nimport { makeFocusProps } from '@/composables/focus'\nimport { useForm } from '@/composables/form'\nimport { useProxiedModel } from '@/composables/proxiedModel'\nimport { useToggleScope } from '@/composables/toggleScope'\n\n// Utilities\nimport { computed, nextTick, onBeforeMount, onBeforeUnmount, onMounted, ref, shallowRef, unref, watch } from 'vue'\nimport { getCurrentInstance, getCurrentInstanceName, getUid, propsFactory, wrapInArray } from '@/util'\n\n// Types\nimport type { PropType } from 'vue'\nimport type { EventProp, MaybeRef } from '@/util'\n\nexport type ValidationResult = string | boolean\nexport type ValidationRule =\n  | ValidationResult\n  | PromiseLike\n  | ((value: any) => ValidationResult)\n  | ((value: any) => PromiseLike)\n\ntype ValidateOnValue = 'blur' | 'input' | 'submit'\n\nexport interface ValidationProps {\n  disabled: boolean | null\n  error: boolean\n  errorMessages: string | readonly string[] | null\n  focused: boolean\n  maxErrors: string | number\n  name: string | undefined\n  label: string | undefined\n  readonly: boolean | null\n  rules: readonly ValidationRule[]\n  modelValue: any\n  'onUpdate:modelValue': EventProp | undefined\n  validateOn?: ValidateOnValue | `${ValidateOnValue} lazy` | `lazy ${ValidateOnValue}` | 'lazy'\n  validationValue: any\n}\n\nexport const makeValidationProps = propsFactory({\n  disabled: {\n    type: Boolean as PropType,\n    default: null,\n  },\n  error: Boolean,\n  errorMessages: {\n    type: [Array, String] as PropType,\n    default: () => ([]),\n  },\n  maxErrors: {\n    type: [Number, String],\n    default: 1,\n  },\n  name: String,\n  label: String,\n  readonly: {\n    type: Boolean as PropType,\n    default: null,\n  },\n  rules: {\n    type: Array as PropType,\n    default: () => ([]),\n  },\n  modelValue: null,\n  validateOn: String as PropType,\n  validationValue: null,\n\n  ...makeFocusProps(),\n}, 'validation')\n\nexport function useValidation (\n  props: ValidationProps,\n  name = getCurrentInstanceName(),\n  id: MaybeRef = getUid(),\n) {\n  const model = useProxiedModel(props, 'modelValue')\n  const validationModel = computed(() => props.validationValue === undefined ? model.value : props.validationValue)\n  const form = useForm()\n  const internalErrorMessages = ref([])\n  const isPristine = shallowRef(true)\n  const isDirty = computed(() => !!(\n    wrapInArray(model.value === '' ? null : model.value).length ||\n    wrapInArray(validationModel.value === '' ? null : validationModel.value).length\n  ))\n  const isDisabled = computed(() => !!(props.disabled ?? form?.isDisabled.value))\n  const isReadonly = computed(() => !!(props.readonly ?? form?.isReadonly.value))\n  const errorMessages = computed(() => {\n    return props.errorMessages?.length\n      ? wrapInArray(props.errorMessages).concat(internalErrorMessages.value).slice(0, Math.max(0, +props.maxErrors))\n      : internalErrorMessages.value\n  })\n  const validateOn = computed(() => {\n    let value = (props.validateOn ?? form?.validateOn.value) || 'input'\n    if (value === 'lazy') value = 'input lazy'\n    const set = new Set(value?.split(' ') ?? [])\n\n    return {\n      blur: set.has('blur') || set.has('input'),\n      input: set.has('input'),\n      submit: set.has('submit'),\n      lazy: set.has('lazy'),\n    }\n  })\n  const isValid = computed(() => {\n    if (props.error || props.errorMessages?.length) return false\n    if (!props.rules.length) return true\n    if (isPristine.value) {\n      return internalErrorMessages.value.length || validateOn.value.lazy ? null : true\n    } else {\n      return !internalErrorMessages.value.length\n    }\n  })\n  const isValidating = shallowRef(false)\n  const validationClasses = computed(() => {\n    return {\n      [`${name}--error`]: isValid.value === false,\n      [`${name}--dirty`]: isDirty.value,\n      [`${name}--disabled`]: isDisabled.value,\n      [`${name}--readonly`]: isReadonly.value,\n    }\n  })\n\n  const vm = getCurrentInstance('validation')\n  const uid = computed(() => props.name ?? unref(id))\n\n  onBeforeMount(() => {\n    form?.register({\n      id: uid.value,\n      vm,\n      validate,\n      reset,\n      resetValidation,\n    })\n  })\n\n  onBeforeUnmount(() => {\n    form?.unregister(uid.value)\n  })\n\n  onMounted(async () => {\n    if (!validateOn.value.lazy) {\n      await validate(true)\n    }\n    form?.update(uid.value, isValid.value, errorMessages.value)\n  })\n\n  useToggleScope(() => validateOn.value.input, () => {\n    watch(validationModel, () => {\n      if (validationModel.value != null) {\n        validate()\n      } else if (props.focused) {\n        const unwatch = watch(() => props.focused, val => {\n          if (!val) validate()\n\n          unwatch()\n        })\n      }\n    })\n  })\n\n  useToggleScope(() => validateOn.value.blur, () => {\n    watch(() => props.focused, val => {\n      if (!val) validate()\n    })\n  })\n\n  watch([isValid, errorMessages], () => {\n    form?.update(uid.value, isValid.value, errorMessages.value)\n  })\n\n  async function reset () {\n    model.value = null\n    await nextTick()\n    await resetValidation()\n  }\n\n  async function resetValidation () {\n    isPristine.value = true\n    if (!validateOn.value.lazy) {\n      await validate(true)\n    } else {\n      internalErrorMessages.value = []\n    }\n  }\n\n  async function validate (silent = false) {\n    const results = []\n\n    isValidating.value = true\n\n    for (const rule of props.rules) {\n      if (results.length >= +(props.maxErrors ?? 1)) {\n        break\n      }\n\n      const handler = typeof rule === 'function' ? rule : () => rule\n      const result = await handler(validationModel.value)\n\n      if (result === true) continue\n\n      if (result !== false && typeof result !== 'string') {\n        // eslint-disable-next-line no-console\n        console.warn(`${result} is not a valid value. Rule functions must return boolean true or a string.`)\n\n        continue\n      }\n\n      results.push(result || '')\n    }\n\n    internalErrorMessages.value = results\n    isValidating.value = false\n    isPristine.value = silent\n\n    return internalErrorMessages.value\n  }\n\n  return {\n    errorMessages,\n    isDirty,\n    isDisabled,\n    isReadonly,\n    isPristine,\n    isValid,\n    isValidating,\n    reset,\n    resetValidation,\n    validate,\n    validationClasses,\n  }\n}\n"],"mappings":"AAAA;AAAA,SACSA,cAAc;AAAA,SACdC,OAAO;AAAA,SACPC,eAAe;AAAA,SACfC,cAAc,6BAEvB;AACA,SAASC,QAAQ,EAAEC,QAAQ,EAAEC,aAAa,EAAEC,eAAe,EAAEC,SAAS,EAAEC,GAAG,EAAEC,UAAU,EAAEC,KAAK,EAAEC,KAAK,QAAQ,KAAK;AAAA,SACzGC,kBAAkB,EAAEC,sBAAsB,EAAEC,MAAM,EAAEC,YAAY,EAAEC,WAAW,6BAEtF;AA6BA,OAAO,MAAMC,mBAAmB,GAAGF,YAAY,CAAC;EAC9CG,QAAQ,EAAE;IACRC,IAAI,EAAEC,OAAmC;IACzCC,OAAO,EAAE;EACX,CAAC;EACDC,KAAK,EAAEF,OAAO;EACdG,aAAa,EAAE;IACbJ,IAAI,EAAE,CAACK,KAAK,EAAEC,MAAM,CAAgD;IACpEJ,OAAO,EAAEA,CAAA,KAAO;EAClB,CAAC;EACDK,SAAS,EAAE;IACTP,IAAI,EAAE,CAACQ,MAAM,EAAEF,MAAM,CAAC;IACtBJ,OAAO,EAAE;EACX,CAAC;EACDO,IAAI,EAAEH,MAAM;EACZI,KAAK,EAAEJ,MAAM;EACbK,QAAQ,EAAE;IACRX,IAAI,EAAEC,OAAmC;IACzCC,OAAO,EAAE;EACX,CAAC;EACDU,KAAK,EAAE;IACLZ,IAAI,EAAEK,KAA4C;IAClDH,OAAO,EAAEA,CAAA,KAAO;EAClB,CAAC;EACDW,UAAU,EAAE,IAAI;EAChBC,UAAU,EAAER,MAAiD;EAC7DS,eAAe,EAAE,IAAI;EAErB,GAAGnC,cAAc,CAAC;AACpB,CAAC,EAAE,YAAY,CAAC;AAEhB,OAAO,SAASoC,aAAaA,CAC3BC,KAAsB,EAG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