zod-formik-adapter

repository·master·Indexed 41 days ago

https://github.com/robertlichtnow/zod-formik-adapter

A utility library version 1.1.0 that adapts Zod object validation for use with Formik. It provides the toFormikValidationSchema function for Formik's validationSchema prop and the toFormikValidate function for the validate prop, bridging Zod's schema-based validation with Formik's expected interfaces.

Tokens
1.2K
Snippets
6
Records
6
Agent score
1%

What's inside zod-formik-adapter

  1. Use toFormikValidationSchema for Formik validationSchema

    master

    To use a Zod schema with Formik's validationSchema prop (which is compatible with Yup-style validation), use the toFormikValidationSchema function. This is the recommended way to integrate Zod with Formik's built-in validation logic.

    import { z } from 'zod';
    import { Formik } from 'formik';
    import { toFormikValidationSchema } from 'zod-formik-adapter';
    
    const Schema = z.object({
      name: z.string(),
      age: z.number(),
    });
    
    const Component = () => (
      <Formik
        validationSchema={toFormikValidationSchema(Schema)}
      >
        {/* ... */}
      </Formik>
    );
  2. Use toFormikValidate for Formik validate prop

    master

    If you prefer to use Formik's validate prop instead of validationSchema, use the toFormikValidate function. This converts a Zod schema into a function that Formik calls to perform validation.

    import { z } from 'zod';
    import { Formik } from 'formik';
    import { toFormikValidate } from 'zod-formik-adapter';
    
    const Schema = z.object({
      name: z.string(),
      age: z.number(),
    });
    
    const Component = () => (
      <Formik
        validate={toFormikValidate(Schema)}
      >
        {/* ... */}
      </Formik>
    );
  3. Convert a Zod schema for Formik's validate prop using toFormikValidate()

    master

    Use toFormikValidate to wrap a Zod schema for use with Formik's validate prop. This function returns an asynchronous function that, when called with form values, returns an object of errors if validation fails. The returned error object uses dot-notation strings for keys (e.g., "user.email") and maps them to their respective error messages.

    import { toFormikValidate } from 'zod-formik-adapter';
    import { z } from 'zod';
    
    const schema = z.object({
      email: z.string().email(),
    });
    
    // Use this in your Formik component
    const validate = toFormikValidate(schema);
    
    // <Formik validate={validate} ... />
  4. Convert a Zod schema for Formik's validationSchema prop using toFormikValidationSchema()

    master

    Use toFormikValidationSchema to wrap a Zod schema so it can be passed directly to Formik's validationSchema prop. This function returns an object with an asynchronous validate method. If validation fails, it throws a ValidationError containing an inner array of errors, where each error includes a path (joined by dots) and a message.

    import { toFormikValidationSchema } from 'zod-formik-adapter';
    import { z } from 'zod';
    
    const schema = z.object({
      email: z.string().email(),
    });
    
    // Use this in your Formik component
    const validationSchema = toFormikValidationSchema(schema);
    
    // <Formik validationSchema={validationSchema} ... />
  5. Handle errors with the ValidationError class

    master

    When using toFormikValidationSchema, validation failures throw a ValidationError. You can catch this error and inspect the inner property to access specific field errors. Each item in the inner array contains:

    • path: A string representing the path to the field (e.g., "address.street").
    • message: The error message provided by Zod.
    import { ValidationError } from 'zod-formik-adapter';
    
    try {
      await validationSchema.validate(formData);
    } catch (err) {
      if (err instanceof ValidationError) {
        console.log(err.inner); // Array<{ path: string; message: string }>
      }
    }