dotnet / templating

This repo contains the Template Engine which is used by dotnet new
https://docs.microsoft.com/dotnet/
MIT License
1.61k stars 368 forks source link

Validating parameter using RegExp #2038

Open grahamehorner opened 5 years ago

grahamehorner commented 5 years ago

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

nickwesselman commented 4 years ago

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.

KathleenDollard commented 3 years ago

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:

Anything else?

phenning commented 1 year ago

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.”

vlada-shubina commented 1 year ago

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

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?

baronfel commented 1 year ago

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.

phenning commented 1 year ago

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

A

joeloff commented 1 year ago

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?

baronfel commented 1 year ago

@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.

vlada-shubina commented 1 year ago

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.