X Tutup
The Wayback Machine - https://web.archive.org/web/20221221054535/https://github.com/PowerShell/PowerShell/pull/17191
Skip to content
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 鈥淪ign up for GitHub鈥, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ValidateNotNullOrWhiteSpace Attribute #17191

Merged
merged 7 commits into from Aug 9, 2022
Merged

Add ValidateNotNullOrWhiteSpace Attribute #17191

merged 7 commits into from Aug 9, 2022

Conversation

wmentha
Copy link
Contributor

@wmentha wmentha commented Apr 25, 2022

PR Summary

Adds ValidateNotNullOrWhiteSpace to System.Management.Automation

PR Context

Closes #10010

Currently, users who want to validate a string or collection of strings will add the [ValidateNotNullOrEmpty()] attribute to it and then manually validate the string with [String]::IsNullOrWhiteSpace(my_string).

[ValidateNotNullOrEmpty()]
[String[]]
$string_collection = @("a", "b", " ", "d")

foreach ($str in $string_collection)
{
    if (-not [String]::IsNullOrWhiteSpace($str))
    {
        throw [System.ArgumentNullException] "Element consisted entirely of white-space characters"
    }
}

Alternate implentation @vexx32 suggested could have been a bool argument to ValidateNotNullOrEmpy: ValidateNotNullOrEmpty(WhiteSpace). However I have chosen to implement this as a seperate attribute as users who work with strings know to use a seperate attribute when validating them: ValidateNotNullOrEmpty as opposed to ValidateNotNull. ValidateNotNullOrEmpty adds a further requirement for the validation over ValidateNotNull. Likewise ValidateNotNullOrWhiteSpace adds the further requirement of no strings consisting entirely of white-space over ValidateNotNullOrEmpty.

Also, this is my first commit 馃槉

Class Diagram Before

classDiagram
    ValidateArgumentsAttribute <|-- NullValidationAttributeBase
    NullValidationAttributeBase <|-- ValidateNotNullAttribute
    NullValidationAttributeBase <|-- ValidateNotNullOrEmptyAttribute
    class ValidateArgumentsAttribute {
        <<abstract>>
        #Validate(object arguments, EngineIntrinsics engineIntrinsics)* void
    }
    class NullValidationAttributeBase {
        <<abstract>>
        #IsArgumentCollection(Type argumentType, out bool isElementValueType) bool
    }
    class ValidateNotNullAttribute{
        #Validate(object arguments, EngineIntrinsics engineIntrinsics) void
    }
    class ValidateNotNullOrEmptyAttribute{
        #Validate(object arguments, EngineIntrinsics engineIntrinsics) void
    }

Class Diagram After

classDiagram
    ValidateArgumentsAttribute <|-- NullValidationAttributeBase
    NullValidationAttributeBase <|-- ValidateNotNullAttribute
    NullValidationAttributeBase <|-- ValidateNotNullOr
    ValidateNotNullOr <|-- ValidateNotNullOrEmptyAttribute
    ValidateNotNullOr <|-- ValidateNotNullOrWhiteSpaceAttribute
    class ValidateArgumentsAttribute {
        <<abstract>>
        #Validate(object arguments, EngineIntrinsics engineIntrinsics)* void
    }
    class NullValidationAttributeBase {
        <<abstract>>
        #IsArgumentCollection(Type argumentType, out bool isElementValueType) bool
    }
    class ValidateNotNullAttribute{
        #Validate(object arguments, EngineIntrinsics engineIntrinsics) void
    }
    class ValidateNotNullOr{
        <<abstract>>
        #bool _whiteSpace
        #ValidateNotNullOr(bool whiteSpace)*
        #Validate(object arguments, EngineIntrinsics engineIntrinsics) void
    }
    class ValidateNotNullOrEmptyAttribute {
        +ValidateNotNullOrEmptyAttribute(bool whiteSpace)
    }
    class ValidateNotNullOrWhiteSpaceAttribute {
        +ValidateNotNullOrWhiteSpaceAttribute(bool whiteSpace)
    }

PR Checklist

@microsoft-cla-retired
Copy link

microsoft-cla-retired bot commented Apr 25, 2022

CLA assistant check
All CLA requirements met.

@wmentha wmentha requested a review from daxian-dbw as a code owner Apr 25, 2022
Acts identically to ValidateNotNullOrEmpty, except this attribute will now also check if argument consists entirely of white-space.
@daxian-dbw daxian-dbw requested a review from JamesWTruher Apr 27, 2022
@daxian-dbw
Copy link
Member

daxian-dbw commented Apr 27, 2022

@JamesWTruher Can you please take a look and see if this change is something we want to take? My concern is that any commands using this attribute won't work on downlevel PowerShell versions, which may make this attribute less interesting to module authors, until all downlevel versions are out of support.

@msftbot msftbot bot added the Review - Needed The PR is being reviewed label May 5, 2022
@msftbot
Copy link

msftbot bot commented May 5, 2022

This pull request has been automatically marked as Review Needed because it has been there has not been any activity for 7 days.
Maintainer, please provide feedback and/or mark it as Waiting on Author

@daxian-dbw
Copy link
Member

daxian-dbw commented May 5, 2022

@JamesWTruher gentle ping :)

@msftbot msftbot bot removed the Review - Needed The PR is being reviewed label May 5, 2022
@msftbot msftbot bot added the Review - Needed The PR is being reviewed label May 13, 2022
@msftbot
Copy link

msftbot bot commented May 13, 2022

This pull request has been automatically marked as Review Needed because it has been there has not been any activity for 7 days.
Maintainer, please provide feedback and/or mark it as Waiting on Author

@JamesWTruher
Copy link
Member

JamesWTruher commented May 23, 2022

I think this is fine. We have plenty of examples of new behaviors which will not be available down level.

@daxian-dbw daxian-dbw added the WG-Engine core PowerShell engine, interpreter, and runtime label Jun 6, 2022
Copy link
Member

@JamesWTruher JamesWTruher left a comment

while I understand that there is some concern about this with regard to forward behavior, i think there's enough value here to take this. We have a number of examples where we've do this. (null coalescing, etc)

@wmentha
Copy link
Contributor Author

wmentha commented Jul 26, 2022

@daxian-dbw gentle ping 馃槉

Copy link
Member

@daxian-dbw daxian-dbw left a comment

@wmentha Sorry for the long delay of review. The changes look good overall, thanks for that!
My comments are mainly about style changes and comment/error string improvements. Please take a look and let me know if you have any concerns.

src/System.Management.Automation/engine/Attributes.cs Outdated Show resolved Hide resolved
src/System.Management.Automation/engine/Attributes.cs Outdated Show resolved Hide resolved
src/System.Management.Automation/engine/Attributes.cs Outdated Show resolved Hide resolved
src/System.Management.Automation/engine/Attributes.cs Outdated Show resolved Hide resolved
src/System.Management.Automation/engine/Attributes.cs Outdated Show resolved Hide resolved
src/System.Management.Automation/engine/Attributes.cs Outdated Show resolved Hide resolved
src/System.Management.Automation/engine/Attributes.cs Outdated Show resolved Hide resolved
src/System.Management.Automation/resources/Metadata.resx Outdated Show resolved Hide resolved
src/System.Management.Automation/resources/Metadata.resx Outdated Show resolved Hide resolved
@msftbot msftbot bot added Waiting on Author The PR was reviewed and requires changes or comments from the author before being accept and removed Review - Needed The PR is being reviewed Waiting on Author The PR was reviewed and requires changes or comments from the author before being accept labels Aug 8, 2022
wmentha added 6 commits Aug 9, 2022
Added extra ValidateAttributesTests to check for scalar values.
Added extra ValidateAttributesTests to check for scalar values.
Added extra ValidateAttributesTests to check for scalar values.
@pull-request-quantifier
Copy link

pull-request-quantifier bot commented Aug 9, 2022

This PR has 76 quantified lines of changes. In general, a change size of upto 200 lines is ideal for the best PR experience!


Quantification details

Label      : Small
Size       : +67 -9
Percentile : 30.4%

Total files changed: 7

Change summary by file extension:
.cs : +32 -4
.resx : +6 -0
.ps1 : +28 -5
.csv : +1 -0

Change counts above are quantified counts, based on the PullRequestQuantifier customizations.

Why proper sizing of changes matters

Optimal pull request sizes drive a better predictable PR flow as they strike a
balance between between PR complexity and PR review overhead. PRs within the
optimal size (typical small, or medium sized PRs) mean:

  • Fast and predictable releases to production:
    • Optimal size changes are more likely to be reviewed faster with fewer
      iterations.
    • Similarity in low PR complexity drives similar review times.
  • Review quality is likely higher as complexity is lower:
    • Bugs are more likely to be detected.
    • Code inconsistencies are more likely to be detected.
  • Knowledge sharing is improved within the participants:
    • Small portions can be assimilated better.
  • Better engineering practices are exercised:
    • Solving big problems by dividing them in well contained, smaller problems.
    • Exercising separation of concerns within the code changes.

What can I do to optimize my changes

  • Use the PullRequestQuantifier to quantify your PR accurately
    • Create a context profile for your repo using the context generator
    • Exclude files that are not necessary to be reviewed or do not increase the review complexity. Example: Autogenerated code, docs, project IDE setting files, binaries, etc. Check out the Excluded section from your prquantifier.yaml context profile.
    • Understand your typical change complexity, drive towards the desired complexity by adjusting the label mapping in your prquantifier.yaml context profile.
    • Only use the labels that matter to you, see context specification to customize your prquantifier.yaml context profile.
  • Change your engineering behaviors
    • For PRs that fall outside of the desired spectrum, review the details and check if:
      • Your PR could be split in smaller, self-contained PRs instead
      • Your PR only solves one particular issue. (For example, don't refactor and code new features in the same PR).

How to interpret the change counts in git diff output

  • One line was added: +1 -0
  • One line was deleted: +0 -1
  • One line was modified: +1 -1 (git diff doesn't know about modified, it will
    interpret that line like one addition plus one deletion)
  • Change percentiles: Change characteristics (addition, deletion, modification)
    of this PR in relation to all other PRs within the repository.


Was this comment helpful? 馃憤 馃憣 馃憥 (Email)
Customize PullRequestQuantifier for this repository.

@wmentha wmentha requested a review from daxian-dbw Aug 9, 2022
@wmentha
Copy link
Contributor Author

wmentha commented Aug 9, 2022

@daxian-dbw Apologies for the mess of commits. I got tripped up on the new $totalAccelerators count since the addition of the ordered type accelerator for OrderedDictionary #17804

Copy link
Member

@daxian-dbw daxian-dbw left a comment

LGTM. Thanks @wmentha for the contribution!

@daxian-dbw daxian-dbw merged commit d75d3b6 into PowerShell:master Aug 9, 2022
38 of 39 checks passed
@daxian-dbw daxian-dbw added the CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log label Aug 9, 2022
@wmentha wmentha deleted the ValidateNotNullOrWhiteSpaceAttribute branch Aug 10, 2022
@TravisEz13 TravisEz13 mentioned this pull request Sep 30, 2022
22 tasks
@msftbot
Copy link

msftbot bot commented Dec 20, 2022

馃帀v7.4.0-preview.1 has been released which incorporates this pull request.馃帀

Handy links:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log Small WG-Engine core PowerShell engine, interpreter, and runtime
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Validation for strings that have more than just white space.
3 participants
X Tutup