From a22ee09344d6edd7107280b283f0226e0dd0c94e Mon Sep 17 00:00:00 2001 From: axolotle Date: Thu, 11 Jul 2024 13:34:48 +0200 Subject: [PATCH] feat: add useArrayRule to generate rules for each array elem --- app/src/composables/form.ts | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/app/src/composables/form.ts b/app/src/composables/form.ts index 71150f0d..27d38613 100644 --- a/app/src/composables/form.ts +++ b/app/src/composables/form.ts @@ -5,8 +5,8 @@ import type { ValidationRuleCollection, } from '@vuelidate/core' import useVuelidate from '@vuelidate/core' -import type { InjectionKey, MaybeRefOrGetter, Ref } from 'vue' -import { inject, provide, reactive, toValue } from 'vue' +import type { ComputedRef, InjectionKey, MaybeRefOrGetter, Ref } from 'vue' +import { computed, inject, provide, reactive, toValue } from 'vue' import { computedWithControl } from '@vueuse/core' import { APIBadRequestError, type APIError } from '@/api/errors' @@ -128,3 +128,15 @@ export function deepSetErrors( serverErrors[k] = value } } + +export function useArrayRule( + values: MaybeRefOrGetter, + rules: T, +): ComputedRef> { + return computed(() => { + return toValue(values).reduce((total: Obj, v: V[number], index) => { + total[index] = rules + return total + }, {}) + }) +}