aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGravatar Corentin Thomasset <corentin.thomasset74@gmail.com> 2022-05-09 17:40:29 +0200
committerGravatar Corentin Thomasset <corentin.thomasset74@gmail.com> 2022-05-09 17:40:29 +0200
commita60f64f74417f811204121f97c16cdb4754afc3b (patch)
treeb2ab1d855e59826e7c35115e495cb8ebbb7e4996 /src
parentb89db3c8d0de601fecbd2f9f79492dff1b461bd8 (diff)
downloadit-tools-a60f64f74417f811204121f97c16cdb4754afc3b.tar.gz
it-tools-a60f64f74417f811204121f97c16cdb4754afc3b.tar.zst
it-tools-a60f64f74417f811204121f97c16cdb4754afc3b.zip
feat: catch throw on validation
Diffstat (limited to 'src')
-rw-r--r--src/composable/validation.ts10
1 files changed, 9 insertions, 1 deletions
diff --git a/src/composable/validation.ts b/src/composable/validation.ts
index 9d1d424..40df887 100644
--- a/src/composable/validation.ts
+++ b/src/composable/validation.ts
@@ -5,6 +5,14 @@ type UseValidationRule<T> = {
message: string;
};
+function isFalsyOrHasThrown(cb: () => boolean) {
+ try {
+ return !cb();
+ } catch (_) {
+ return true;
+ }
+}
+
export function useValidation<T>({ source, rules }: { source: Ref<T>; rules: UseValidationRule<T>[] }) {
const state = reactive<{
message: string;
@@ -19,7 +27,7 @@ export function useValidation<T>({ source, rules }: { source: Ref<T>; rules: Use
state.status = undefined;
for (const rule of rules) {
- if (!rule.validator(source.value)) {
+ if (isFalsyOrHasThrown(() => rule.validator(source.value))) {
state.message = rule.message;
state.status = 'error';
}