Validate the input of your serverless function with 1 line of code

November 07, 2020

simple runtime check

typescript-is is a library which enables type checks at run-time! This is an incredible tool to validate input and make your code more type-safe 🎉.

Validating the input of cloud functions can be a challenging problem. Re-using your types to ensure that your input matches your type solves a big part of the validation problem. To do this with the typescript-is library, the only thing you need to do is to use the assertType method that typescript-is exports:

// 2. Use the `assertType` method to perform your runtime check
assertType<MyEvent>(input)

Above we use assertType to check whether our runtime object input matches our type MyEvent. If it doesn’t match the MyEvent type, an error is thrown. typescript-is has a bunch of other methods such as is or strictEqual. If you’d like to throw the error yourself you could do this for example:

if (!is<MyEvent>(input)) {
  throw new Error('input does not match type')
}

That’s pretty much all it takes to add a run-time type check of your cloud functions input (provided you are a typescript user 🙃). Whereas previously you might have reached for validation libraries such as joi or god-forbid, written your own validator, it’s now just one line of code. What are you going to do with all this new-found time?

Below is the entirety of an AWS lambda function handler with this pattern applied:

import { Handler } from 'aws-lambda'
import { assertType } from 'typescript-is';

type MyEvent = {
  msg: string
}

async function handler<Handler>(event: MyEvent) {
  assertType<MyEvent>(event)
  return { msg: 'Hello World' }
}

exports.handler = handler

🚀

For a fully working example of this pattern including deployment scripts for AWS and compilation with webpack please have a look at this boilerplate I’ve put together.


👋 Thanks for reading

I build full-stack serverless applications on AWS for businesses and start-ups.
Want to talk shop? Fill out the questionaire or book a slot and I'll get back to you.