-
-
Notifications
You must be signed in to change notification settings - Fork 4
Readme #53
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
Merged
Merged
Readme #53
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c734b7a
update readme file
arpitkuriyal 2792ad9
correct minor mistake on the readme file
arpitkuriyal f0154de
update the readme file and added docs folder
arpitkuriyal 721b561
minor changes
arpitkuriyal cb725ad
updated readme file
arpitkuriyal aff5e52
updated readme file and added new files in documentation folder
arpitkuriyal 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,23 +1,251 @@ | ||||||
| # Hyperjump - Better JSON Schema Errors | ||||||
| # Hyperjump - Better JSON Schema Errors | ||||||
| It transforms standard, machine-oriented validation output into clear, human-friendly error messages ideal for API responses and developer tools. Built upon the official JSON Schema Output Format introduced in draft 2019-09, it ensures seamless compatibility with any compliant validator. | ||||||
|
|
||||||
| TODO: Write a short description of the package | ||||||
| ### Node.js | ||||||
|
|
||||||
| ```bash | ||||||
| npm install @hyperjump/better-json-schema-errors | ||||||
| ``` | ||||||
|
|
||||||
| ## API Error Message Format | ||||||
|
|
||||||
| Our API Error Format includes :- | ||||||
| - **`schemaLocation`** - A JSON Pointer or URI that points to the specific keyword(s) within the schema that failed validation. This can be a single string or an array of absolute keyword locations for errors that are grouped together. | ||||||
|
|
||||||
| - **`instanceLocation`** - JSON Pointer to the invalid piece of input data. | ||||||
|
|
||||||
| - **`message`** - Human-friendly explanation of the validation error. | ||||||
|
|
||||||
| Example:- | ||||||
| ```json | ||||||
| { | ||||||
| "errors": [ | ||||||
| { | ||||||
| "schemaLocation": "https://example.com/main#/properties/name/minLength", | ||||||
| "instanceLocation": "#/name", | ||||||
| "message": "Expected a string at least 5 characters long." | ||||||
| } | ||||||
| ] | ||||||
| } | ||||||
| ``` | ||||||
|
|
||||||
| ## Install | ||||||
|
|
||||||
| This module is designed for node.js (ES Modules, TypeScript) and browsers. It | ||||||
| should work in Bun and Deno as well, but the test runner doesn't work in these | ||||||
| environments, so this module may be less stable in those environments. | ||||||
|
|
||||||
| ### Node.js | ||||||
|
|
||||||
| ```bash | ||||||
| npm install @hyperjump/better-json-schema-errors | ||||||
|
|
||||||
| ## Examples and Basic Usage | ||||||
| Better JSON Schema Errors works with **any JSON Schema validator** that follows the official [JSON Schema Output Format](https://json-schema.org/draft/2019-09). | ||||||
arpitkuriyal marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
| In this example, we’ll showcase it with the [Hyperjump JSON Schema Validator](https://github.com/hyperjump-io/json-schema). | ||||||
|
|
||||||
| Now let's define a schema and some invalid data, then run the validation and process the output with `better-json-schema-errors`. :- | ||||||
| ```js | ||||||
| import { registerSchema, validate, unregisterSchema } from "@hyperjump/json-schema/draft-2020-12"; | ||||||
| import { betterJsonSchemaErrors } from "@hyperjump/better-json-schema-errors"; | ||||||
|
|
||||||
| async function runExample() { | ||||||
arpitkuriyal marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
| const schemaUri = "https://example.com/main"; | ||||||
| registerSchema({ | ||||||
| $schema: "https://json-schema.org/draft/2020-12/schema", | ||||||
| anyOf: [ | ||||||
| { enum: ["a", "b", "c"] } | ||||||
| ] | ||||||
| }, schemaUri); | ||||||
|
|
||||||
| const instance = 4; | ||||||
| const result = await validate(schemaUri, instance, "BASIC"); | ||||||
|
|
||||||
| if (result.valid) { | ||||||
| console.log("Valid instance!"); | ||||||
| } else { | ||||||
| const friendlyErrors = await betterJsonSchemaErrors(result, schemaUri, instance); | ||||||
| console.log(JSON.stringify(friendlyErrors, null, 2)); | ||||||
| } | ||||||
|
|
||||||
| unregisterSchema(schemaUri); | ||||||
| } | ||||||
|
|
||||||
| await runExample(); | ||||||
| ``` | ||||||
| Output:- | ||||||
| ```json | ||||||
| { | ||||||
| "errors": [ | ||||||
| { | ||||||
| "message": "Unexpected value 4. Expected one of: 'a', 'b', or 'c'.", | ||||||
| "instanceLocation": "#", | ||||||
| "schemaLocation": "https://example.com/main#/enum" | ||||||
| } | ||||||
| ] | ||||||
| } | ||||||
| ``` | ||||||
|
|
||||||
| ## Features and Advanced Usage | ||||||
| Better JSON Schema Errors goes beyond simply rephrasing validation messages. It provides a **robust error normalization layer** on top of the official [JSON Schema Output Format](https://json-schema.org/draft/2019-09), ensuring consistent, human-friendly results across different validators and error formats. | ||||||
arpitkuriyal marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
| ### 1. Works with All Output Formats | ||||||
| Supports all three standardized output formats: | ||||||
| - **BASIC** — The "Basic" structure is a flat list of output units | ||||||
| - **DETAILED** — The "Detailed" structure is based on the schema and can be more readable for both | ||||||
| humans and machines. | ||||||
| - **VERBOSE** — The "Verbose" structure is a fully realised hierarchy that exactly matches that of the | ||||||
| schema. | ||||||
|
|
||||||
| No matter which output format your validator produces, Better JSON Schema Errors can process it. | ||||||
|
|
||||||
| ### 2. Resolves Output Ambiguities | ||||||
| Some validators may provide inconsistent or ambiguous data, such as using `keywordLocation` instead of the required `absoluteKeywordLocation`. Our library resolves these issues by normalizing both the `keywordLocation` and `instanceLocation`. It adds the leading `#` to `instanceLocation` and standardizes the path, so everything is a valid JSON Pointer and can be used predictably. This helps resolve ambiguities and provides a unified, reliable format for all error messages. | ||||||
arpitkuriyal marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
|
|
||||||
| ### 3. Multiple Schema Locations | ||||||
| Sometimes a single validation issue is tied to **more than one schema keyword**. | ||||||
| For example, when both `minimum` and `exclusiveMinimum` apply, or when `minLength` and `maxLength` constraints overlap or when when both `minimum` and `maximum` apply. | ||||||
|
|
||||||
| Instead of generating duplicate error messages, It groups these into an **array of schema locations** and produces one concise, human-friendly message :- | ||||||
|
||||||
| Instead of generating duplicate error messages, It groups these into an **array of schema locations** and produces one concise, human-friendly message :- | |
| Instead of multiple related error messages, It groups these into an **array of schema locations** and produces one concise, human-friendly message :- |
arpitkuriyal marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
arpitkuriyal marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
arpitkuriyal marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
arpitkuriyal marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
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.