refactor(Form)!: drop explicit support for zod and valibot (#3617)

This commit is contained in:
Romain Hamel
2025-03-19 12:18:26 +01:00
committed by GitHub
parent 02184b016a
commit 9a4bb34d7d
9 changed files with 14 additions and 95 deletions

View File

@@ -1,9 +1,7 @@
import type { StandardSchemaV1 } from '@standard-schema/spec'
import type { ComputedRef, DeepReadonly, Ref } from 'vue'
import type { ZodSchema } from 'zod'
import type { Schema as JoiSchema } from 'joi'
import type { ObjectSchema as YupObjectSchema } from 'yup'
import type { GenericSchema as ValibotSchema, GenericSchemaAsync as ValibotSchemaAsync, SafeParser as ValibotSafeParser, SafeParserAsync as ValibotSafeParserAsync } from 'valibot'
import type { GetObjectField } from './utils'
import type { Struct as SuperstructSchema } from 'superstruct'
@@ -23,12 +21,7 @@ export interface Form<T extends object> {
}
export type FormSchema<T extends object> =
| ZodSchema
| YupObjectSchema<T>
| ValibotSchema
| ValibotSchemaAsync
| ValibotSafeParser<any, any>
| ValibotSafeParserAsync<any, any>
| JoiSchema<T>
| SuperstructSchema<any, any>
| StandardSchemaV1

View File

@@ -1,8 +1,6 @@
import type { StandardSchemaV1 } 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'
import type { GenericSchema as ValibotSchema, GenericSchemaAsync as ValibotSchemaAsync, SafeParser as ValibotSafeParser, SafeParserAsync as ValibotSafeParserAsync } from 'valibot'
import type { Struct } from 'superstruct'
import type { FormSchema, ValidateReturnSchema } from '../types/form'
@@ -23,10 +21,6 @@ export function isSuperStructSchema(schema: any): schema is Struct<any, any> {
)
}
export function isZodSchema(schema: any): schema is ZodSchema {
return schema.parse !== undefined
}
export function isJoiSchema(schema: any): schema is JoiSchema {
return schema.validateAsync !== undefined && schema.id !== undefined
}
@@ -35,10 +29,6 @@ export function isJoiError(error: any): error is JoiError {
return error.isJoi === true
}
export function isValibotSchema(schema: any): schema is ValibotSchema | ValibotSchemaAsync | ValibotSafeParser<any, any> | ValibotSafeParserAsync<any, any> {
return '_run' in schema || (typeof schema === 'function' && 'schema' in schema)
}
export function isStandardSchema(schema: any): schema is StandardSchemaV1 {
return '~standard' in schema
}
@@ -112,28 +102,6 @@ async function validateSuperstructSchema(state: any, schema: Struct<any, any>):
}
}
async function validateZodSchema(
state: any,
schema: ZodSchema
): Promise<ValidateReturnSchema<typeof state>> {
const result = await schema.safeParseAsync(state)
if (result.success === false) {
const errors = result.error.issues.map(issue => ({
name: issue.path.join('.'),
message: issue.message
}))
return {
errors,
result: null
}
}
return {
result: result.data,
errors: null
}
}
async function validateJoiSchema(
state: any,
schema: JoiSchema
@@ -161,44 +129,11 @@ async function validateJoiSchema(
}
}
async function validateValibotSchema(
state: any,
schema: ValibotSchema | ValibotSchemaAsync | ValibotSafeParser<any, any> | ValibotSafeParserAsync<any, any>
): Promise<ValidateReturnSchema<typeof state>> {
const result = await ('_run' in schema ? schema._run({ typed: false, value: state }, {}) : schema(state))
if (!result.issues || result.issues.length === 0) {
const output = ('output' in result
? result.output
: 'value' in result
? result.value
: null)
return {
errors: null,
result: output
}
}
const errors = result.issues.map(issue => ({
name: issue.path?.map((item: any) => item.key).join('.') || '',
message: issue.message
}))
return {
errors,
result: null
}
}
export function validateSchema<T extends object>(state: T, schema: FormSchema<T>): Promise<ValidateReturnSchema<typeof state>> {
if (isZodSchema(schema)) {
return validateZodSchema(state, schema)
if (isStandardSchema(schema)) {
return validateStandardSchema(state, schema)
} else if (isJoiSchema(schema)) {
return validateJoiSchema(state, schema)
} else if (isStandardSchema(schema)) {
return validateStandardSchema(state, schema)
} else if (isValibotSchema(schema)) {
return validateValibotSchema(state, schema)
} else if (isYupSchema(schema)) {
return validateYupSchema(state, schema)
} else if (isSuperStructSchema(schema)) {