Fix Test-Json false positive errors when using oneOf or anyOf in schema#26618
Merged
iSazonov merged 2 commits intoDec 17, 2025
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes false positive validation errors in Test-Json when using JSON schemas with oneOf or anyOf constructs. Previously, all non-matching choices would report errors even when validation succeeded. The fix switches to hierarchical result processing and skips reporting errors for valid nodes, eliminating false positives while preserving actual validation errors.
Key Changes:
- Switched from List to Hierarchical output format for schema evaluation
- Added recursive error reporting that skips valid nodes and their children
- Added comprehensive test coverage for oneOf and anyOf scenarios
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
TestJsonCommand.cs |
Modified schema evaluation to use Hierarchical output format and added ReportValidationErrors method for recursive error processing with valid-node filtering |
Test-Json.Tests.ps1 |
Added 6 new test cases with oneOf/anyOf schemas covering valid and invalid device/port scenarios to verify false positives are eliminated |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
iSazonov
approved these changes
Dec 17, 2025
21 tasks
kilasuit
pushed a commit
to kilasuit/PowerShell
that referenced
this pull request
Jan 2, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR Summary
Fixes false positive validation errors in
Test-Jsonwhen using JSON schemas withoneOforanyOfconstructs by switching to hierarchical result processing and skipping valid nodes.Fixes #21471
Based on #24577 by @sotteson1
PR Context
Problem
When validating JSON against schemas using
oneOforanyOf,Test-Jsonreports validation errors for all non-matching choices, even when one choice successfully matches. This produces many false positive errors that make the output unusable for complex schemas.For example, with a
oneOfschema defining smartphone (requiresos) and laptop (requiresarch) choices, a valid laptop entry would incorrectly report "Required properties [os] are not present" because the validation against the smartphone subschema also runs and its failure is reported as an error.Solution
Changed the evaluation output format from
ListtoHierarchicaland added a newReportValidationErrorsmethod that recursively walks the result tree, skipping nodes (and their children) whereIsValidis true. This eliminates false positives from valid branches while still reporting actual validation errors.PR Checklist
.h,.cpp,.cs,.ps1and.psm1files have the correct copyright headerChanges Made
1.
TestJsonCommand.cs(+29 lines, -10 lines)OutputFormat.ListtoOutputFormat.Hierarchicalfor schema evaluationReportValidationErrorsmethodReportValidationErrorsmethod that recursively processes hierarchical results, skipping valid nodes and their children2.
Test-Json.Tests.ps1(+195 lines)oneOfschema with integer/string port pattern (simple case)oneOfschema with smartphone/laptop device types (original issue scenario)anyOfschema with smartphone/laptop device typesTotal: 2 files changed, 224 insertions(+), 10 deletions(-)
Behavior Examples
Before (false positives)
After (only relevant errors)
Testing
All 55 tests pass (51 existing + 6 new).
New Test Cases
Test-Json does not report false positives for valid oneOf matches- Valid ports array returns true with no errorsTest-Json reports only relevant errors for invalid oneOf values- Invalid port reports error only for that itemTest-Json does not report false positives for valid oneOf device matches- Valid mixed device types returns true with no errorsTest-Json reports errors only for the invalid device in oneOf schema- Invalid device reports errors only for that device, not for valid onesTest-Json does not report false positives for valid anyOf device matches- Valid mixed device types returns true with no errorsTest-Json reports errors only for the invalid device in anyOf schema- Invalid device reports errors only for that device, not for valid onesImplementation Details
Design Decisions
ReportValidationErrorshandles the recursive traversal withIsValidchecks, keepingHandleValidationErrorsunchanged for error reportingIsValidis true, eliminating false positives efficientlyHasDetailscheck before iterating child results for safety, which was not present in the original PRBackward Compatibility