feat: add useArrayRule to generate rules for each array elem

This commit is contained in:
axolotle 2024-07-11 13:34:48 +02:00
parent 85b4980bee
commit a22ee09344

View file

@ -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<V extends any[], T extends ValidationArgs>(
values: MaybeRefOrGetter<V>,
rules: T,
): ComputedRef<ValidationArgs<T>> {
return computed(() => {
return toValue(values).reduce((total: Obj<T>, v: V[number], index) => {
total[index] = rules
return total
}, {})
})
}