Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yup vs Joi for frontend validation

I want to implement frontend validation with either Yup or Joi.

From all the docs and articles that I've found, I've got to a couple of conclusions:

  • Joi has better performance
  • Joi is more popular for backend validation, while Yup is more popular for frontend validation
  • Joi has a lack of support on the frontend
  • Per official docs, Yup is leaner and built with client-side validation
  • Yup bundle size is ~2.5 times smaller than Joi - link

However, I didn't manage to find what Joi lacks in terms of support compared to Yup?

Right now, from all of these conclusions, it's choosing to either have a smaller bundle or better performance.

like image 296
Nikola Avatar asked Feb 02 '21 12:02

Nikola


People also ask

Should I use Joi or express validator?

Joi can be used for creating schemas (just like we use mongoose for creating NoSQL schemas) and you can use it with plain Javascript objects. It's like a plug n play library and is easy to use. On the other hand, express-validator uses validator. js to validate expressjs routes, and it's mainly built for express.

Can Joi be used in the browser?

It can be used for Node. js or in the browser by bundling with webpack or browserify.

What is Joi validator?

Hapi Joi is an object schema description language and validator for JavaScript objects. With Hapi Joi, we create blueprints or schemas for JavaScript objects (an object that stores information) to ensure validation of key information.

What is the difference between Joi and Yup?

Joi has better performance Joi is more popular for backend validation, while Yup is more popular for frontend validation Joi has a lack of support on the frontend Per official docs, Yup is leaner and built with client-side validation

Is it possible to use Joi on the frontend?

It was still possible to use Browserify on it, or use the unofficial joi-browser npm package, but the process was cumbersome enough that it was less common to see joi used on the frontend. Yup was basically a workaround for that.

How to use express-validator with Joi?

In Joi, you need to use Joi.object () to instantiate a Joi schema object to work with. All schemas require Joi.object () to process validation and other Joi features. You need to separately read req.body , req.params , req.query to request body, params, and query. You can just require express-validator and start using its methods.

Should I use Yup or yup for form validation?

I recommend using Yup if you do a lot of form validation because its extensive functions cover many patterns that are used in forms, even situational ones where you have to do a rounding.


Video Answer


2 Answers

In the past, it was true that joi lacked browser support, at least out of the box. It uses a few Node.js APIs, which aren't available in the browser, to implement some of its features. It was still possible to use Browserify on it, or use the unofficial joi-browser npm package, but the process was cumbersome enough that it was less common to see joi used on the frontend. Yup was basically a workaround for that.

However, that information is out of date, as joi now includes an official browser build that's easy to use and roughly the same size as yup. See its package.json: https://github.com/sideway/joi/blob/83092836583a7f4ce16cbf116b8776737e80d16f/package.json#L8

Your bundler, assuming it is set up correctly, should detect the browser build and use it automatically. For example, if you're using Rollup, make sure you are using @rollup/plugin-node-resolve with the browser: true option.

I would strongly recommend using joi on the frontend now as you can share schemas between frontend and backend, which is really fantastic.

like image 114
Seth Holladay Avatar answered Sep 21 '22 18:09

Seth Holladay


For the front-end, performance bundle size is more important rather than fast work (you do not need to make millions of validation on the client-side). As you mentioned: "Yup bundle size is ~2.5 times smaller than Joi - link"

So my choice is Yup.

But if you use joi for the back-end and going to share schemas with the front-end I agree with Seth Holladay.

like image 42
Konstantin Avatar answered Sep 23 '22 18:09

Konstantin