Skip to main content

Using Custom Logic

How to validate data?

Data validation is a crucial part of building a web application, and Express.js provides several ways to perform it. In this documentation, we will discuss the popular methods of data validation in Express.js with examples.

  1. Custom validation logic
  2. Mongoose validation
  3. Express-validator
  4. Joi validation
tip

Custom validation logic is the simplest way to validate data. But it is not recommended to use it in production. Because it is not scalable. If you have a lot of routes, then you will have to write a lot of validation logic. And it will be difficult to maintain the code. So, it is recommended to use a validation library like express-validator or Joi.

But here my goal is to explain the concept of data validation. So, I will use custom validation logic in this documentation.

Custom validation logic

The simplest way to validate data is to write custom validation logic. We can write our validation logic in the route handler. For example, let's say we have a route handler for creating a new user. We want to validate the data received in the request body before proceeding further. Validation can be like if the user has provided all the required fields, if the data is of the correct type, if the data is within a specific range, etc. Let's see how we can do this.

Validation using custom logic (if-else)
app.post("/register", (req, res) => {
const data = req.body;

// Check if all the required fields are present

if (!data.name) {
// If the name is not present, then return an error response
return res.status(400).json({
message: "Name is required",
});
}

if (!data.email) {
return res.status(400).json({
message: "Email is required",
});
}

if (!data.password) {
return res.status(400).json({
message: "Password is required",
});
}

// check if the data is of the correct type

if (typeof data.name !== "string") {
// If the name is not a string, then return an error response
return res.status(400).json({
message: "Name must be a string",
});
}

if (typeof data.email !== "string") {
return res.status(400).json({
message: "Email must be a string",
});
}

if (typeof data.password !== "string") {
return res.status(400).json({
message: "Password must be a string",
});
}

// check if the data is within a specific range

if (data.name.length < 3) {
// If the name is less than 3 characters, then return an error response
return res.status(400).json({
message: "Name must be at least 3 characters long",
});
}

if (data.password.length < 6) {
return res.status(400).json({
message: "Password must be at least 6 characters long",
});
}

// If all the above conditions are true, then the data is valid
// We can create the user in the database, etc. here

// do rest of the thing
});

This is a very simple example, but it is not very efficient. We have to write a lot of code to validate data. If we have to validate a lot of data, then it will be very difficult to write code for validation, and considering the number of APIs a web application may have, it will be very difficult to maintain the code. So, we need a better way to validate data.

What's next?

In the next section, we will learn how to validate data using Joi, Mongoose, and Express-validator. I want to write separate documentation for each of them, to keep the documentation simple and easy to understand.

Conclusion

In this documentation, we learned how to validate data using custom validation logic. In the next section, we will learn how to validate data using Joi, Mongoose, and Express-validator.