-
-
Notifications
You must be signed in to change notification settings - Fork 9
Add Support for JSON Schema Draft-04 Keywords #130
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
Draft
srivastava-diya
wants to merge
7
commits into
hyperjump-io:main
Choose a base branch
from
srivastava-diya:main
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.
Draft
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8df7abf
feat(draft-04): add support for JSON Schema Draft-04 keywords
srivastava-diya c0a8983
draft-04 support
srivastava-diya a352e5b
refactor dependencies error-handler
srivastava-diya 1602ca0
add correct compatibility for dependencies test
srivastava-diya 2df468c
fix formatting
srivastava-diya 8826f9a
refactor draft-04 max and min error handler
srivastava-diya dadbb96
code cleanup
srivastava-diya 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 |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| import { getSchema } from "@hyperjump/json-schema/experimental"; | ||
| import { getErrors } from "../../json-schema-errors.js"; | ||
| import * as Schema from "@hyperjump/browser"; | ||
| import * as Instance from "@hyperjump/json-schema/instance/experimental"; | ||
|
|
||
| /** | ||
| * @import { ErrorHandler, ErrorObject, NormalizedOutput } from "../../index.d.ts" | ||
| */ | ||
|
|
||
| /** @type ErrorHandler */ | ||
| const dependenciesErrorHandler = async (normalizedErrors, instance, localization) => { | ||
| /** @type ErrorObject[] */ | ||
| const errors = []; | ||
|
|
||
| for (const schemaLocation in normalizedErrors["https://json-schema.org/keyword/draft-04/dependencies"]) { | ||
| if (typeof normalizedErrors["https://json-schema.org/keyword/draft-04/dependencies"][schemaLocation] === "boolean") { | ||
| continue; | ||
| } | ||
|
|
||
| const outputs = normalizedErrors["https://json-schema.org/keyword/draft-04/dependencies"][schemaLocation]; | ||
| for (const output of outputs) { | ||
| const result = await getErrors(output, instance, localization); | ||
| errors.push(...result); | ||
| } | ||
|
|
||
| const dependencies = await getSchema(schemaLocation); | ||
| for await (const [property, dependency] of Schema.entries(dependencies)) { | ||
| if (!Instance.has(property, instance)) { | ||
| continue; | ||
| } | ||
|
|
||
| if (Schema.typeOf(dependency) === "array") { | ||
| const dependentRequired = /** @type {string[]} */ (Schema.value(dependency)); | ||
| const missing = dependentRequired.filter((required) => !Instance.has(required, instance)); | ||
| errors.push({ | ||
| message: localization.getRequiredErrorMessage(missing), | ||
| instanceLocation: Instance.uri(instance), | ||
| schemaLocations: [schemaLocation] | ||
| }); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return errors; | ||
| }; | ||
|
|
||
| export default dependenciesErrorHandler; |
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 |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| import { getSchema } from "@hyperjump/json-schema/experimental"; | ||
| import * as Schema from "@hyperjump/browser"; | ||
| import * as Instance from "@hyperjump/json-schema/instance/experimental"; | ||
|
|
||
| /** | ||
| * @import { ErrorHandler, ErrorObject } from "../../index.d.ts" | ||
| */ | ||
|
|
||
| /** @type ErrorHandler */ | ||
| const maximumErrorHandler = async (normalizedErrors, instance, localization) => { | ||
| /** @type ErrorObject[] */ | ||
| const errors = []; | ||
|
|
||
| for (const schemaLocation in normalizedErrors["https://json-schema.org/keyword/draft-04/maximum"]) { | ||
| if (normalizedErrors["https://json-schema.org/keyword/draft-04/maximum"][schemaLocation]) { | ||
| continue; | ||
| } | ||
|
|
||
| const compiled = await getSchema(schemaLocation); | ||
| const maximum = /** @type number */ (Schema.value(compiled)); | ||
|
|
||
| const parentLocation = schemaLocation.replace(/\/[^/]+$/, ""); | ||
|
|
||
| let exclusive = false; | ||
| for (const exclusiveLocation in normalizedErrors["https://json-schema.org/keyword/draft-04/exclusiveMaximum"]) { | ||
| const exclusiveParentLocation = exclusiveLocation.replace(/\/[^/]+$/, ""); | ||
| if (exclusiveParentLocation === parentLocation) { | ||
| const exclusiveCompiled = await getSchema(exclusiveLocation); | ||
| exclusive = /** @type boolean */ (Schema.value(exclusiveCompiled)); | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| if (exclusive) { | ||
| errors.push({ | ||
| message: localization.getExclusiveMaximumErrorMessage(maximum), | ||
| instanceLocation: Instance.uri(instance), | ||
| schemaLocations: [schemaLocation] | ||
| }); | ||
| } else { | ||
| errors.push({ | ||
| message: localization.getMaximumErrorMessage(maximum), | ||
| instanceLocation: Instance.uri(instance), | ||
| schemaLocations: [schemaLocation] | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| return errors; | ||
| }; | ||
|
|
||
| export default maximumErrorHandler; | ||
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 |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| import { getSchema } from "@hyperjump/json-schema/experimental"; | ||
| import * as Schema from "@hyperjump/browser"; | ||
| import * as Instance from "@hyperjump/json-schema/instance/experimental"; | ||
|
|
||
| /** | ||
| * @import { ErrorHandler, ErrorObject } from "../../index.d.ts" | ||
| */ | ||
|
|
||
| /** @type ErrorHandler */ | ||
| const minimumErrorHandler = async (normalizedErrors, instance, localization) => { | ||
| /** @type ErrorObject[] */ | ||
| const errors = []; | ||
|
|
||
| for (const schemaLocation in normalizedErrors["https://json-schema.org/keyword/draft-04/minimum"]) { | ||
| if (normalizedErrors["https://json-schema.org/keyword/draft-04/minimum"][schemaLocation]) { | ||
| continue; | ||
| } | ||
|
|
||
| const compiled = await getSchema(schemaLocation); | ||
| const minimum = /** @type number */ (Schema.value(compiled)); | ||
|
|
||
| const parentLocation = schemaLocation.replace(/\/[^/]+$/, ""); | ||
|
|
||
| let exclusive = false; | ||
| for (const exclusiveLocation in normalizedErrors["https://json-schema.org/keyword/draft-04/exclusiveMinimum"]) { | ||
| const exclusiveParentLocation = exclusiveLocation.replace(/\/[^/]+$/, ""); | ||
| if (exclusiveParentLocation === parentLocation) { | ||
| const exclusiveCompiled = await getSchema(exclusiveLocation); | ||
| exclusive = /** @type boolean */ (Schema.value(exclusiveCompiled)); | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| if (exclusive) { | ||
| errors.push({ | ||
| message: localization.getExclusiveMinimumErrorMessage(minimum), | ||
| instanceLocation: Instance.uri(instance), | ||
| schemaLocations: [schemaLocation] | ||
| }); | ||
| } else { | ||
| errors.push({ | ||
| message: localization.getMinimumErrorMessage(minimum), | ||
| instanceLocation: Instance.uri(instance), | ||
| schemaLocations: [schemaLocation] | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| return errors; | ||
| }; | ||
|
|
||
| export default minimumErrorHandler; |
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
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
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 |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| import { evaluateSchema } from "../../json-schema-errors.js"; | ||
| import * as Instance from "@hyperjump/json-schema/instance/experimental"; | ||
| import * as Pact from "@hyperjump/pact"; | ||
|
|
||
| /** | ||
| * @import { NormalizationHandler, NormalizedOutput } from "../../index.d.ts" | ||
| */ | ||
|
|
||
| /** @type NormalizationHandler<[number, string]> */ | ||
| const additionalItemsNormalizationHandler = { | ||
| evaluate([numberOfItems, additionalItems], instance, context) { | ||
| /** @type NormalizedOutput[] */ | ||
| const outputs = []; | ||
|
|
||
| if (Instance.typeOf(instance) !== "array") { | ||
| return outputs; | ||
| } | ||
|
|
||
| for (const item of Pact.drop(numberOfItems, Instance.iter(instance))) { | ||
| outputs.push(evaluateSchema(additionalItems, item, context)); | ||
| } | ||
|
|
||
| return outputs; | ||
| }, | ||
| simpleApplicator: true | ||
| }; | ||
|
|
||
| export default additionalItemsNormalizationHandler; |
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 |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| import { evaluateSchema } from "../../json-schema-errors.js"; | ||
| import * as Instance from "@hyperjump/json-schema/instance/experimental"; | ||
|
|
||
| /** | ||
| * @import { NormalizationHandler, NormalizedOutput } from "../../index.d.ts" | ||
| */ | ||
|
|
||
| /** @type NormalizationHandler<Array<[string, string | string[]]>> */ | ||
| const dependenciesNormalizationHandler = { | ||
| evaluate(dependencies, instance, context) { | ||
| /** @type NormalizedOutput[] */ | ||
| const outputs = []; | ||
|
|
||
| if (Instance.typeOf(instance) !== "object") { | ||
| return outputs; | ||
| } | ||
|
|
||
| for (const [property, dependency] of dependencies) { | ||
| if (!Instance.has(property, instance)) { | ||
| continue; | ||
| } | ||
|
|
||
| if (typeof dependency === "string") { | ||
| outputs.push(evaluateSchema(dependency, instance, context)); | ||
| } | ||
| } | ||
|
|
||
| return outputs; | ||
| } | ||
| }; | ||
|
|
||
| export default dependenciesNormalizationHandler; |
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 |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| /** | ||
| * @import { NormalizationHandler } from "../../index.d.ts" | ||
| */ | ||
|
|
||
| /** @type NormalizationHandler */ | ||
| const exclusiveMaximumNormalizationHandler = { | ||
| evaluate() { | ||
| } | ||
| }; | ||
|
|
||
| export default exclusiveMaximumNormalizationHandler; |
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 |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| /** | ||
| * @import { NormalizationHandler } from "../../index.d.ts" | ||
| */ | ||
|
|
||
| /** @type NormalizationHandler */ | ||
| const exclusiveMinimumNormalizationHandler = { | ||
| evaluate() { | ||
| } | ||
| }; | ||
|
|
||
| export default exclusiveMinimumNormalizationHandler; |
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 |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| import { evaluateSchema } from "../../json-schema-errors.js"; | ||
| import * as Instance from "@hyperjump/json-schema/instance/experimental"; | ||
|
|
||
| /** | ||
| * @import { NormalizationHandler, NormalizedOutput } from "../../index.d.ts" | ||
| */ | ||
|
|
||
| /** @type NormalizationHandler<string | string[]> */ | ||
| const itemsNormalizationHandler = { | ||
| evaluate(items, instance, context) { | ||
| /** @type NormalizedOutput[] */ | ||
| const outputs = []; | ||
|
|
||
| if (Instance.typeOf(instance) !== "array") { | ||
| return outputs; | ||
| } | ||
|
|
||
| if (typeof items === "string") { | ||
| for (const itemNode of Instance.iter(instance)) { | ||
| outputs.push(evaluateSchema(items, itemNode, context)); | ||
| } | ||
| } else { | ||
| for (const [index, schemaLocation] of items.entries()) { | ||
| const itemNode = Instance.step(String(index), instance); | ||
| if (itemNode) { | ||
| outputs.push(evaluateSchema(schemaLocation, itemNode, context)); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return outputs; | ||
| }, | ||
| simpleApplicator: true | ||
| }; | ||
|
|
||
| export default itemsNormalizationHandler; |
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 |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| /** | ||
| * @import { NormalizationHandler } from "../../index.d.ts" | ||
| */ | ||
|
|
||
| /** @type NormalizationHandler */ | ||
| const maximumNormalizationHandler = { | ||
| evaluate() { | ||
| } | ||
| }; | ||
|
|
||
| export default maximumNormalizationHandler; |
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 |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| /** | ||
| * @import { NormalizationHandler } from "../../index.d.ts" | ||
| */ | ||
|
|
||
| /** @type NormalizationHandler */ | ||
| const minimumNormalizationHandler = { | ||
| evaluate() { | ||
| } | ||
| }; | ||
|
|
||
| export default minimumNormalizationHandler; |
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.
Uh oh!
There was an error while loading. Please reload this page.