templating icon indicating copy to clipboard operation
templating copied to clipboard

Validating parameter using RegExp

Open grahamehorner opened this issue 6 years ago • 10 comments

does the templating engine support validation of parameter values using regex? and/or displaying of error message valid regex is violated?

grahamehorner avatar Jul 31 '19 16:07 grahamehorner

Any sort of parameter validation beyond isRequired would be helpful. In my case, I'd like to validate that the parameter is a file of a particular name, which does exist.

nickwesselman avatar Jun 27 '20 20:06 nickwesselman

When we move to the System.CommandLine, we will be able to validate on type in the parser.

I think we should have additional validation in the backlog:

  • RegEx (probably)
  • Min/Max range

Anything else?

KathleenDollard avatar Apr 13 '21 17:04 KathleenDollard

Some thoughts I had when the Maui issue was brought up:

“ I think we would also want to do the validation at multiple layers. It would be useful if the core engine would do the validation but also be able to expose the validation rule and the error message so that hosts could pre-validate before passing the parameter to the core engine.”

phenning avatar Feb 23 '23 00:02 phenning

We need to consider implementing the regex validation in the following layers

  • dotnet new CLI
  • handling passed parameters in Edge (for a generic case)

The regex to use can be defined on parameter symbol configuration in template.json.

Some thoughts I had when the Maui issue was brought up:

“ I think we would also want to do the validation at multiple layers. It would be useful if the core engine would do the validation but also be able to expose the validation rule and the error message so that hosts could pre-validate before passing the parameter to the core engine.”

@phenning in case this feature is interested for VS, please let us know here. Once implemented you'll need to implement it in Visual Studio too. It is anticipated that regex for validation should be available via ITemplateInfo, and exception will be thrown on template creation if parameter value doesn't match the regex, but likely the better UX will be to handle it directly on UI. What do you think?

vlada-shubina avatar May 16 '23 14:05 vlada-shubina

We'd also need a feature like this (perhaps not just regex, but numeric range bounds, etc) to have a good interactive prompting experience. The prompt loop would want to accept input and validate it before continuing to the next symbol.

baronfel avatar May 16 '23 14:05 baronfel

I think it's a good idea. Are we thinking for 8.0 template engine, or for a 2/3xx?

A

phenning avatar May 16 '23 14:05 phenning

I assume we'd want the regex pattern to be any valid C# pattern., so we need to validate the expression first before using it to validate the input.

Question: What is the preferred behavior if the regex pattern in the JSON is an invalid regular expression?

joeloff avatar Jul 05 '23 20:07 joeloff

@joeloff at packaging/testing time we should log build warnings/errors if an invalid regex is used.

At instantiation-time we should treat it like any other invalid symbol input - it should be a CLI syntax error. We couldn't validate the input because the thing you asked us to validate with is borked.

baronfel avatar Jul 05 '23 20:07 baronfel

I assume we'd want the regex pattern to be any valid C# pattern., so we need to validate the expression first before using it to validate the input.

Question: What is the preferred behavior if the regex pattern in the JSON is an invalid regular expression?

@joeloff you can use https://github.com/dotnet/templating/issues/2623 logic to show validation issue during installation or/and instantiation time. I would recommend that in this case it is a warning, and regex validation is just skipped in case of invalid regex.

vlada-shubina avatar Jul 10 '23 10:07 vlada-shubina