aboutsummaryrefslogtreecommitdiff
path: root/src/composable
diff options
context:
space:
mode:
authorGravatar Corentin Thomasset <corentin.thomasset74@gmail.com> 2022-04-11 22:47:05 +0200
committerGravatar Corentin Thomasset <corentin.thomasset74@gmail.com> 2022-04-11 22:47:05 +0200
commitd55329f3abc3d3f8ad48def7d7f63b44cd768e27 (patch)
treecb054d00cd569ff9c1883528d79550a2b4b92251 /src/composable
parent3ae872847b00d65e4e2e629775d479a3333450f1 (diff)
downloadit-tools-d55329f3abc3d3f8ad48def7d7f63b44cd768e27.tar.gz
it-tools-d55329f3abc3d3f8ad48def7d7f63b44cd768e27.tar.zst
it-tools-d55329f3abc3d3f8ad48def7d7f63b44cd768e27.zip
feat(tool): bip39-generator
Diffstat (limited to 'src/composable')
-rw-r--r--src/composable/validation.ts27
1 files changed, 27 insertions, 0 deletions
diff --git a/src/composable/validation.ts b/src/composable/validation.ts
new file mode 100644
index 0000000..09b1fce
--- /dev/null
+++ b/src/composable/validation.ts
@@ -0,0 +1,27 @@
+import { reactive, watch, type Ref } from 'vue';
+
+type UseValidationRule<T> = {
+ 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: '',
+ status: undefined
+ })
+
+ watch([source], () => {
+ for(const rule of rules) {
+ if(!rule.validator(source.value)){
+ state.message = rule.message
+ state.status = 'error'
+ }
+ }
+ })
+
+ return state;
+}