mirror of
https://github.com/ArthurDanjou/ui.git
synced 2026-01-18 14:08:06 +01:00
feat(Form): add Standard Schema support (#2303)
Co-authored-by: Benjamin Canac <canacb1@gmail.com>
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import type { StandardSchema } from '@standard-schema/spec'
|
||||
import type { ZodSchema } from 'zod'
|
||||
import type { ValidationError as JoiError, Schema as JoiSchema } from 'joi'
|
||||
import type { ObjectSchema as YupObjectSchema, ValidationError as YupError } from 'yup'
|
||||
@@ -71,7 +72,7 @@ export function isValibotSchema(schema: any): schema is ValibotSchema | ValibotS
|
||||
return '_run' in schema || (typeof schema === 'function' && 'schema' in schema)
|
||||
}
|
||||
|
||||
export async function getValibotError(
|
||||
export async function getValibotErrors(
|
||||
state: any,
|
||||
schema: ValibotSchema | ValibotSchemaAsync | ValibotSafeParser<any, any> | ValibotSafeParserAsync<any, any>
|
||||
): Promise<FormError[]> {
|
||||
@@ -82,3 +83,18 @@ export async function getValibotError(
|
||||
message: issue.message
|
||||
})) || []
|
||||
}
|
||||
|
||||
export function isStandardSchema(schema: any): schema is StandardSchema {
|
||||
return '~standard' in schema
|
||||
}
|
||||
|
||||
export async function getStandardErrors(
|
||||
state: any,
|
||||
schema: StandardSchema
|
||||
): Promise<FormError[]> {
|
||||
const result = await schema['~validate']({ value: state })
|
||||
return result.issues?.map(issue => ({
|
||||
name: issue.path?.map(item => typeof item === 'object' ? item.key : item).join('.') || '',
|
||||
message: issue.message
|
||||
})) || []
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user