DEV Community

Viacheslav Snizhko
Viacheslav Snizhko

Posted on

AWS CDK context validation

AWS CDK gives us rather primitive tools to access its context. It is only possible to get context values one by one (with tryGetContext), they are not typed and not validated. I want to be able to get multiple values at once and have them validated so that I don't have to test each value if it's empty etc.

All of this we can achieve by using Zod and a small wrapper.

We'll create a function that takes Zod schema as an input, and returns us validated context values.

import {getValidatedContext, z} from 'zod-cdk-context'

const app = new cdk.App();

// {foo: string; bar?: string; baz: number}
const {foo, bar, baz} = getValidatedContext(app.node, {
  foo: z.string().min(1),
  bar: z.string().min(1).optional(),
  baz: z.coerce.number(),
})
Enter fullscreen mode Exit fullscreen mode

Note: Complete source code is below

Main idea is to use schema.shape to get schema keys and use them as context names

Object.keys(schema.shape).map((key) => node.tryGetContext(key))
Enter fullscreen mode Exit fullscreen mode

Also we need to narrow down possible validators of input schema to strings and numbers - context values can only be strings but we can also pass number as a string, then "coerce" them to numbers so we have number type in the end.

{
  [key: string]: z.ZodString | z.ZodNumber | z.ZodOptional<z.ZodString | z.ZodNumber>
}
Enter fullscreen mode Exit fullscreen mode

And that's all the interesting details. Here is complete source code of the function

import type { Node } from 'constructs'

import { z } from 'zod'

export const getValidatedContext = <
  T extends {
    [key: string]: z.ZodString | z.ZodNumber | z.ZodOptional<z.ZodString | z.ZodNumber>
  }
>(
  node: Node,
  schemaObject: T
): z.infer<z.ZodObject<T>> => {
  const schema = z.object(schemaObject)
  const obj = Object.fromEntries(
    Object.keys(schema.shape).map((key) => [key, node.tryGetContext(key)])
  )

  return schema.parse(obj)
}

Enter fullscreen mode Exit fullscreen mode

Or you can use my npm package:

> npm i zod-cdk-context
Enter fullscreen mode Exit fullscreen mode

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay