{"version":3,"file":"validation.mjs","names":["makeFocusProps","useForm","useProxiedModel","useToggleScope","computed","nextTick","onBeforeMount","onBeforeUnmount","onMounted","ref","shallowRef","unref","watch","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","slice","Math","max","set","Set","split","blur","has","input","submit","lazy","isValid","isValidating","validationClasses","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 { getCurrentInstanceName, getUid, propsFactory, wrapInArray } from '@/util'\n\n// Types\nimport type { PropType } from 'vue'\nimport type { 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[]\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': ((val: any) => void) | 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).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 uid = computed(() => props.name ?? unref(id))\n\n onBeforeMount(() => {\n form?.register({\n id: uid.value,\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, () => {\n form?.update(uid.value, isValid.value, errorMessages.value)\n })\n\n function reset () {\n model.value = null\n nextTick(resetValidation)\n }\n\n function resetValidation () {\n isPristine.value = true\n if (!validateOn.value.lazy) {\n 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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