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(),
})
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))
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>
}
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)
}
Or you can use my npm package:
> npm i zod-cdk-context
Top comments (0)