refactor(base64-to-file): clean validation to convert base64 to file

This commit is contained in:
Corentin Thomasset 2022-07-29 10:56:04 +02:00
parent 5f03619ab4
commit 750a76b00f
No known key found for this signature in database
GPG key ID: 3103EB5E79496F9C
4 changed files with 113 additions and 22 deletions

View file

@ -1,13 +1,20 @@
import _ from 'lodash';
import { reactive, watch, type Ref } from 'vue';
type UseValidationRule<T> = {
validator: (value: T) => boolean;
message: string;
};
type ValidatorReturnType = unknown;
function isFalsyOrHasThrown(cb: () => boolean) {
interface UseValidationRule<T> {
validator: (value: T) => ValidatorReturnType;
message: string;
}
export function isFalsyOrHasThrown(cb: () => ValidatorReturnType): boolean {
try {
return !cb();
const returnValue = cb();
if (_.isNil(returnValue)) return true;
return returnValue === false;
} catch (_) {
return true;
}
@ -17,22 +24,30 @@ export function useValidation<T>({ source, rules }: { source: Ref<T>; rules: Use
const state = reactive<{
message: string;
status: undefined | 'error';
isValid: boolean;
}>({
message: '',
status: undefined,
isValid: false,
});
watch([source], () => {
state.message = '';
state.status = undefined;
watch(
[source],
() => {
state.message = '';
state.status = undefined;
for (const rule of rules) {
if (isFalsyOrHasThrown(() => rule.validator(source.value))) {
state.message = rule.message;
state.status = 'error';
for (const rule of rules) {
if (isFalsyOrHasThrown(() => rule.validator(source.value))) {
state.message = rule.message;
state.status = 'error';
}
}
}
});
state.isValid = state.status !== 'error';
},
{ immediate: true },
);
return state;
}