Dwarves
Memo
Type ESC to close search bar

Validation with Zod

Zod is the TypeScript-first schema validation library with static type inference. It’s functional approach to data validation is parse-dont-validate-in-typescript which parses the data in order to validate and catch errors.

Why use Zod?

How to use Zod

Example using validate method

const z = require('zod')

// Define a schema for the input data
const schema = z.object({
  name: z.string(),
  phone_number: z.number().min(0).max(12),
  email: z.string().email(),
})

// Input data to be validated and coerced
const input = {
  name: 'Dwarves Foundation',
  phone_number: 123456,
  email: 'team@dwarves.foundation',
}

// Validate and coerce the input data
const data = schema.validate(input)
console.log(data)
/*
{
  name: 'Dwarves Foundation',
  phone_number: 123456,
  email: 'team@dwarves.foundation',
}
*/

In this example, we define a schema using the Zod library that specifies the types and constraints for the input data. The schema.validate(input) function is used to validate and coerce the input data to match the schema. If the input data is valid and meets all the constraints defined in the schema, it will be returned in a proper format, otherwise it will throw an error with the validation issues.

In this case, the input data is an object that has a name, phone number, and email address. According to the standard, the name and email should be strings, and the phone number should be a number between 0 and 12.

It also ensure that the email is valid email.

This way the application can be sure that the data it is receiving is in the correct format, and that any issues with the data will be caught early on.

Example using parse method

The parse() method in the Zod validation library is used to parse and validate input data, and return the parsed data in the correct format. It is similar to the validate() method, but it also removes any extra properties from the input data that are not defined in the schema.

const z = require('zod');

// Define a schema for the input data
const schema = z.object({
  name: z.string(),
  phone_number: z.number().min(0).max(12),
  email: z.string().email(),
});

// Input data to be parsed and validated
const input = {
  name: 'Dwarves Foundation',
  phone_number: 123456
  email: 'team@dwarves.foundation',
  extra_property: 'This should not be included in the output'
};

// Parse and validate the input data
const data = schema.parse(input);
console.log(data);
/*
{
  name: 'Dwarves Foundation',
  phone_number: 123456,
  email: 'team@dwarves.foundation',
}
*/

In this example, the input data contains an extra property extra_property which is not defined in the schema, it will be removed from the parsed data, resulting in a cleaner object without any unnecessary properties.

parse() method is useful when the input data may contain extra properties that are not needed by the application. It also ensures that the input data matches the schema and that any issues with the data will be caught early on.

It’s also worth mentioning that, like validate(), if the incoming data does not match the schema, parse() will throw an exception indicating the validity issues.

Zod vs Yup

Zod and Yup are both JavaScript libraries for data validation, but they have some key differences:

Both Zod and Yup are robust validation libraries, and the choice between them is based on your project’s specific demands, syntax preference, and the capabilities that you require.

References