Blitz is in beta! 🎉 1.0 expected in Q3 this year
Back to Documentation Menu

Utilities

Topics

Jump to a Topic

Blitz provides some handy utility functions

validateZodSchema

This utility function will validate input using a zod schema, and format any errors to be usable with your form library.

This is currently compatible with both React Final Form and Formik and any others with the same API.

Example

import {validateZodSchema} from 'blitz'

<FinalForm
  initialValues={initialValues}
  validate={validateZodSchema(schema)}
  ...

API

const validationFunction = validateZodSchema(MyZodSchema)

Arguments

  • ZodSchema: a zod schema
    • Required

Returns

A validation function to pass to a Form component's validate prop. It accepts some values and returns an object containing any errors.

(values: any) => Object

formatZodError

This utility function will take a ZodError and format it nicely to be usable with your form library.

This is currently compatible with both React Final Form and Formik and any others with the same API.

Example

import {formatZodError} from 'blitz'

<FinalForm
  initialValues={initialValues}
  validate={values => {
    try {
      schema.parse(values)
    } catch (error) {
      return formatZodError(error)
    }
  }}
  ...

API

const formattedErrorsObject = formatZodError(myZodError)

Arguments

  • ZodError: a zod error
    • Required

Returns

An object with errors that makes the same shape as the original schema


Idea for improving this page? Edit it on GitHub.