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

Top comments (0)