You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
2
3
3
-
TODO: Write a short description of the package
4
+
### Node.js
5
+
6
+
```bash
7
+
npm install @hyperjump/better-json-schema-errors
8
+
```
9
+
10
+
## API Error Message Format
11
+
12
+
Our API Error Format includes :-
13
+
-**`schemaLocation`** - A 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.
14
+
15
+
-**`instanceLocation`** - JSON Pointer to the invalid piece of input data.
16
+
17
+
-**`message`** - Human-friendly explanation of the validation error.
"message": "Expected a string at least 5 characters long."
27
+
}
28
+
]
29
+
}
30
+
```
4
31
5
32
## Install
6
33
7
34
This module is designed for node.js (ES Modules, TypeScript) and browsers. It
8
35
should work in Bun and Deno as well, but the test runner doesn't work in these
9
36
environments, so this module may be less stable in those environments.
10
37
11
-
### Node.js
38
+
## Examples and Basic Usage
39
+
Better JSON Schema Errors works with **any JSON Schema validator** that follows the official [JSON Schema Output Format](https://json-schema.org/draft/2020-12/json-schema-core#name-output-structure).
40
+
In this example, we’ll showcase it with the [Hyperjump JSON Schema Validator](https://github.com/hyperjump-io/json-schema).
12
41
13
-
```bash
14
-
npm install @hyperjump/better-json-schema-errors
42
+
Now let's define a schema and some invalid data, then run the validation and process the output with `better-json-schema-errors`. :-
-**BASIC** — The "Basic" structure is a flat list of output units
86
+
-**DETAILED** — The "Detailed" structure is based on the schema and can be more readable for both
87
+
humans and machines.
88
+
-**VERBOSE** — The "Verbose" structure is a fully realised hierarchy that exactly matches that of the
89
+
schema.
90
+
91
+
No matter which output format your validator produces, Better JSON Schema Errors can process it.
92
+
93
+
### 2. Multiple Schema Locations
94
+
Sometimes a single validation issue is tied to **more than one schema keyword**.
95
+
For example, when both `minimum` and `exclusiveMinimum` apply, or when `minLength` and `maxLength` constraints overlap or when when both `minimum` and `maximum` apply.
96
+
97
+
Instead of multiple related error messages, It groups these into an **array of schema locations** and produces one concise, human-friendly message :-
98
+
99
+
```json
100
+
{
101
+
"schemaLocation": [
102
+
"https://example.com/main#/minimum",
103
+
"https://example.com/main#/maximum"
104
+
],
105
+
"instanceLocation": "#/age",
106
+
"message": "Expected a number greater than 5 and less than 10."
107
+
}
15
108
```
109
+
### 3. Localization
16
110
17
-
## Examples and Usage
111
+
The library uses [fluent](https://projectfluent.org)`.ftl` files to provide localized error messages. By default, only English is supported.
18
112
19
-
TODO: Write some examples
113
+
We need contributions from different countries to add more languages.
20
114
115
+
To change the language, pass a language option to the betterJsonSchemaErrors function, like this:
The `anyOf`/`oneOf` keyword is a powerful but complex JSON Schema feature. **better-json-schema-errors** intelligently simplifies its output by providing clear, consolidated error messages that are easier to debug. Whenever possible it will try to determine which alternative the user intended and focus the error output to only those errors related to correcting the data for that alternative.
Instead of 2 error message it manages to give a single concise error message. For details, see the dedicated [Range documenetation](./documentation/range-handler.md)
202
+
203
+
### 6. Custom Keywords and Error Handlers
204
+
In order to create the custom keywords and error handlers we need to create and
205
+
register two types of handlers: **Normalization Handler** and **Error Handlers**.
206
+
207
+
1. Normalization: This phase takes the raw, often deeply nested, error output
208
+
from the validator and converts it into a NormalizedOutput (you can check type of
209
+
normalizedOutput in the index.d.ts file).
210
+
211
+
2. Error Handling: This phase takes the normalized output and uses it to generate the final error messages. This is the job of the Error Handlers.
212
+
213
+
Fist step -: Creating the keywordHandler
214
+
```js
215
+
/**
216
+
* @import { KeywordHandler } from "@hyperjump/better-json-schema-errors"
0 commit comments