chore(Form): export FormValidationException in #ui/types (#47)

This commit is contained in:
Romain Hamel
2024-04-08 12:00:38 +02:00
committed by GitHub
parent bb42594ea4
commit 2dfaea580a
2 changed files with 21 additions and 15 deletions

View File

@@ -4,6 +4,7 @@ import type { AppConfig } from '@nuxt/schema'
import _appConfig from '#build/app.config'
import theme from '#build/ui/form'
import type { FormSchema, FormError, FormInputEvents, FormErrorEvent, FormSubmitEvent, FormEvent, FormInjectedOptions, Form, FormErrorWithId } from '#ui/types/form'
import { FormValidationException } from '#ui/types/form'
const appConfig = _appConfig as AppConfig & { ui: { form: Partial<typeof theme> } }
@@ -28,20 +29,6 @@ export interface FormEmits<T extends object> {
export interface FormSlots {
default(): any
}
export class FormValidationException extends Error {
formId: string | number
errors: FormErrorWithId[]
childrens: FormValidationException[]
constructor (formId: string | number, errors: FormErrorWithId[], childErrors: FormValidationException[]) {
super('Form validation exception')
this.formId = formId
this.errors = errors
this.childrens = childErrors
Object.setPrototypeOf(this, FormValidationException.prototype)
}
}
</script>
<script lang="ts" setup generic="T extends object">

View File

@@ -1,4 +1,8 @@
import type { ComputedRef, Ref } from 'vue'
import type { ZodSchema } from 'zod'
import type { Schema as JoiSchema } from 'joi'
import type { ObjectSchema as YupObjectSchema } from 'yup'
import type { ObjectSchemaAsync as ValibotObjectSchema } from 'valibot'
export interface Form<T> {
validate (opts?: { name: string | string[], silent?: false, nested?: boolean }): Promise<T | false>
@@ -10,7 +14,7 @@ export interface Form<T> {
disabled: ComputedRef<boolean>
}
export type FormSchema<T extends object> =
export type FormSchema<T extends Record<string, any>> =
| ZodSchema
| YupObjectSchema<T>
| ValibotObjectSchema<T>
@@ -67,8 +71,23 @@ export interface FormInjectedOptions {
export interface FormFieldInjectedOptions<T> {
inputId: Ref<string | undefined>
name: ComputedRef<string | undefined>
// @ts-ignore FIXME: TS doesn't like this
size: ComputedRef<T['size']>
error: ComputedRef<string | boolean | undefined>
eagerValidation: ComputedRef<boolean | undefined>
validateOnInputDelay: ComputedRef<number | undefined>
}
export class FormValidationException extends Error {
formId: string | number
errors: FormErrorWithId[]
childrens: FormValidationException[]
constructor (formId: string | number, errors: FormErrorWithId[], childErrors: FormValidationException[]) {
super('Form validation exception')
this.formId = formId
this.errors = errors
this.childrens = childErrors
Object.setPrototypeOf(this, FormValidationException.prototype)
}
}