2023-05-28 23:13:24 +02:00
|
|
|
import { type MaybeRef, get } from '@vueuse/core';
|
2022-07-29 10:56:04 +02:00
|
|
|
import _ from 'lodash';
|
2023-05-28 23:13:24 +02:00
|
|
|
import { type Ref, reactive, watch } from 'vue';
|
2022-04-11 22:47:05 +02:00
|
|
|
|
2022-07-29 10:56:04 +02:00
|
|
|
type ValidatorReturnType = unknown;
|
|
|
|
|
2023-04-10 16:34:10 +02:00
|
|
|
export interface UseValidationRule<T> {
|
2023-05-28 23:13:24 +02:00
|
|
|
validator: (value: T) => ValidatorReturnType
|
|
|
|
message: string
|
2022-07-29 10:56:04 +02:00
|
|
|
}
|
2022-04-11 22:47:05 +02:00
|
|
|
|
2022-07-29 10:56:04 +02:00
|
|
|
export function isFalsyOrHasThrown(cb: () => ValidatorReturnType): boolean {
|
2022-05-09 17:40:29 +02:00
|
|
|
try {
|
2022-07-29 10:56:04 +02:00
|
|
|
const returnValue = cb();
|
|
|
|
|
2023-05-28 23:13:24 +02:00
|
|
|
if (_.isNil(returnValue)) {
|
|
|
|
return true;
|
|
|
|
}
|
2022-07-29 10:56:04 +02:00
|
|
|
|
|
|
|
return returnValue === false;
|
2023-05-28 23:13:24 +02:00
|
|
|
}
|
|
|
|
catch (_) {
|
2022-05-09 17:40:29 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-28 23:13:24 +02:00
|
|
|
export interface ValidationAttrs {
|
|
|
|
feedback: string
|
|
|
|
validationStatus: string | undefined
|
|
|
|
}
|
2022-07-29 15:49:28 +02:00
|
|
|
|
2023-04-12 23:01:21 +02:00
|
|
|
export function useValidation<T>({
|
|
|
|
source,
|
|
|
|
rules,
|
|
|
|
watch: watchRefs = [],
|
|
|
|
}: {
|
2023-05-28 23:13:24 +02:00
|
|
|
source: Ref<T>
|
|
|
|
rules: MaybeRef<UseValidationRule<T>[]>
|
|
|
|
watch?: Ref<unknown>[]
|
2023-04-12 23:01:21 +02:00
|
|
|
}) {
|
2022-04-11 22:47:05 +02:00
|
|
|
const state = reactive<{
|
2023-05-28 23:13:24 +02:00
|
|
|
message: string
|
|
|
|
status: undefined | 'error'
|
|
|
|
isValid: boolean
|
|
|
|
attrs: ValidationAttrs
|
2022-04-11 22:47:05 +02:00
|
|
|
}>({
|
|
|
|
message: '',
|
2022-04-11 23:08:50 +02:00
|
|
|
status: undefined,
|
2022-07-29 10:56:04 +02:00
|
|
|
isValid: false,
|
2022-07-29 15:49:28 +02:00
|
|
|
attrs: {
|
|
|
|
validationStatus: undefined,
|
|
|
|
feedback: '',
|
|
|
|
},
|
2022-04-11 23:08:50 +02:00
|
|
|
});
|
2022-04-11 22:47:05 +02:00
|
|
|
|
2022-07-29 10:56:04 +02:00
|
|
|
watch(
|
2023-04-12 23:01:21 +02:00
|
|
|
[source, ...watchRefs],
|
2022-07-29 10:56:04 +02:00
|
|
|
() => {
|
|
|
|
state.message = '';
|
|
|
|
state.status = undefined;
|
2022-04-11 23:08:50 +02:00
|
|
|
|
2023-05-07 23:31:10 +02:00
|
|
|
for (const rule of get(rules)) {
|
2022-07-29 10:56:04 +02:00
|
|
|
if (isFalsyOrHasThrown(() => rule.validator(source.value))) {
|
|
|
|
state.message = rule.message;
|
|
|
|
state.status = 'error';
|
|
|
|
}
|
2022-04-11 22:47:05 +02:00
|
|
|
}
|
2022-07-29 10:56:04 +02:00
|
|
|
|
|
|
|
state.isValid = state.status !== 'error';
|
2022-07-29 15:49:28 +02:00
|
|
|
state.attrs.feedback = state.message;
|
|
|
|
state.attrs.validationStatus = state.status;
|
2022-07-29 10:56:04 +02:00
|
|
|
},
|
|
|
|
{ immediate: true },
|
|
|
|
);
|
2022-04-11 22:47:05 +02:00
|
|
|
|
|
|
|
return state;
|
|
|
|
}
|