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';
|
import { reactive, watch, type Ref } from 'vue';
|
||||||
|
|
||||||
type UseValidationRule<T> = {
|
type UseValidationRule<T> = {
|
||||||
validator: (value: T) => boolean
|
validator: (value: T) => boolean;
|
||||||
message: string
|
message: string;
|
||||||
}
|
};
|
||||||
|
|
||||||
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';
|
||||||
}>({
|
}>({
|
||||||
message: '',
|
message: '',
|
||||||
status: undefined
|
status: undefined,
|
||||||
})
|
});
|
||||||
|
|
||||||
watch([source], () => {
|
watch([source], () => {
|
||||||
for(const rule of rules) {
|
state.message = '';
|
||||||
if(!rule.validator(source.value)){
|
state.status = undefined;
|
||||||
state.message = rule.message
|
|
||||||
state.status = 'error'
|
for (const rule of rules) {
|
||||||
|
if (!rule.validator(source.value)) {
|
||||||
|
state.message = rule.message;
|
||||||
|
state.status = 'error';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
});
|
||||||
|
|
||||||
return state;
|
return state;
|
||||||
}
|
}
|
||||||
|
|
|
@ -110,7 +110,7 @@ const entropyValidation = useValidation({
|
||||||
message: 'Entropy length should be >= 16, <= 32 and be a multiple of 4'
|
message: 'Entropy length should be >= 16, <= 32 and be a multiple of 4'
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
validator: (value) => /^[a-fA-f0-9]?$/.test(value),
|
validator: (value) => /^[a-fA-F0-9]*$/.test(value),
|
||||||
message: 'Entropy should an hexadecimal number'
|
message: 'Entropy should an hexadecimal number'
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue