diff options
author | 2022-04-11 23:08:50 +0200 | |
---|---|---|
committer | 2022-04-11 23:08:50 +0200 | |
commit | 11d8110226e22e30ae16d297628c1d252a93be9e (patch) | |
tree | 2d94ccceedb58b0ac105a7776e52654a44b4a575 /src/composable | |
parent | b44539c1820defbaaa6dfe83a76c72982a641971 (diff) | |
download | it-tools-11d8110226e22e30ae16d297628c1d252a93be9e.tar.gz it-tools-11d8110226e22e30ae16d297628c1d252a93be9e.tar.zst it-tools-11d8110226e22e30ae16d297628c1d252a93be9e.zip |
fix(validation): proper rules
Diffstat (limited to 'src/composable')
-rw-r--r-- | src/composable/validation.ts | 27 |
1 files changed, 15 insertions, 12 deletions
diff --git a/src/composable/validation.ts b/src/composable/validation.ts index 09b1fce..9d1d424 100644 --- a/src/composable/validation.ts +++ b/src/composable/validation.ts @@ -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; } |