The Power of Accurate Data: Unlocking the Potential of Schema Validators

In today’s digital landscape, data reigns supreme. As developers, we create dynamic applications that respond to user input, but users don’t always provide information in the format we need. This is where schema validators come in – tools that define and validate the structure of data to ensure accuracy and consistency. In this article, we’ll delve into the world of Valibot, a cutting-edge JavaScript schema library that’s taking the development community by storm.

What is Valibot?

Released in July 2023, Valibot has quickly gained popularity, amassing over 3,500 GitHub stars. This lightweight library stands out from its more established counterparts, such as Yup and Zod, thanks to its optimized source code and modular API design. By allowing bundlers to import only the necessary code, Valibot reduces bundle size by up to 98% compared to Zod.

Features of Valibot

Valibot boasts an impressive range of features, including:

  • Small bundle size: Code splitting and modular design minimize bundle size
  • Validates everything: Supports various TypeScript types, including primitive values and complex objects
  • Includes helpers: Provides important validation and transformation helpers
  • Fully type-safe: Enjoy the benefits of type safety and static type inference in TypeScript
  • Offers great developer experience: API design is well thought out and influenced by Zod

Getting Started with Valibot

To get started with Valibot, simply install it using one of the following commands. Then, import the necessary methods into your app. Valibot provides a range of schemas, methods, validations, and transformations, including number, boolean, string, symbol, and object schemas, as well as parse, strict, and partial methods.

Exploring Valibot’s Functionalities

Let’s create a simple login schema using Valibot’s object schema. We’ll describe the structure of the data we expect, including an email and password, both strings. We can then use the string schema to ensure the email and password are provided in the correct format.

Handling Validation Errors

When the data received doesn’t match the expected format or structure, Valibot throws an error. We can customize the error message by passing an error text as an argument.

Chaining Validations

Valibot allows us to chain multiple validations together. We can create a pipeline of functions that the data being validated passes through synchronously. This pipeline can check the minimum lengths of the email and password, as well as validate the email using the email validator.

Setting Up Custom Validations

We can create custom validations for specific use cases. For example, let’s extend the LoginSchema to confirm that the password contains at least one uppercase letter, one number, and one special character. Valibot provides a custom validator that we can use to create a custom validation function.

Validating a Form with Valibot

For our final exploration, let’s use Valibot’s schema validation capabilities to validate a form. We’ll work with React Hook Form (RHF) and its Valibot resolver for form validation. First, set up the form, then add the LoginSchema. The schema is the same password chain validation schema we set up earlier, with one difference: we added some chain validation for the email.

With Valibot, we can create a seamless and efficient form validation experience. Its modular API and minimal size make it a viable option for developers looking to improve the performance of their applications. While its documentation is currently incomplete, we look forward to seeing how it evolves in the long term. Give Valibot a try in your next project and share your experience!

Leave a Reply