echoing-address-44214
03/29/2023, 1:42 AMpulumi.Input<string>
field in my constructor? Imagine the following code:
class RuwenTest extends pulumi.ComponentResource {
constructor(
name: string,
text: pulumi.Input<string>,
opts?: pulumi.ComponentResourceOptions
) {
super("ruwen:index:Test/Test", name, {}, opts);
// validate if text contains foo
pulumi.output(text).apply((t) => {
if (t.includes("foo")) {
throw new Error("text contains foo");
}
});
}
}
Since apply is not executed in pulumi preview, I will only see the error if I actually apply the change. I want to see it early. And I struggled to unit test the error.
What is the recommended approach?little-cartoon-10569
03/29/2023, 1:54 AMechoing-address-44214
03/29/2023, 1:58 AMResources should be able to assume valid input.I need to validate user supplied input somewhere. I can't limit everything down enough via the type system.
little-cartoon-10569
03/29/2023, 1:59 AMechoing-address-44214
03/29/2023, 2:05 AMlittle-cartoon-10569
03/29/2023, 2:05 AMechoing-address-44214
03/29/2023, 2:06 AMlittle-cartoon-10569
03/29/2023, 2:09 AMpulumi.Input<string>
is pulumi.Output<string> | string
, so you can do this (assuming you're in a constructor):
const toBeValidated = getValue();
if (toBeValidated instanceof string) {
if (!isValid(toBeValidated)) {
throw new ResourceError(`${toBeValidated} is invalid.`, this);
}
}
echoing-address-44214
03/29/2023, 2:12 AMlittle-cartoon-10569
03/29/2023, 2:12 AMechoing-address-44214
03/29/2023, 2:13 AMlittle-cartoon-10569
03/29/2023, 2:14 AMechoing-address-44214
03/29/2023, 2:14 AMlittle-cartoon-10569
03/29/2023, 2:15 AM