DEV Community

Cover image for Validate your cloud function with 1 line of code
Julian Krispel-Samsel
Julian Krispel-Samsel

Posted on

3

Validate your cloud function with 1 line of code

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)
Enter fullscreen mode Exit fullscreen mode

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')
}
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

🚀

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, this post was originally published on my website.

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

Best practices for optimal infrastructure performance with Magento

Running a Magento store? Struggling with performance bottlenecks? Join us and get actionable insights and real-world strategies to keep your store fast and reliable.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️