◐ Shell
clean mode source ↗

try-json: Fix results to avoid false positives for constructs like OneOf or AnyOf by sotteson1 · Pull Request #24577 · PowerShell/PowerShell

PR Summary

Fixes #21471
When calling the API to get the results against the schema, ask for the results in hierarchical format. Then walk down the tree and skip any nodes and its children if IsValid is true.

PR Context

See #21471
When using a construct like OneOf, each json node that uses it will have results nodes like this:
OneOf - true
--Choice1 - false
--Choice2- true
OneOf - true
--Choice1 - true
--Choice2- false

The 'false' items aren't actually failures. They're just the result of checking each OneOf choice. But if you then add a node where the OneOf actually fails:
OneOf - false
--Choice1 - false
--Choice2- false

When you call try-json with the existing code, it includes all the "false" choices even if the parent was "true". This leads to a bunch of false positives and can make so much noise as to make try-json unsuable. The fix is to ask for the results in hierarchical format instead of list format. Then walk down the tree and skip any node and its children if IsValid is true.

Some real-world output:

Before the fix (the only invalid json node is /Devices/3):

Test-Json: The JSON is not valid with the schema: Expected 1 matching subschema but found 0 at '/Devices/3'
Test-Json: The JSON is not valid with the schema: Required properties ["os"] are not present at '/Devices/0'
Test-Json: The JSON is not valid with the schema: Required properties ["arch"] are not present at '/Devices/1'
Test-Json: The JSON is not valid with the schema: Required properties ["os"] are not present at '/Devices/2'
Test-Json: The JSON is not valid with the schema: Required properties ["arch"] are not present at '/Devices/3'
Test-Json: The JSON is not valid with the schema: Expected "\u0022smartphone\u0022" at '/Devices/0/deviceType'
Test-Json: The JSON is not valid with the schema: Expected "\u0022laptop\u0022" at '/Devices/1/deviceType'
Test-Json: The JSON is not valid with the schema: Expected "\u0022smartphone\u0022" at '/Devices/2/deviceType'
Test-Json: The JSON is not valid with the schema: Value should match one of the values specified by the enum at '/Devices/3/os'
Test-Json: The JSON is not valid with the schema: Expected "\u0022laptop\u0022" at '/Devices/3/deviceType'

After the fix it correctly only shows errors from /Devices/3:

Test-Json: The JSON is not valid with the schema: Expected 1 matching subschema but found 0 at '/Devices/3'
Test-Json: The JSON is not valid with the schema: Value should match one of the values specified by the enum at '/Devices/3/os'
Test-Json: The JSON is not valid with the schema: Required properties ["arch"] are not present at '/Devices/3'
Test-Json: The JSON is not valid with the schema: Expected "\u0022laptop\u0022" at '/Devices/3/deviceType'

PR Checklist