Validating User/Computer Supplied Input

So I picked up a new project that looks like it might be a couple or three months long which is going to require a significant chunk of code to bump our current API along a version and has had me thinking.  A pattern I’ve been using lately looks a little bit like the below

validator is some set of functions defined in an external file that I use to process various aspects of the request’s body and then if successful can move along the pipeline.  This is obviously an improvement upon

But with the upcoming chunk of work, I really wanted something a little bit more elegant, maintainable and configurable.  A quick flashback for me a couple of months and I remembered driving home and listening to Nodeup where the topics was a former Paypal dev who talked about his experience with Hapi.  And that night I did some reading and I can remember liking their plugin architecture and one of those plugins was Joi

Joi describes itself like this “Object schema description language and validator for JavaScript objects.”  On the surface when I read this, I thought that sounds interesting.

Now fast forward a couple of months and here I am looking at an extensive project and not really liking my validation pattern.  Sure it’ll work.  And honestly, there are only a couple of us coding so it won’t kill us to have a little bit more verbose code but when I lay my head down at night, I’ll know it’s’ not quite what I want.   A few searches later and I found express-joi-validation

What I like about this module is that you can use it as middleware which means that it fits into the flow and isn’t applied after the flow.  In additional, you describe the validators as Joi schemas which might be my favorite part.

I don’t have a ton of examples yet, but just wanted to post to bring awareness.  If you find yourself needing something like I’ve described, take a look.  I continue to be impressed with the Node.js community and all of the cool things that people are willing to contribute.  I hope and plan as I get deeper into this journey that I’ll start contributing as well.  We get better together.



About the author

Ben Pyle is a software developer who enjoys solving problems and delighting customers with right quality software. He's a husband, father to 2 boys and avid golfer. In his free time he enjoys reading, playing lots of golf and hanging out with his family doing whatever they are into.

Leave a Reply

Your email address will not be published. Required fields are marked *