https://pulumi.com logo
#dotnet
Title
# dotnet
f

fast-dinner-32080

03/18/2020, 1:47 AM
How do I set a ComponentResource Args to be required or do conditional checks on the values? I found that I can add the [Input()] attribute on the ResourceArgs of my component but that doesn't seem to do anything. If I don't provide a value it just ends up throwing an error at the resource that was needing the properties value.
Adding a method that just wraps the args in tuple methods does the trick for validating complex requirements. Still trying to see if I can mark args as required vs throwing them all in an if statement.
t

tall-librarian-49374

03/18/2020, 6:46 AM
For custom resources, required properties are usually non-nullable while optional are nullable:
Copy code
[Input("applicationType", required: true)]
public Input<string> ApplicationType { get; set; } = null!;
        [Input("dailyDataCapInGb")]
public Input<double>? DailyDataCapInGb { get; set; }
For component resources, you could enforce the requirement e.g. in the constructor.
f

fast-dinner-32080

03/18/2020, 3:09 PM
Yea mine is a component resource that has a ResourceArgs type for it's arguments.
I guess building the ResourceArgs to be created using a constructor would make sense.
t

tall-librarian-49374

03/18/2020, 3:51 PM
Actually you don’t have to derive from ResourceArgs for compnent resource args AFAIK
f

fast-dinner-32080

03/18/2020, 3:51 PM
yea i figured that. I was just coping some examples trying to see what happens đŸ˜„
So I am using a combination of a constructor for all required fields and then a tuple with if statements to check dynamic fields. Would be nice if the required attribute worked on ComponentResource so they cannot be null.
t

tall-librarian-49374

03/18/2020, 6:46 PM
If I understand you correctly (a code gist could help), we have the same problem with all resources. In C# there’s no real way to mark a property as required.
f

fast-dinner-32080

03/18/2020, 6:47 PM
Yea its a pretty basic ComponentResource that takes an object Args with a bunch of fields. Just trying to make it reusable and include checks to make sure the fields are not null/empty when required for better feedback. Some providers (if in tuples) are even ignored if the property it needs is null without feedback.
In other C# development I have used the RequiredAttribute and similar to make sure class properties are required for like forms and databases, etc.
I wonder if something could be created for Pulumi resource argument properties.
t

tall-librarian-49374

03/18/2020, 8:34 PM
Interesting… How is that RequiredAttribute than used and by whom? Is it picked up by the editor?