Schema-Based Form Validation in React.js

Schema-Based Form Validation in React.js

There are several libraries in the market, that can provide React.js projects with the schema-based form validation.

The top three of them are: Zod, Joi, and Yup.

Joi - Form Validation


In this post, I am going to describe one of the best called "Zod".

This library, at the moment, has more than 6M weekly downloads in NPM.

With Zod, we can define a validation schema as follows:

const schema = z.object({

name: z.string().min(3),

age: z.number().min(18),

});

By using Zod schema, we no longer need to define an interface like this:

interface FormData {

name: string;

age: number;

}

Yup form validator in React.js


We can extract the type of the form fields by using infer like this:

type FormData = z.infer<typeof schema>;

For connection of the zod schema to "react-hook-form" we need to install resolvers like this:

yarn add @hookform/resolvers

react hook form


To use it in our component, first, we need to import the zodResolver:

import { zodResolver } from "@hookform/resolvers/zod";

Zod - Form Handling Library


Note that, if you are using other libraries for form validation, such as Yup or Joi, the import statements are similar:

import { yupResolver } from '@hookform/resolvers/yup';

import { joiResolver } from '@hookform/resolvers/joi';

Next, we have to define it as the useForm hook config:

const {

register,

handleSubmit,

formState: { errors },

} = useForm<FormData>({ resolver: zodResolver(schema) });

And finally, the form input and its validation error message can be like this:

<input

type="text"

className="form-control"

id="name"

{...register("name")}

/>

{errors.name && <p className="text-danger">{errors.name.message}</p>}

How to use ZOD


Thanks for reading this article.

For more information, please refer to:

https://www.npmjs.com/package/yup

https://joi.dev/api/

https://www.npmjs.com/package/zod

#Joi #Zod #Yup #typescript #form_validation #react_hook_form


Ehsan Safari

Frontend Developer | React.js, Next.js | JavaScript, TypeScript

1 年
回复

要查看或添加评论,请登录

Ehsan Safari的更多文章

  • Tips For Better Next.js Development...!

    Tips For Better Next.js Development...!

    Hi, dear connections, In this article, I listed 5 tips to improve your Next.js Development.

    2 条评论
  • Call Stack Concept in JavaScript

    Call Stack Concept in JavaScript

    JavaScript is a single-threaded programming language which means that it has only one call stack that is used to…

  • Prevent XSS Attacks in React.js

    Prevent XSS Attacks in React.js

    Imagine that we have a blog variable that contains some HTML tags. If we display the blog directly inside the JSX…

    6 条评论
  • Updating objects in React State

    Updating objects in React State

    Just like Props, we should treat state objects as immutable (read-only). For example, in the following code, we cannot…

    4 条评论
  • How to integrate TinyMCE into a React.js App

    How to integrate TinyMCE into a React.js App

    When it comes to choosing the best text editor for our application, TinyMCE is one of the top rich text editors in the…

  • Image Loading Optimization in Nextjs

    Image Loading Optimization in Nextjs

    ?? Image Loading Optimization in Next.js: For your NEXT.

  • ????? ???? ???? ?? pagination ???? ?? ??? ?????

    ????? ???? ???? ?? pagination ???? ?? ??? ?????

    ???? pagination ?? ???? ??? useQuery ?? ?? ??? ??? ????? ???: useQuery(["posts", currentPage],() =>…

社区洞察

其他会员也浏览了