Schema Validation

Learn how to validate your stepper with any schema library

Stepperize provides a way to add schemas to your steps with any schema library. This is very useful for working with forms and libraries such as React Hook Form, Conform, etc.

Let's see how we can create our validation schemas and have them within our stepper.

Zod

import {  } from "zod";
import {  } from "@stepperize/react";

const  = .({
  : .().(),
  : .().(8),
});

type  = .<typeof >;

const  = ({
  : "personal",
  : "Personal Information",
  : ,
});

ValiBot

import * as  from "valibot";
import {  } from "@stepperize/react";

const  = .({
  : .(.(), .()),
  : .(.(), .(8)),
});

type  = .<typeof >;

const  = ({
  : "personal",
  : "Personal Information",
  : ,
});

ArkType

import {  } from "arktype";
import {  } from "@stepperize/react";

const  = ({
  : "string.email",
  : "string >= 8",
});

type  = typeof .;

const  = ({
  : "personal",
  : "Personal Information",
  : ,
});

Working with multiple schemas and steps

Let's say we have a series of steps and we will use them with forms, each of which has a validation schema. All you have to do is add the schemas with the key of your choice, and that's it.

Let's see how it works in a practical example with Zod.

import {  } from "zod";
import {  } from "@stepperize/react";

const  = .({
  : .().(1),
  : .().(1),
});

const  = .({
  : .().(),
  : .().(8),
});

const  = .({});

const  = (
  {
    : "personal-info",
    : "Personal Information",
    : ,
  },
  {
    : "account",
    : "Account Information",
    : ,
  },
  {
    : "complete",
    : "Complete",
    : ,
  }
);

You've probably noticed that the last step has an empty schema. This is because stepperize infers all the steps, and when you want to send your currentStep.schema, one of them could be undefined. To avoid this, you can simply add an empty schema. Or, if you prefer, you can choose not to do this and validate for yourself whether the schema exists.

Edit on GitHub

Last updated on