Form Validation Utilities - Admin Components
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.
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.
Values#
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.
Row Properties#
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.
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#
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.