Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is JOI more popular than AJV?

Tags:

I am deciding on a validation library that I can use for both client and server side validation. I have been comparing JOI (+ joi-browser) and AJV.

As I understand it JOI and AJV can accomplish the same things. AJV can even perform async validation where as JOI cannot. It seems like AJV also validates the JSON schema written in the standardized format which is convenient, but it is not as developer friendly as JOI.

JOI has over twice as many stars and contributors as AJV.

Why is it that JOI is twice as popular as AJV?

like image 667
tkalis Avatar asked Jan 17 '19 03:01

tkalis


People also ask

Why is Ajv so fast?

Ajv generates code to turn JSON Schemas into super-fast validation functions that are efficient for v8 optimization. Currently Ajv is the fastest and the most standard compliant validator according to these benchmarks: json-schema-benchmark - 50% faster than the second place.

Does JSON have a Schema?

JSON has a schema. REST services have WADL. Also there are tools like wadl2java . Old question, but worth clarifying: The JSON Schema standard includes "hyper-schemas", which specify links/actions - including HTTP method, required data (specified as JSON Schema), and expected results.


1 Answers

The important difference between AJV and Joi is that AJV is a JSON Schema validator and Joi is a JavaScript validator. JSON Schema is cross-platform while Joi only works in JavaScript. So, this isn't a choice between AJV and Joi as much as it's a choice between JSON Schema and Joi.

Each approach has its trade-offs, so which you choose depends a lot on your specific situation.

AJV/JSON Schema

The big win you get from JSON Schema is that it's cross platform. JSON Schema validator implementations exist in every major programming language. No matter what language you choose, you can use the same JSON Schema on the frontend and the backend and get consistent validation results. Write once, validate anywhere.

The downside is that because it's cross platform, it's also somewhat limited in what it can do. It's purposely kept simple enough so that it's not too difficult to implement in any programming language. Because JSON Schema isn't a full fledged programming language, it's limited in what it can do.

You might choose AJV/JSON Schema if you are using something other than JavaScript on the backend or your app is a public API that could be used by any number of apps in any number of languages.

Joi

Joi's biggest advantage is its usability. It's easy to use, easy to extend, and it has the full power of JavaScript.

The downside is that if you want to reuse your validation logic on the frontend and the backend, your only choice of language on the backend is node.

You might choose Joi if you use JavaScript for the frontend and the backend and don't anticipate non-JavaScript clients that you need to support. This is a fairly narrow situation, but if it is your situation, you will likely get more out or Joi than AJV/JSON Schema.

Popularity

Github stars aren't a great measure of popularity. If you look at npm daily downloads you see a very different story (AJV: 18.9M, Joi: 2.2M). JSON Schema is by far more widely used because it is cross platform. AJV is just one implementation in one language. But, if your development stack fits Joi's particular niche, it can be just as good a choice or a better choice than JSON Schema.

like image 149
Jason Desrosiers Avatar answered Sep 22 '22 16:09

Jason Desrosiers