it-tools/src/composable/validation.ts

78 lines
1.5 KiB
TypeScript
Raw Normal View History

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