-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Add parameter SchemaFile to Test-Json cmdlet
#11934
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
Add parameter SchemaFile to Test-Json cmdlet
#11934
Conversation
|
Signed CLA, added tests. |
src/Microsoft.PowerShell.Commands.Utility/commands/utility/TestJsonCommand.cs
Outdated
Show resolved
Hide resolved
src/Microsoft.PowerShell.Commands.Utility/commands/utility/TestJsonCommand.cs
Outdated
Show resolved
Hide resolved
src/Microsoft.PowerShell.Commands.Utility/commands/utility/TestJsonCommand.cs
Outdated
Show resolved
Hide resolved
test/powershell/Modules/Microsoft.PowerShell.Utility/assets/invalid_schema_definitions.json
Outdated
Show resolved
Hide resolved
Co-Authored-By: Ilya <darpa@yandex.ru>
Seems that "ResolveFilePath" doesn't throw in any conditions I could test. NJsonSchema will throw "System.IO.IOException" so we catch that.
| /// This class implements Test-Json command. | ||
| /// </summary> | ||
| [Cmdlet(VerbsDiagnostic.Test, "Json", HelpUri = "")] | ||
| [Cmdlet(VerbsDiagnostic.Test, "Json", DefaultParameterSetName = ParameterAttribute.AllParameterSets, HelpUri = "")] |
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.
@sdwheeler @SteveL-MSFT I wonder that we haven't HelpUri for the cmdlet.
src/Microsoft.PowerShell.Commands.Utility/commands/utility/TestJsonCommand.cs
Outdated
Show resolved
Hide resolved
src/Microsoft.PowerShell.Commands.Utility/commands/utility/TestJsonCommand.cs
Outdated
Show resolved
Hide resolved
|
We may also want to consider #11999 in tandem with this PR and the overall behaviour when a JSON input fails validation / schema. |
|
My initial thoughts was and I still believe that users want to see errors of the JSON validation. |
|
@iSazonov I would agree that there is potential value in being able to see that, but I think that having that as an explicit opt-in would be more useful. There's a lot of cases where you just want to test whether some input JSON is valid or not, and not do much else. For the cases where the full feedback is asked for, I would expect we output a rich object indicating success status and a proper list of structured data indicating any validation failures that were encountered, all on the one output object. The current state of having two separate and simultaneous modes of outputting data across two separate streams is, I think, much more difficult for folks to work with. |
I don't know what is a primary scenario. In interactive scenario users could get verbose output without extra switches and in script scenario we could do |
|
@iSazonov I don't think that changing behaviours between interactive use and script is ever a good idea. It should always be an option for the user to specify. Any kind of black magic like that will simply confuse users and make it more difficult to appropriately diagnose issues. Cmdlet behaviours should be consistent. |
|
This pull request has been automatically marked as Review Needed because it has been there has not been any activity for 7 days. |
|
@beatcracker If the PR is ready for review, please remove the WIP from the title. That helps me prioritize things. |
|
@adityapatwardhan I think it is. I've removed |
src/Microsoft.PowerShell.Commands.Utility/commands/utility/TestJsonCommand.cs
Outdated
Show resolved
Hide resolved
|
@beatcracker Can you also respond to comment about not rethrowing and writing error instead. |
Co-authored-by: Aditya Patwardhan <adityap@microsoft.com>
|
@adityapatwardhan, Sure, I just did that. As far as I understand, in that case we're supposed to throw. because this is invalid input. |
|
@PoshChan Please remind me in 1 hour |
SchemaFile to Test-Json cmdlet
|
@adityapatwardhan, this is the reminder you requested 1 hour ago |
|
@PoshChan please rebuild windows |
|
@adityapatwardhan, successfully started rebuild of |
|
@beatcracker Thank you for your contribution! |
|
@adityapatwardhan @beatcracker did we get an issue filed in the docs repo to ensure this is documented in time for 7.1.0? |
|
@vexx32 I don't think so. If you point me in the right direction, I could do that. Are we talking about PR to update Test-Json.md? |
|
@beatcracker yep! You can either submit a PR to add the information directly or file an issue so the docs team are aware and can sort it out as they're able. 🙂 |
* Test-Json: document SchemaFile parameter See PowerShell/PowerShell#11934 for details * Update reference/7.1/Microsoft.PowerShell.Utility/Test-Json.md Co-authored-by: Sean Wheeler <sean.wheeler@microsoft.com> * Add SchemaFile example * Reformatted to fix build error Fixed formatting to fit PlatyPS schema requirements * Fix duplicate JSON in SchemaFile example Co-authored-by: Sean Wheeler <sean.wheeler@microsoft.com>
|
🎉 Handy links: |
PR Summary
Add parameter
SchemaFileto theTest-Jsoncmdlet.PR Context
Current implementation allows JSON schema to be only passed as string. This makes it impossible to validate JSON files against the schema that includes definitions from separate files: https://json-schema.org/understanding-json-schema/structuring.html#reuse
{ "$ref": "definitions.json#/address" }The new parameter accepts literal path to the JSON schema file and allows JSON files to be validated against such schemas.
Notes
This PR currently lacks tests. I think that I need to extend Test-Json.Tests.ps1, but I'm not sure where to put JSON schema files with includes. Would assets folder be ok?Test-Jsonimplementation and there is Migrate Test-Json to System.Text.Json API #11397 that should be taken into account.PR Checklist
.h,.cpp,.cs,.ps1and.psm1files have the correct copyright headerWIP:or[ WIP ]to the beginning of the title (theWIPbot will keep its status check atPendingwhile the prefix is present) and remove the prefix when the PR is ready.