Skip to content

Commit 723cf36

Browse files
fix(Form): remove joi and yup in favor of @standard-schema/spec (#5035)
Co-authored-by: Benjamin Canac <[email protected]>
1 parent 355d66c commit 723cf36

File tree

5 files changed

+13
-137
lines changed

5 files changed

+13
-137
lines changed

docs/content/docs/2.components/form.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ links:
99

1010
## Usage
1111

12-
Use the Form component to validate form data using validation libraries such as [Valibot](https://github.com/fabian-hiller/valibot), [Zod](https://github.com/colinhacks/zod), [Yup](https://github.com/jquense/yup), [Joi](https://github.com/hapijs/joi), [Superstruct](https://github.com/ianstormtaylor/superstruct) or your own validation logic.
12+
Use the Form component to validate form data using any validation library supporting [Standard Schema](https://github.com/standard-schema/standard-schema) such as [Valibot](https://github.com/fabian-hiller/valibot), [Zod](https://github.com/colinhacks/zod), [Yup](https://github.com/jquense/yup), [Joi](https://github.com/hapijs/joi) or [Superstruct](https://github.com/ianstormtaylor/superstruct) or your own validation logic.
1313

1414
It works with the [FormField](/docs/components/form-field) component to display error messages around form elements automatically.
1515

@@ -18,7 +18,7 @@ It works with the [FormField](/docs/components/form-field) component to display
1818
It requires two props:
1919

2020
- `state` - a reactive object holding the form's state.
21-
- `schema` - any [Standard Schema](https://standardschema.dev/) or a schema from [Yup](https://github.com/jquense/yup), [Joi](https://github.com/hapijs/joi) or [Superstruct](https://github.com/ianstormtaylor/superstruct).
21+
- `schema` - any [Standard Schema](https://github.com/standard-schema/standard-schema) or [Superstruct](https://github.com/ianstormtaylor/superstruct).
2222

2323
::warning
2424
**No validation library is included** by default, ensure you **install the one you need**.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,12 +175,12 @@
175175
},
176176
"peerDependencies": {
177177
"@inertiajs/vue3": "^2.0.7",
178-
"joi": "^17.13.0",
178+
"joi": "^18.0.0",
179179
"superstruct": "^2.0.0",
180180
"typescript": "^5.6.3",
181181
"valibot": "^1.0.0",
182182
"vue-router": "^4.5.0",
183-
"yup": "^1.6.0",
183+
"yup": "^1.7.0",
184184
"zod": "^3.24.0 || ^4.0.0"
185185
},
186186
"peerDependenciesMeta": {

pnpm-lock.yaml

Lines changed: 3 additions & 43 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/runtime/types/form.ts

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import type { StandardSchemaV1 } from '@standard-schema/spec'
22
import type { ComputedRef, DeepReadonly, Ref } from 'vue'
3-
import type { Schema as JoiSchema } from 'joi'
4-
import type { ObjectSchema as YupObjectSchema } from 'yup'
53
import type { GetObjectField } from './utils'
64
import type { Struct as SuperstructSchema } from 'superstruct'
75

@@ -22,25 +20,19 @@ export interface Form<S extends FormSchema> {
2220
}
2321

2422
export type FormSchema<I extends object = object, O extends object = I>
25-
= | YupObjectSchema<I>
26-
| JoiSchema<I>
27-
| SuperstructSchema<any, any>
23+
= | SuperstructSchema<any, any>
2824
| StandardSchemaV1<I, O>
2925

3026
// Define a utility type to infer the input type based on the schema type
3127
export type InferInput<Schema> = Schema extends StandardSchemaV1 ? StandardSchemaV1.InferInput<Schema>
32-
: Schema extends YupObjectSchema<infer I> ? I
33-
: Schema extends JoiSchema<infer I> ? I
34-
: Schema extends SuperstructSchema<infer I, any> ? I
35-
: Schema extends StandardSchemaV1 ? StandardSchemaV1.InferInput<Schema>
36-
: never
28+
: Schema extends SuperstructSchema<infer I, any> ? I
29+
: Schema extends StandardSchemaV1 ? StandardSchemaV1.InferInput<Schema>
30+
: never
3731

3832
// Define a utility type to infer the output type based on the schema type
3933
export type InferOutput<Schema> = Schema extends StandardSchemaV1 ? StandardSchemaV1.InferOutput<Schema>
40-
: Schema extends YupObjectSchema<infer O> ? O
41-
: Schema extends JoiSchema<infer O> ? O
42-
: Schema extends SuperstructSchema<infer O, any> ? O
43-
: never
34+
: Schema extends SuperstructSchema<infer O, any> ? O
35+
: never
4436

4537
export type FormData<S extends FormSchema, T extends boolean = true> = T extends true ? InferOutput<S> : InferInput<S>
4638

src/runtime/utils/form.ts

Lines changed: 0 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,7 @@
11
import type { StandardSchemaV1 } from '@standard-schema/spec'
2-
import type { ValidationError as JoiError, Schema as JoiSchema } from 'joi'
3-
import type { ObjectSchema as YupObjectSchema, ValidationError as YupError } from 'yup'
42
import type { Struct } from 'superstruct'
53
import type { FormSchema, ValidateReturnSchema } from '../types/form'
64

7-
export function isYupSchema(schema: any): schema is YupObjectSchema<any> {
8-
return schema.validate && schema.__isYupSchema__
9-
}
10-
11-
export function isYupError(error: any): error is YupError {
12-
return error.inner !== undefined
13-
}
14-
155
export function isSuperStructSchema(schema: any): schema is Struct<any, any> {
166
return (
177
'schema' in schema
@@ -21,14 +11,6 @@ export function isSuperStructSchema(schema: any): schema is Struct<any, any> {
2111
)
2212
}
2313

24-
export function isJoiSchema(schema: any): schema is JoiSchema {
25-
return schema.validateAsync !== undefined && schema.id !== undefined
26-
}
27-
28-
export function isJoiError(error: any): error is JoiError {
29-
return error.isJoi === true
30-
}
31-
3214
export function isStandardSchema(schema: any): schema is StandardSchemaV1 {
3315
return '~standard' in schema
3416
}
@@ -55,33 +37,6 @@ export async function validateStandardSchema(
5537
}
5638
}
5739

58-
async function validateYupSchema(
59-
state: any,
60-
schema: YupObjectSchema<any>
61-
): Promise<ValidateReturnSchema<typeof state>> {
62-
try {
63-
const result = await schema.validate(state, { abortEarly: false })
64-
return {
65-
errors: null,
66-
result
67-
}
68-
} catch (error) {
69-
if (isYupError(error)) {
70-
const errors = error.inner.map(issue => ({
71-
name: issue.path ?? '',
72-
message: issue.message
73-
}))
74-
75-
return {
76-
errors,
77-
result: null
78-
}
79-
} else {
80-
throw error
81-
}
82-
}
83-
}
84-
8540
async function validateSuperstructSchema(state: any, schema: Struct<any, any>): Promise<ValidateReturnSchema<typeof state>> {
8641
const [err, result] = schema.validate(state)
8742
if (err) {
@@ -102,40 +57,9 @@ async function validateSuperstructSchema(state: any, schema: Struct<any, any>):
10257
}
10358
}
10459

105-
async function validateJoiSchema(
106-
state: any,
107-
schema: JoiSchema
108-
): Promise<ValidateReturnSchema<typeof state>> {
109-
try {
110-
const result = await schema.validateAsync(state, { abortEarly: false })
111-
return {
112-
errors: null,
113-
result
114-
}
115-
} catch (error) {
116-
if (isJoiError(error)) {
117-
const errors = error.details.map(issue => ({
118-
name: issue.path.join('.'),
119-
message: issue.message
120-
}))
121-
122-
return {
123-
errors,
124-
result: null
125-
}
126-
} else {
127-
throw error
128-
}
129-
}
130-
}
131-
13260
export function validateSchema<T extends object>(state: T, schema: FormSchema<T>): Promise<ValidateReturnSchema<typeof state>> {
13361
if (isStandardSchema(schema)) {
13462
return validateStandardSchema(state, schema)
135-
} else if (isJoiSchema(schema)) {
136-
return validateJoiSchema(state, schema)
137-
} else if (isYupSchema(schema)) {
138-
return validateYupSchema(state, schema)
13963
} else if (isSuperStructSchema(schema)) {
14064
return validateSuperstructSchema(state, schema)
14165
} else {

0 commit comments

Comments
 (0)