it-tools/src/composable/validation.ts

75 lines
1.5 KiB
TypeScript
Raw Normal View History

import { get, type MaybeRef } from '@vueuse/core';
import _ from 'lodash';
2022-04-11 22:47:05 +02:00
import { reactive, watch, type Ref } from 'vue';
type ValidatorReturnType = unknown;
2023-04-10 16:34:10 +02:00
export interface UseValidationRule<T> {
validator: (value: T) => ValidatorReturnType;
2022-04-11 23:08:50 +02:00
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;
2022-05-09 17:40:29 +02:00
} catch (_) {
return true;
}
}
export type 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<{
2022-04-11 23:08:50 +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,
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;
}