cfn-language-discussion icon indicating copy to clipboard operation
cfn-language-discussion copied to clipboard

A way to validate min length for List types

Open chkp-dmitrytc opened this issue 3 years ago • 6 comments

Name of the resource

Other

Resource name

ListAWS::EC2::AvailabilityZone::Name

Description

Hey All, I've looked everywhere in the documentation and examples in GitHub, but somehow I don't see this kind of validation anywhere. There are some use cases in which we want a user to choose a list of subnets with a min size of 2 for example, I can't see a way to validate that. Not with rules (which is less ideal) and not with a dedicated property.

It is possible to validate that the List<Resource> is not empty (with AllowedPattern) but not a min size of the list.

So the request is to add MinLength / MinSize to the List types (e.g. ListAWS::EC2::AvailabilityZone::Name). If someone got a workaround for the time being it would be greatly appreciated.

P.S. MinLength somehow works to validate non-empty lists but does not validate length for Lists.

Other Details

No response

chkp-dmitrytc avatar Mar 22 '22 12:03 chkp-dmitrytc

@chkp-dmitrytc Thank you very much for your feedback! Since this repository is focused on resource coverage, I'm transferring this issue over to a new GitHub repository dedicated to CloudFormation template language issues.

lejiati avatar May 10 '22 02:05 lejiati

I haven't tested this, but have you tried using an AllowedPattern?

AllowedPattern: '.*,.*'

benbridts avatar May 23 '22 11:05 benbridts

I believe I did, and I've also tried to play with length-dependent regex, without success. I tried: AllowedPattern: '.{27,}' (27 because each item in the list is 26 characters long) This did not work as well. I found out that: AllowedPattern: '.{26}' Matched when one item was selected and multiple items were selected. meaning, the system checks one item's pattern, even if multiple are selected.

chkp-dmitrytc avatar May 31 '22 05:05 chkp-dmitrytc

I don't want to transform this into a regex discussion, but ^.{26}$ should match exact 26 characters.

If you want to make the ,-based regex a bit more robust, you could use:

^([^,]{26},)+[^,]{26}$

Which translates to: From the start of the string (^), have at least one (+) group of 26 characters that are not a comma followed by a comma(([^,]{26},)). Right before the end of the string ($) have another group of 26 non-comma characters ([^,]{26}).

Or shorter: at least 2 times a group of 26 characters

benbridts avatar May 31 '22 08:05 benbridts

I forgot to write in the comment the ^ and $ but if I recall correctly that's how I've tested it :) I used the '.{27,}' to test if there was a string longer than 26 characters (27 and above), I purposely did not use , as a character to make it more generic.

So '.{27,}' always did not match, no matter how many items were selected. So my conclusion was that the pattern that is examined is of a single item (there was no string greater than 26 characters long no matter the selection).

chkp-dmitrytc avatar May 31 '22 09:05 chkp-dmitrytc

I see what you're saying, thanks for the clarification. A more hacky workaround is to use a String as parameter type, and use Fn::Split to convert it to a list in the Resources section

benbridts avatar May 31 '22 09:05 benbridts