How to validate input variables in Terraform CDKTF?

I am using Typescript and I am trying to use Input Variables validation but don’t know how to do it. Can I use regex and or javascript built in functions?

		const poolCidrMask = new TerraformVariable(this, "poolCidrMask", {
			type: "list",
			description: "List of Pool CIDRs Subnet Mask",
			validation: {
				condition: ?? How to refer to the variable? 
			}
		});

You can manually put var.<qualified var name> in the validation block, but it’s probably easier to instead use the addValidation method after creation since at that point you have a reference to the variable.
For example:

  const variable = new TerraformVariable(stack, "test-variable", {
    type: "string",
  });
  variable.addValidation({
    condition: `${Fn.lengthOf(variable.fqn)} > 4 && ${Fn.substr(
      variable.fqn,
      0,
      4
    )} == "ami-"`,
    errorMessage:
      'The image_id value must be a valid AMI id, starting with "ami-".',
  });
1 Like