|
1
|
--- |
|
2
|
sidebar_label: "Form Validation" |
|
3
|
--- |
|
4
|
|
|
5
|
import { TypeList } from "docs-ui" |
|
6
|
|
|
7
|
export const metadata = { |
|
8
|
title: `Form Validation Utilities - Admin Components`, |
|
9
|
} |
|
10
|
|
|
11
|
# {metadata.title} |
|
12
|
|
|
13
|
<Note> |
|
14
|
|
|
15
|
These utilities are available since Medusa [v2.21.0](https://github.com/medusajs/medusa/releases/tag/v2.21.0). |
|
16
|
|
|
17
|
</Note> |
|
18
|
|
|
19
|
The `@medusajs/dashboard/lib` subpath exports the [Zod](https://zod.dev/) schemas and validation helpers that the Medusa Admin uses in its forms. Use them with [react-hook-form](https://react-hook-form.com/) and the `Form` component, as shown in the [Forms guide](../../guides/forms/page.mdx), to validate your [admin extension](!docs!/learn/fundamentals/admin) forms the same way the Medusa Admin does. |
|
20
|
|
|
21
|
```tsx |
|
22
|
import { |
|
23
|
AddressSchema, |
|
24
|
EmailSchema, |
|
25
|
metadataFormSchema, |
|
26
|
optionalFloat, |
|
27
|
optionalInt, |
|
28
|
partialFormValidation, |
|
29
|
} from "@medusajs/dashboard/lib" |
|
30
|
``` |
|
31
|
|
|
32
|
--- |
|
33
|
|
|
34
|
## optionalInt and optionalFloat |
|
35
|
|
|
36
|
Zod schemas for optional number inputs that accept either a string or a number, since an input's value is a string. Both treat an empty string and `undefined` as valid, and both reject negative values. `optionalInt` also rejects values that aren't whole numbers. |
|
37
|
|
|
38
|
Both schemas use the Medusa Admin's translated error messages. |
|
39
|
|
|
40
|
```tsx |
|
41
|
import { z } from "zod" |
|
42
|
import { |
|
43
|
optionalFloat, |
|
44
|
optionalInt, |
|
45
|
} from "@medusajs/dashboard/lib" |
|
46
|
|
|
47
|
const schema = z.object({ |
|
48
|
quantity: optionalInt, |
|
49
|
weight: optionalFloat, |
|
50
|
}) |
|
51
|
``` |
|
52
|
|
|
53
|
### Values |
|
54
|
|
|
55
|
<TypeList |
|
56
|
types={[ |
|
57
|
{ |
|
58
|
name: "optionalInt", |
|
59
|
type: "`ZodEffects<ZodOptional<ZodUnion<[ZodString, ZodNumber]>>>`", |
|
60
|
description: "Validates that the value is a positive integer, allowing an empty string or `undefined`.", |
|
61
|
}, |
|
62
|
{ |
|
63
|
name: "optionalFloat", |
|
64
|
type: "`ZodEffects<ZodOptional<ZodUnion<[ZodString, ZodNumber]>>>`", |
|
65
|
description: "Validates that the value is a positive number, allowing an empty string or `undefined`.", |
|
66
|
}, |
|
67
|
]} |
|
68
|
/> |
|
69
|
|
|
70
|
<Note> |
|
71
|
|
|
72
|
Both schemas keep the value as a string when the user types one. Use [castNumber](../money-and-currency/page.mdx#castnumber) to convert the validated value into a number before sending it to the Medusa backend. |
|
73
|
|
|
74
|
</Note> |
|
75
|
|
|
76
|
--- |
|
77
|
|
|
78
|
## metadataFormSchema |
|
79
|
|
|
80
|
A Zod schema for the key-value metadata form that the Medusa Admin uses to edit a resource's `metadata` property. It validates an array of rows, where each row holds a key, a value, and flags that track how the row changed. |
|
81
|
|
|
82
|
```tsx |
|
83
|
import { z } from "zod" |
|
84
|
import { metadataFormSchema } from "@medusajs/dashboard/lib" |
|
85
|
|
|
86
|
const schema = z.object({ |
|
87
|
metadata: metadataFormSchema, |
|
88
|
}) |
|
89
|
``` |
|
90
|
|
|
91
|
### Row Properties |
|
92
|
|
|
93
|
<TypeList |
|
94
|
types={[ |
|
95
|
{ |
|
96
|
name: "key", |
|
97
|
type: "`string`", |
|
98
|
optional: false, |
|
99
|
description: "The metadata key.", |
|
100
|
}, |
|
101
|
{ |
|
102
|
name: "value", |
|
103
|
type: "`unknown`", |
|
104
|
optional: false, |
|
105
|
description: "The metadata value.", |
|
106
|
}, |
|
107
|
{ |
|
108
|
name: "isInitial", |
|
109
|
type: "`boolean | undefined`", |
|
110
|
optional: false, |
|
111
|
description: "Whether the row existed before the user edited the form.", |
|
112
|
}, |
|
113
|
{ |
|
114
|
name: "isDeleted", |
|
115
|
type: "`boolean | undefined`", |
|
116
|
optional: false, |
|
117
|
description: "Whether the user removed the row.", |
|
118
|
}, |
|
119
|
{ |
|
120
|
name: "isIgnored", |
|
121
|
type: "`boolean | undefined`", |
|
122
|
optional: false, |
|
123
|
description: "Whether the row is excluded from the submitted metadata.", |
|
124
|
}, |
|
125
|
]} |
|
126
|
/> |
|
127
|
|
|
128
|
--- |
|
129
|
|
|
130
|
## partialFormValidation |
|
131
|
|
|
132
|
Validates a subset of a form's fields against a Zod schema. Use it in a multi-step form to validate only the fields of the current step before moving to the next one. |
|
133
|
|
|
134
|
The function clears the errors of the passed fields, validates their current values, and sets an error on each field that fails. |
|
135
|
|
|
136
|
```tsx |
|
137
|
import { z } from "zod" |
|
138
|
import { |
|
139
|
partialFormValidation, |
|
140
|
} from "@medusajs/dashboard/lib" |
|
141
|
|
|
142
|
const schema = z.object({ |
|
143
|
name: z.string().min(1), |
|
144
|
email: z.string().email(), |
|
145
|
}) |
|
146
|
|
|
147
|
const onContinue = () => { |
|
148
|
const isValid = partialFormValidation( |
|
149
|
form, |
|
150
|
["name", "email"], |
|
151
|
schema |
|
152
|
) |
|
153
|
|
|
154
|
if (isValid) { |
|
155
|
setStep("details") |
|
156
|
} |
|
157
|
} |
|
158
|
``` |
|
159
|
|
|
160
|
### Parameters |
|
161
|
|
|
162
|
<TypeList |
|
163
|
types={[ |
|
164
|
{ |
|
165
|
name: "form", |
|
166
|
type: "`UseFormReturn<TForm>`", |
|
167
|
optional: false, |
|
168
|
description: "The form returned by react-hook-form's `useForm` hook.", |
|
169
|
}, |
|
170
|
{ |
|
171
|
name: "fields", |
|
172
|
type: "`FieldPath<any>[]`", |
|
173
|
optional: false, |
|
174
|
description: "The names of the fields to validate.", |
|
175
|
}, |
|
176
|
{ |
|
177
|
name: "schema", |
|
178
|
type: "`z.ZodSchema<any>`", |
|
179
|
optional: false, |
|
180
|
description: "The Zod schema to validate the fields against.", |
|
181
|
}, |
|
182
|
]} |
|
183
|
/> |
|
184
|
|
|
185
|
### Returns |
|
186
|
|
|
187
|
Returns a `boolean` that is `true` when every passed field is valid. When it returns `false`, it has already set the errors on the form. |
|
188
|
|
|
189
|
--- |
|
190
|
|
|
191
|
## AddressSchema and EmailSchema |
|
192
|
|
|
193
|
Zod schemas for the address and email fields that the Medusa Admin uses in its forms. |
|
194
|
|
|
195
|
```tsx |
|
196
|
import { z } from "zod" |
|
197
|
import { |
|
198
|
AddressSchema, |
|
199
|
EmailSchema, |
|
200
|
} from "@medusajs/dashboard/lib" |
|
201
|
|
|
202
|
const schema = z.object({ |
|
203
|
shipping_address: AddressSchema, |
|
204
|
}).merge(EmailSchema) |
|
205
|
``` |
|
206
|
|
|
207
|
### Values |
|
208
|
|
|
209
|
<TypeList |
|
210
|
types={[ |
|
211
|
{ |
|
212
|
name: "AddressSchema", |
|
213
|
type: "`ZodObject`", |
|
214
|
description: "An object schema that requires `first_name`, `last_name`, `address_1`, `city`, `postal_code`, and `country_code`, and optionally accepts `company`, `address_2`, `province`, and `phone`.", |
|
215
|
}, |
|
216
|
{ |
|
217
|
name: "EmailSchema", |
|
218
|
type: "`ZodObject`", |
|
219
|
description: "An object schema with a required `email` property that must be a valid email address.", |
|
220
|
}, |
|
221
|
]} |
|
222
|
/> |