Typescript - validate aws cdk context json configuration

87 views Asked by At

I have sample aws cdk.context.json configuration:

{
  "projectName": "demo",
  "environments": {
    "default": {
      "account": "xxxxxxxxxxxxxx",
      "region": "us-east-1",
      "externalEndpoint": "https://external.endpoint.com",
      "optionalExternalEndpoint": "https://optional.external.endpoint.com",
      "isEnabled": false
    }
  }
}

In my code I'm getting this with this line:

  const environments = scope.node.tryGetContext('environments') as Environments | undefined;

I'd like to provide some type checking for these fields, also some optional fields checking. I thought about defining these types:

type EnvironmentConfiguration = {
  account: string;
  region: string;
  externalEndpoint: string;
  optionalExternalEndpoint: string;
  isEnabled: boolean;
};

type Environments = {
  [key: string]: EnvironmentConfiguration;
};

But even though I map my json content to Environments type there is no type checking at all. I can put anything as an 'isEnabled' boolean value. This solution somehow ignores it. How can I ensure that my json values corresponds to typescript types so that I can validate them?

0

There are 0 answers