mirror of
https://github.com/CorentinTh/it-tools.git
synced 2025-04-22 07:46:15 -04:00
fix(validation): proper rules
This commit is contained in:
parent
b44539c182
commit
11d8110226
2 changed files with 16 additions and 13 deletions
|
@ -1,27 +1,30 @@
|
|||
import { reactive, watch, type Ref } from 'vue';
|
||||
|
||||
type UseValidationRule<T> = {
|
||||
validator: (value: T) => boolean
|
||||
message: string
|
||||
}
|
||||
validator: (value: T) => boolean;
|
||||
message: string;
|
||||
};
|
||||
|
||||
export function useValidation<T>({ source, rules }: { source: Ref<T>; rules: UseValidationRule<T>[] }) {
|
||||
const state = reactive<{
|
||||
message: string,
|
||||
status: undefined | 'error'
|
||||
message: string;
|
||||
status: undefined | 'error';
|
||||
}>({
|
||||
message: '',
|
||||
status: undefined
|
||||
})
|
||||
status: undefined,
|
||||
});
|
||||
|
||||
watch([source], () => {
|
||||
for(const rule of rules) {
|
||||
if(!rule.validator(source.value)){
|
||||
state.message = rule.message
|
||||
state.status = 'error'
|
||||
state.message = '';
|
||||
state.status = undefined;
|
||||
|
||||
for (const rule of rules) {
|
||||
if (!rule.validator(source.value)) {
|
||||
state.message = rule.message;
|
||||
state.status = 'error';
|
||||
}
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
return state;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue