Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Add ValidateNotNullOrWhiteSpace Attribute#17191

Merged
daxian-dbw merged 7 commits into
PowerShell:masterPowerShell/PowerShell:masterfrom
wmentha:ValidateNotNullOrWhiteSpaceAttributeCopy head branch name to clipboard
Aug 9, 2022
Merged

Add ValidateNotNullOrWhiteSpace Attribute#17191
daxian-dbw merged 7 commits into
PowerShell:masterPowerShell/PowerShell:masterfrom
wmentha:ValidateNotNullOrWhiteSpaceAttributeCopy head branch name to clipboard

Conversation

@wmentha

@wmentha wmentha commented Apr 25, 2022

Copy link
Copy Markdown
Contributor

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
    }
Loading

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)
    }
Loading

PR Checklist

@ghost

ghost commented Apr 25, 2022

Copy link
Copy Markdown

CLA assistant check
All CLA requirements met.

@wmentha wmentha requested a review from daxian-dbw as a code owner April 25, 2022 12:08
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 April 27, 2022 17:34
@daxian-dbw

daxian-dbw commented Apr 27, 2022

Copy link
Copy Markdown
Member

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

@ghost ghost added the Review - Needed The PR is being reviewed label May 5, 2022
@ghost

ghost commented May 5, 2022

Copy link
Copy Markdown

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
Copy Markdown
Member

@JamesWTruher gentle ping :)

@ghost ghost removed the Review - Needed The PR is being reviewed label May 5, 2022
@ghost ghost added the Review - Needed The PR is being reviewed label May 13, 2022
@ghost

ghost commented May 13, 2022

Copy link
Copy Markdown

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
Copy Markdown
Collaborator

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

@JamesWTruher JamesWTruher left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

wmentha commented Jul 26, 2022

Copy link
Copy Markdown
Contributor Author

@daxian-dbw gentle ping 😊

@daxian-dbw daxian-dbw left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment thread src/System.Management.Automation/engine/Attributes.cs Outdated
Comment thread src/System.Management.Automation/engine/Attributes.cs Outdated
Comment thread src/System.Management.Automation/engine/Attributes.cs Outdated
Comment thread src/System.Management.Automation/engine/Attributes.cs Outdated
Comment thread src/System.Management.Automation/engine/Attributes.cs Outdated
Comment thread src/System.Management.Automation/engine/Attributes.cs Outdated
Comment thread src/System.Management.Automation/engine/Attributes.cs Outdated
Comment thread src/System.Management.Automation/resources/Metadata.resx Outdated
Comment thread src/System.Management.Automation/resources/Metadata.resx Outdated
Comment thread test/powershell/engine/Basic/ValidateAttributes.Tests.ps1
@ghost ghost 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 August 9, 2022 11:07
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-deprecated

Copy link
Copy Markdown

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? 👍  :ok_hand:  :thumbsdown: (Email)
Customize PullRequestQuantifier for this repository.

@wmentha wmentha requested a review from daxian-dbw August 9, 2022 03:09
@wmentha

wmentha commented Aug 9, 2022

Copy link
Copy Markdown
Contributor Author

@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

@daxian-dbw daxian-dbw left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks @wmentha for the contribution!

@daxian-dbw daxian-dbw merged commit d75d3b6 into PowerShell:master Aug 9, 2022
@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 August 10, 2022 23:11
@TravisEz13 TravisEz13 mentioned this pull request Sep 30, 2022
22 tasks
@ghost

ghost commented Dec 20, 2022

Copy link
Copy Markdown

🎉v7.4.0-preview.1 has been released which incorporates this pull request.:tada:

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

Morty Proxy This is a proxified and sanitized view of the page, visit original site.