-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Adding errormessage property to ValidatePatternAttribute #2728
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding errormessage property to ValidatePatternAttribute #2728
Conversation
|
Hi @powercode, I'm your friendly neighborhood Microsoft Pull Request Bot (You can call me MSBOT). Thanks for your contribution! The agreement was validated by Microsoft and real humans are currently evaluating your PR. TTYL, MSBOT; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is done a discrimination of attributes? :-)
Why not move it to the base class ValidateArgumentsAttribute?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the answer there is localization. For the other attributes it is easy to provide a clear message to the user, but in this case it is not possible.
But it is a good point. Maybe there are times when you want other validation messages than the hardcoded ones.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The localization is not problem because of ErrorMessage is custom message and if it is absent an user will receive the default localized message.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can see wanting a custom message with a few others. ValidateScript if you don't want to throw, and ValidateSet if you have too many possibilities and a shorter message is clearer.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added ErrorMessage to ValidateScript and ValidateSet too as suggested by @lzybkr
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you can specify ErrorMessage and it might be ignored, that's poor UX because then you must teach people where it is honored.
I see a bunch of custom validation attributes in this query: https://github.com/search?q=ValidateArgumentsAttribute&type=Code&utf8=%E2%9C%93
Based on the number of interesting hits, I think it would be a mistake to add ErrorMessage to ValidateArgumentsAttribute.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's stake out beginning position. Whether bad UX is that here we add ErrorMessage to only one attribute?
If Yes, then we can search for how to do it correctly for all validate attributes in the repo.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you mean all public validation attributes, then maybe, but I'm not fully convinced.
With a custom error message, it's possible for a cmdlet author to provide a worse error message than the default, especially without localized messages.
For a few of the attributes, I can't convince myself it's worth it. And there are minor costs - an extra pointer that is never used isn't free - it requires more memory and extra time in garbage collection checking if the (usually null) value needs collecting.
ValidateNotNullOrEmpty stands out as one example where I wouldn't bother. ValidateDrive is also probably not worth it.
And last - I can see a custom error message being useful on ValidateRange, e.g. to say the value should be a positive value or whatever, but if we go that route, we need localization support similar to HelpMessageBaseName and HelpMessageResourceId in ParameterAttribute.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you mean all public validation attributes, then maybe, but I'm not fully convinced.
Yes.
With a custom error message, it's possible for a cmdlet author to provide a worse error message than the default, especially without localized messages.
Yes, but it is a problem of any public interface that can be used not the way we want. The only thing we can do is make the interface simple and intuitive.
And there are minor costs - an extra pointer that is never used isn't free - it requires more memory and extra time in garbage collection checking if the (usually null) value needs collecting.
I think this is a minor problem, while we do not generate millions of attributes and not delete them dynamically.
ValidateNotNullOrEmpty stands out as one example where I wouldn't bother. ValidateDrive is also probably not worth it.
And last - I can see a custom error message being useful on ValidateRange , e.g. to say the value should be a positive value or whatever
Our standard messages are messages of a programmer, they is the exact pronunciation of what attribute checks. They do not contain user-defined semantics. The creator of cmdlet can give its own semantics absolutely any attribute.
Ex.: ValidateNotNullOrEmpty - Please specify how much virtual memory you need for virtual machine.
ValidateDrive - Please specify the drive where connected Game Key.
we need localization support similar to HelpMessageBaseName and HelpMessageResourceId in ParameterAttribute .
Great! I agree.
The only question is whether we can do it without breaking change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Half-serious, but can we introduce one more class in the hirarchy, like ValidateArgumentsWithErrorMessageAttribute?
dcd0183 to
bbb9218
Compare
|
@TravisEz13 When the label is Review - Waiting for author, I suspect I'm supposed to do something, but in this case I'm not sure what it is. What am I missing? |
|
@powercode Thanks, Just saying you don't know what you need to do will trigger the bot to change the tag. Someone should take a look at the PR soon. |
lzybkr
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for losing track of things and not merging this yet.
I'm fine with the code changes - I think selectively adding ErrorMessage is reasonable.
I'd like to see some tests, and at least a help comment in the setter so that someone knows what the arguments in the format string are.
|
Can we solve this another way? What if we added another field in the property attribute or introduced a new attribute just for the validation message? |
|
If you read the thread closely - I think your suggestion was proposed and I'm not in favor of it because it provides a false promise - a validation attribute may ignore the property. After further thought - there might be a way for the engine to check if an error message uses the I think this might be reasonable to implement, but it feels a little clunky. Because |
…dateScript
This makes it possibe to write for example
[ValidatePattern('[A-Z]:', ErrorMessage='The Drive should be specified as a single letter followed by a colon, for example "D:"')]
[string] $Drive,
The element being validated is also passed, so {0} can be used in the custom error message
0a0cc01 to
c0fe998
Compare
|
I had to make a change to Compiler to make it handle ErrorMessage property on the ValidateSetAttribute. |
c0fe998 to
a38d380
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All tests is similar so we can use "-TestCases"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How do I do that to change an attribute?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I attach a file with sample.
TestSample.txt
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$ErrorActionPreference ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch. Leftovers from earlier iteration. Removing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
'A;B;C' -> 'A,B,C'
3215abd to
af1800a
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indent.
af1800a to
e48dc90
Compare
|
@lzybkr comments addressed. |
Fixes #2719
Adding an ErrorMessage property to ValidatePatternAttribute so a message that
is meaningful to the user can be displayed, instead of a regular expression that
probably even the person who wrote will have a hard time understanding.