Creating your first JOI extension

So this is going to be a super short primer for people who’re trying to write JOI extensions. I recently wrote an extension to validate mongodb object IDs and I’ll document the steps for the same here; Let’s get started

Joi uses something called as extension objects to “extend” its functionality.
These objects typically look something like this:

now, we can either pass this object as an argument to the Joi.extend method or create a function that returns an object like that 👆
Joi says you can also pass an array of a combination of the two; I haven’t tried it.

Coming to the object-id extension, we know that object-ids are essentially strings. So we can use the Joi.string methods as the base. For the name, let’s call it dbId. Why? We could club multiple rules here. Say you also have a postgres table that uses UUID as ids. 🤷‍♂
For now, though, let’s stick to creating just one rule; for mongoDb, mongoid.

How do we validate whether a string is a valid mongo id?

Simple; If you’re using mongoose as your ODM, it exposes a function where you can test whether a value is a valid Mongo Type. Read more about it here.

The function we can use here is the mongoose.Types.ObjectId.isValid. As obvious, it returns a true/false depending on the value passed.

Plugging all the pieces together

Now, we have everything required to create our first extension object factory method. It should look something like this:

Now all you’ve got to do is extend the original Joi object with the factory method and you’re good to go 👍

Yep; that’s all there is to it;
You can find the code and npm link for this package here:

Cheers! ☕️

--

--

@szanwar22 on twitter :D

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store