Skip to content

Add -Path and -LiteralPath parameters to Test-Json cmdlet - #19042

Merged
Dongbo Wang (daxian-dbw) merged 33 commits into
PowerShell:masterfrom
ArmaanMcleod:test-json-file-parameter
Feb 21, 2023
Merged

Dongbo Wang (daxian-dbw) merged 33 commits into
PowerShell:masterfrom
ArmaanMcleod:test-json-file-parameter

Conversation

@ArmaanMcleod

@ArmaanMcleod Armaan Mcleod (ArmaanMcleod) commented Jan 27, 2023 •

Copy link
Copy Markdown
Contributor

PR Summary

Fixes #15544
Fixes #12882

Added -Path and -LiteralPath parameter to Test-Json cmdlet.

PR Context

Currently you can only pass a JSON string to Test-Json like so:

PS C:\> Test-Json -Json (Get-Content -Path data.json -Raw)

With this change you can now also pass a path:

PS C:\> Test-Json -Path data.json

Or with literal path:

PS C:\> Test-Json -LiteralPath '[data].json'

Which makes validating JSON from file paths much easier.

Current Syntax

Test-Json [-Json] <string> [<CommonParameters>]

Test-Json [-Json] <string> [[-Schema] <string>] [<CommonParameters>]

Test-Json [-Json] <string> [[-SchemaFile] <string>] [<CommonParameters>]

New Syntax

Test-Json [-Json] <string> [<CommonParameters>]

Test-Json [-Json] <string> [-Schema] <string> [<CommonParameters>]

Test-Json [-Json] <string> [-SchemaFile] <string> [<CommonParameters>]

Test-Json [-Path] <string> [<CommonParameters>]

Test-Json [-Path] <string> [-Schema] <string> [<CommonParameters>]

Test-Json [-Path] <string> [-SchemaFile] <string> [<CommonParameters>]

Test-Json [-LiteralPath] <string> [<CommonParameters>]

Test-Json [-LiteralPath] <string> [-Schema] <string> [<CommonParameters>]

Test-Json [-LiteralPath] <string> [-SchemaFile] <string> [<CommonParameters>]

Which does introduce a Bucket 4 breaking change: https://github.com/PowerShell/PowerShell/blob/master/docs/dev-process/breaking-change-contract.md#bucket-4-clearly-non-public

But given changing parameter sets are not technically public API then it seemed fine to reorganize them to allow the new -Path parameter. If there is an easier way to do the parameter sets please let me know 😄.

PR Checklist

@ArmaanMcleod
Armaan Mcleod (ArmaanMcleod) marked this pull request as ready for review January 27, 2023 13:46
@ArmaanMcleod Armaan Mcleod (ArmaanMcleod) changed the title Add Path parameter to Test-Json Add -Path parameter to Test-Json cmdlet Jan 28, 2023
@ArmaanMcleod Armaan Mcleod (ArmaanMcleod) changed the title Add -Path parameter to Test-Json cmdlet Add -Path and -LiteralPath parameters to Test-Json cmdlet Feb 2, 2023
@ArmaanMcleod
Armaan Mcleod (ArmaanMcleod) marked this pull request as ready for review February 2, 2023 14:02
@ArmaanMcleod

Armaan Mcleod (ArmaanMcleod) commented Feb 2, 2023 •

Copy link
Copy Markdown
Contributor Author

Ilya (@iSazonov) Thanks for the feedback. I've included a -LiteralPath parameter and made both path parameters use ValueFromPipelineByPropertyName. Also cleaned up the extra whitspace and generate the test json files on the fly using TestDrive:/.

Let me know what you think, I think it looks much better already 😄.

@ArmaanMcleod

Copy link
Copy Markdown
Contributor Author

One strange thing I've noticed is if you do Get-ChildItem -Path '.markdownlint.json' -File | Test-Json, it will still set the path as a literal path from pipeline parameter binding.

@iSazonov

Copy link
Copy Markdown
Collaborator

One strange thing I've noticed is if you do Get-ChildItem -Path '.markdownlint.json' -File | Test-Json, it will still set the path as a literal path from pipeline parameter binding.

ValueFromPipelineByPropertyName and PSPath alias for LiteralPath work. It is expected behavior.

@iSazonov Ilya (iSazonov) added CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log WG-Cmdlets-Utility labels Feb 4, 2023
@ArmaanMcleod

Copy link
Copy Markdown
Contributor Author

Closed and Reopened to restart CI

@ArmaanMcleod
Armaan Mcleod (ArmaanMcleod) force-pushed the test-json-file-parameter branch 2 times, most recently from d2fbe31 to 707c135 Compare February 8, 2023 12:53
@pull-request-quantifier-deprecated

Copy link
Copy Markdown

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


Quantification details

Label      : Medium
Size       : +144 -11
Percentile : 51%

Total files changed: 2

Change summary by file extension:
.cs : +59 -8
.ps1 : +85 -3

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.

@daxian-dbw Dongbo Wang (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!

@daxian-dbw
Dongbo Wang (daxian-dbw) merged commit 0be3eea into PowerShell:master Feb 21, 2023
@ArmaanMcleod
Armaan Mcleod (ArmaanMcleod) deleted the test-json-file-parameter branch February 21, 2023 19:55
@iSazonov

Copy link
Copy Markdown
Collaborator

Armaan Mcleod (@ArmaanMcleod) Thanks for your contribution!

@ghost

Copy link
Copy Markdown

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

Handy links:

Comment on lines +28 to +30
private const string JsonPathParameterSet = "JsonPath";
private const string JsonPathWithSchemaStringParameterSet = "JsonPathWithSchemaString";
private const string JsonPathWithSchemaFileParameterSet = "JsonPathWithSchemaFile";

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

"JsonPath" is really unfortunate naming as it suggests JSON Path, the query syntax.

@ArmaanMcleod Armaan Mcleod (ArmaanMcleod) May 2, 2023 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Greg Dennis (@gregsdennis) Agree, we could change parameter set names to something more suitable.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Please wait until my #18141 merges, or I can do it afterward. That change almost erased these due to a bad rebase.

Greg Dennis (gregsdennis) added a commit to gregsdennis/PowerShell that referenced this pull request May 2, 2023
@kilasuit Ryan Yates (kilasuit) added WG-Cmdlets general cmdlet issues and removed WG-Cmdlets-Utility labels Jul 17, 2026
Thatgfsj (Thatgfsj) pushed a commit to Thatgfsj-contribs/PowerShell that referenced this pull request Aug 6, 2026
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 Medium WG-Cmdlets general cmdlet issues

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Test-Json : Add parameter for JSON data file Test-Json should have a -JsonFile parameter

5 participants