mirror of
https://github.com/CorentinTh/it-tools.git
synced 2025-04-22 07:46:15 -04:00
refactor(validation): simplified validation system
This commit is contained in:
parent
7570ad9656
commit
77b5b0cab5
2 changed files with 13 additions and 5 deletions
|
@ -20,15 +20,25 @@ export function isFalsyOrHasThrown(cb: () => ValidatorReturnType): boolean {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type ValidationAttrs = {
|
||||||
|
feedback: string;
|
||||||
|
validationStatus: string | undefined;
|
||||||
|
};
|
||||||
|
|
||||||
export function useValidation<T>({ source, rules }: { source: Ref<T>; rules: UseValidationRule<T>[] }) {
|
export function useValidation<T>({ source, rules }: { source: Ref<T>; rules: UseValidationRule<T>[] }) {
|
||||||
const state = reactive<{
|
const state = reactive<{
|
||||||
message: string;
|
message: string;
|
||||||
status: undefined | 'error';
|
status: undefined | 'error';
|
||||||
isValid: boolean;
|
isValid: boolean;
|
||||||
|
attrs: ValidationAttrs;
|
||||||
}>({
|
}>({
|
||||||
message: '',
|
message: '',
|
||||||
status: undefined,
|
status: undefined,
|
||||||
isValid: false,
|
isValid: false,
|
||||||
|
attrs: {
|
||||||
|
validationStatus: undefined,
|
||||||
|
feedback: '',
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
watch(
|
watch(
|
||||||
|
@ -45,6 +55,8 @@ export function useValidation<T>({ source, rules }: { source: Ref<T>; rules: Use
|
||||||
}
|
}
|
||||||
|
|
||||||
state.isValid = state.status !== 'error';
|
state.isValid = state.status !== 'error';
|
||||||
|
state.attrs.feedback = state.message;
|
||||||
|
state.attrs.validationStatus = state.status;
|
||||||
},
|
},
|
||||||
{ immediate: true },
|
{ immediate: true },
|
||||||
);
|
);
|
||||||
|
|
|
@ -20,11 +20,7 @@
|
||||||
</n-card>
|
</n-card>
|
||||||
|
|
||||||
<n-card title="Base64 to string">
|
<n-card title="Base64 to string">
|
||||||
<n-form-item
|
<n-form-item label="Base64 string to decode" v-bind="b64Validation.attrs">
|
||||||
label="Base64 string to decode"
|
|
||||||
:validation-status="b64Validation.status"
|
|
||||||
:feedback="b64Validation.message"
|
|
||||||
>
|
|
||||||
<n-input v-model:value="base64Input" type="textarea" placeholder="Your base64 string..." rows="5" />
|
<n-input v-model:value="base64Input" type="textarea" placeholder="Your base64 string..." rows="5" />
|
||||||
</n-form-item>
|
</n-form-item>
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue