Form Validation Utilities - Admin Components

Note: These utilities are available since Medusa v2.21.0.

The @medusajs/dashboard/lib subpath exports the Zod schemas and validation helpers that the Medusa Admin uses in its forms. Use them with react-hook-form and the Form component, as shown in the Forms guide, to validate your admin extension forms the same way the Medusa Admin does.

Code
1import {2  AddressSchema,3  EmailSchema,4  metadataFormSchema,5  optionalFloat,6  optionalInt,7  partialFormValidation,8} from "@medusajs/dashboard/lib"

optionalInt and optionalFloat#

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.

Both schemas use the Medusa Admin's translated error messages.

Code
1import { z } from "zod"2import {3  optionalFloat,4  optionalInt,5} from "@medusajs/dashboard/lib"6
7const schema = z.object({8  quantity: optionalInt,9  weight: optionalFloat,10})

Values#

Loading...
Note: Both schemas keep the value as a string when the user types one. Use castNumber to convert the validated value into a number before sending it to the Medusa backend.

metadataFormSchema#

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.

Code
1import { z } from "zod"2import { metadataFormSchema } from "@medusajs/dashboard/lib"3
4const schema = z.object({5  metadata: metadataFormSchema,6})

Row Properties#

Loading...

partialFormValidation#

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.

The function clears the errors of the passed fields, validates their current values, and sets an error on each field that fails.

Code
1import { z } from "zod"2import {3  partialFormValidation,4} from "@medusajs/dashboard/lib"5
6const schema = z.object({7  name: z.string().min(1),8  email: z.string().email(),9})10
11const onContinue = () => {12  const isValid = partialFormValidation(13    form,14    ["name", "email"],15    schema16  )17
18  if (isValid) {19    setStep("details")20  }21}

Parameters#

Loading...

Returns#

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.


AddressSchema and EmailSchema#

Zod schemas for the address and email fields that the Medusa Admin uses in its forms.

Code
1import { z } from "zod"2import {3  AddressSchema,4  EmailSchema,5} from "@medusajs/dashboard/lib"6
7const schema = z.object({8  shipping_address: AddressSchema,9}).merge(EmailSchema)

Values#

Loading...
Was this page helpful?
Ask Bloom
For assistance in your development, use Claude Code Plugins or Docs MCP server in Cursor, VSCode, etc...FAQ
What is Medusa?
How can I create a module?
How can I create a data model?
How do I create a workflow?
How can I extend a data model in the Product Module?
Recipes
How do I build a marketplace with Medusa?
How do I build digital products with Medusa?
How do I build subscription-based purchases with Medusa?
What other recipes are available in the Medusa documentation?
Chat is cleared on refresh
Line break
⇧↵