Say ‘Yes’ to Yup (and Middy) — Simple Schema Validation for Node.js Lambda Handlers

Netanel Basal
Netanel Basal
Published in
2 min readApr 4, 2022

--

Client-side validation can prevent redundant requests to the server and provide immediate feedback to the user. Unfortunately, we can’t trust users’ input because they can easily bypass the client validation.

Therefore, adding a server-side validation is an essential requirement in every application. Let’s look at how we can create reusable middleware for our lambda handlers to produce robust and clean validations.

We’ll use middy to create the middleware and yup for our validations. Let’s create the middleware:

We want to validate both the body and the query string parameters. Each of them receives a yup schema that is validated by using the corresponding event data.

Errors are returned to the client to be displayed to our users. Now we can use it in our handlers:

We can easily add validation for any requirement we have with yup.

Follow me on Medium or Twitter to read more about Angular, JS and Serverless!

--

--

A FrontEnd Tech Lead, blogger, and open source maintainer. The founder of ngneat, husband and father.