Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Custom Validation with Zod | Core Zod Integration in React Forms
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
Zod Forms in React

bookCustom Validation with Zod

When you need to implement custom validation logic that goes beyond Zod's built-in type and value checks, you can use the .refine() and .superRefine() methods. These methods allow you to add your own rules to a Zod schema.

The .refine() method lets you attach a custom validation function to a schema. This function receives the value being validated and should return true if the value is valid or false if not. You can also provide a custom error message to display when validation fails.

If you need to perform complex validation that may depend on multiple fields or want to add custom error paths, .superRefine() gives you more control. It provides access to a validation context, letting you add errors for specific paths or with custom messages.

Using .refine() is most common when you want to enforce a rule that cannot be expressed through Zod's standard schema types. For example, you might want to check that a password is strong enough, or that an email address does not belong to a disposable email provider.

Suppose you want to ensure that a password field in your form has at least 8 characters, contains both letters and numbers, and does not include the word "password". You can add this logic to your Zod schema using refine like this:

import { z } from "zod";

const passwordSchema = z.string().refine(
  (val) => {
    const hasMinLength = val.length >= 8;
    const hasLetter = /[a-zA-Z]/.test(val);
    const hasNumber = /[0-9]/.test(val);
    const doesNotContainPassword = !val.toLowerCase().includes("password");
    return hasMinLength && hasLetter && hasNumber && doesNotContainPassword;
  },
  {
    message:
      "Password must be at least 8 characters, contain letters and numbers, and not include the word 'password'."
  }
);

This schema uses refine to enforce all the password rules in a single validation step. If any of the conditions fail, the provided error message will be shown to the user. This approach keeps your custom validation logic close to your schema definition, making it easy to maintain and update as requirements change.

question mark

When should you use the .refine() method in a Zod schema?

Select the correct answer

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 2. Capitolo 3

Chieda ad AI

expand

Chieda ad AI

ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

Suggested prompts:

Can you show an example using `.superRefine()` for cross-field validation?

How do I display custom error messages to users when validation fails?

What are some best practices for writing custom validation logic with Zod?

bookCustom Validation with Zod

Scorri per mostrare il menu

When you need to implement custom validation logic that goes beyond Zod's built-in type and value checks, you can use the .refine() and .superRefine() methods. These methods allow you to add your own rules to a Zod schema.

The .refine() method lets you attach a custom validation function to a schema. This function receives the value being validated and should return true if the value is valid or false if not. You can also provide a custom error message to display when validation fails.

If you need to perform complex validation that may depend on multiple fields or want to add custom error paths, .superRefine() gives you more control. It provides access to a validation context, letting you add errors for specific paths or with custom messages.

Using .refine() is most common when you want to enforce a rule that cannot be expressed through Zod's standard schema types. For example, you might want to check that a password is strong enough, or that an email address does not belong to a disposable email provider.

Suppose you want to ensure that a password field in your form has at least 8 characters, contains both letters and numbers, and does not include the word "password". You can add this logic to your Zod schema using refine like this:

import { z } from "zod";

const passwordSchema = z.string().refine(
  (val) => {
    const hasMinLength = val.length >= 8;
    const hasLetter = /[a-zA-Z]/.test(val);
    const hasNumber = /[0-9]/.test(val);
    const doesNotContainPassword = !val.toLowerCase().includes("password");
    return hasMinLength && hasLetter && hasNumber && doesNotContainPassword;
  },
  {
    message:
      "Password must be at least 8 characters, contain letters and numbers, and not include the word 'password'."
  }
);

This schema uses refine to enforce all the password rules in a single validation step. If any of the conditions fail, the provided error message will be shown to the user. This approach keeps your custom validation logic close to your schema definition, making it easy to maintain and update as requirements change.

question mark

When should you use the .refine() method in a Zod schema?

Select the correct answer

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 2. Capitolo 3
some-alt