-
-
Notifications
You must be signed in to change notification settings - Fork 14
filter anyOf and oneOf alternative #180
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
Open
animeshsahoo1
wants to merge
8
commits into
hyperjump-io:main
Choose a base branch
from
animeshsahoo1:filter-anyOf-oneOf-alternatives
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+529
−28
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
bfb6925
filter anyOf and oneOf alternative
animeshsahoo1 80697b2
added tests for new behavior
animeshsahoo1 72d179f
used hyperjump pact for better performance
animeshsahoo1 803ece5
fixed linting issues with oxlint
animeshsahoo1 67837c8
changed the logic to match what is discussed
animeshsahoo1 e2d845c
simplified code
animeshsahoo1 18a152a
Cleanup pipeline usage
jdesrosiers d59e9de
Move discriminator filtering into the main loop
jdesrosiers File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,10 @@ | ||
| import * as Instance from "@hyperjump/json-schema/instance/experimental"; | ||
| import * as JsonPointer from "@hyperjump/json-pointer"; | ||
| import * as Pact from "@hyperjump/pact"; | ||
| import { getErrors } from "../json-schema-errors.js"; | ||
|
|
||
| /** | ||
| * @import { ErrorHandler, ErrorObject } from "../index.d.ts" | ||
| * @import { ErrorHandler, ErrorObject, NormalizedOutput } from "../index.d.ts" | ||
| */ | ||
|
|
||
| /** @type ErrorHandler */ | ||
|
|
@@ -16,20 +18,65 @@ const anyOfErrorHandler = async (normalizedErrors, instance, localization) => { | |
| continue; | ||
| } | ||
|
|
||
| const alternatives = []; | ||
| const instanceLocation = Instance.uri(instance); | ||
| let filtered = anyOf; | ||
|
|
||
| const instanceProps = Pact.pipe( | ||
| Instance.keys(instance), | ||
| Pact.map((keyNode) => /** @type {string} */ (Instance.value(keyNode))), | ||
| Pact.collectSet | ||
| ); | ||
|
Comment on lines
+24
to
+28
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I occurs to me that we're collecting property names here, but then we always convert to a property location wherever we use this variable. I bet if we just use the property location to begin with, we can simplify things considerably. |
||
|
|
||
| const discriminators = Pact.pipe( | ||
| instanceProps, | ||
| Pact.filter((prop) => { | ||
| const propLocation = JsonPointer.append(prop, instanceLocation); | ||
| return Pact.some((alternative) => propertyPasses(alternative[propLocation]), anyOf); | ||
| }), | ||
| Pact.collectSet | ||
| ); | ||
|
|
||
| const prefix = `${instanceLocation}/`; | ||
|
|
||
| filtered = []; | ||
| for (const alternative of anyOf) { | ||
| const typeErrors = alternative[instanceLocation]["https://json-schema.org/keyword/type"]; | ||
| const match = !typeErrors || Object.values(typeErrors).every((isValid) => isValid); | ||
| // Filter alternatives whose declared type doesn't match the instance type | ||
| const typeResults = alternative[instanceLocation]["https://json-schema.org/keyword/type"]; | ||
| if (typeResults && !Object.values(typeResults).every((isValid) => isValid)) { | ||
| continue; | ||
| } | ||
|
|
||
| if (match) { | ||
| alternatives.push(await getErrors(alternative, instance, localization)); | ||
| if (Instance.typeOf(instance) === "object") { | ||
| const declaredProps = Pact.pipe( | ||
| Object.keys(alternative), | ||
| Pact.filter((loc) => loc.startsWith(prefix)), | ||
| Pact.map((loc) => /** @type {string} */ (Pact.head(JsonPointer.pointerSegments(loc.slice(prefix.length - 1))))), | ||
| Pact.collectSet | ||
| ); | ||
|
|
||
| // Filter alternative if it has no declared properties in common with the instance | ||
| if (!Pact.some((prop) => declaredProps.has(prop), instanceProps)) { | ||
| continue; | ||
| } | ||
|
|
||
| // Filter alternative if it has failing properties that are decalred and passing in another alternative | ||
| if (Pact.some((prop) => !propertyPasses(alternative[JsonPointer.append(prop, instanceLocation)]), discriminators)) { | ||
| continue; | ||
| } | ||
| } | ||
|
|
||
| filtered.push(alternative); | ||
| } | ||
|
|
||
| if (filtered.length === 0) { | ||
| filtered = anyOf; | ||
| } | ||
|
|
||
| /** @type ErrorObject[][] */ | ||
| const alternatives = []; | ||
|
|
||
| if (alternatives.length === 0) { | ||
| for (const alternative of anyOf) { | ||
| for (const alternative of filtered) { | ||
| alternatives.push(await getErrors(alternative, instance, localization)); | ||
| } | ||
| } | ||
|
|
@@ -39,8 +86,8 @@ const anyOfErrorHandler = async (normalizedErrors, instance, localization) => { | |
| } else { | ||
| errors.push({ | ||
| message: localization.getAnyOfErrorMessage(), | ||
| alternatives: alternatives, | ||
| instanceLocation: Instance.uri(instance), | ||
| alternatives, | ||
| instanceLocation, | ||
| schemaLocations: [schemaLocation] | ||
| }); | ||
| } | ||
|
|
@@ -49,4 +96,12 @@ const anyOfErrorHandler = async (normalizedErrors, instance, localization) => { | |
| return errors; | ||
| }; | ||
|
|
||
| /** @type (propOutput: NormalizedOutput[string] | undefined) => boolean */ | ||
| const propertyPasses = (propOutput) => { | ||
| if (!propOutput) { | ||
| return false; | ||
| } | ||
| return Object.values(propOutput).every((keywordResults) => Object.values(keywordResults).every((v) => v === true)); | ||
| }; | ||
|
|
||
| export default anyOfErrorHandler; | ||
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
Oops, something went wrong.
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.
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.
This is initialized to
anyOf, but then reset to[]before it's used. That can be cleaned up.