Skip to content

Commit aff5e52

Browse files
committed
updated readme file and added new files in documentation folder
1 parent cb725ad commit aff5e52

File tree

5 files changed

+66
-58
lines changed

5 files changed

+66
-58
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
node_modules/
22

3-
43
/docs
4+
55
scratch/
66
TODO*
77
.DS_Store

README.md

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ This module is designed for node.js (ES Modules, TypeScript) and browsers. It
3535
should work in Bun and Deno as well, but the test runner doesn't work in these
3636
environments, so this module may be less stable in those environments.
3737

38-
39-
4038
## Examples and Basic Usage
4139
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).
4240
In this example, we’ll showcase it with the [Hyperjump JSON Schema Validator](https://github.com/hyperjump-io/json-schema).
@@ -116,36 +114,36 @@ We need contributions from different countries to add more languages.
116114

117115
To change the language, pass a language option to the betterJsonSchemaErrors function, like this:
118116

119-
```
117+
```js
120118
const friendlyErrors = await betterJsonSchemaErrors(result, schemaUri, instance, { language: "en-US" });
121119
```
122120

123-
### 4. Handling `anyOf` with Clarity
121+
### 4. Handling `anyOf`/`oneOf` with Clarity
124122

125-
The `anyOf` 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.
123+
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.
126124

127125
**Schema:**
128126
```json
129127
{
130128
"anyOf": [
131-
{ "type": "string" },
129+
{ "type": "string", "minLength": 5 },
132130
{ "type": "number" }
133131
]
134132
}
135133
```
136134

137135
Invalid Instance:-
138-
``` Json
139-
false
136+
```json
137+
"abc"
140138
```
141139
BetterJSONSchemaErrors Output:-
142-
``` Json
140+
```json
143141
{
144142
"errors": [
145143
{
146-
"schemaLocation": "https://example.com/main#/anyOf",
144+
"schemaLocation": "https://example.com/main#/anyOf/0/minLength",
147145
"instanceLocation": "#",
148-
"message": "The instance should be of type 'string' or 'number' but found 'boolean'."
146+
"message": "Expected a string at least 5 characters long."
149147
}
150148
]
151149
}
@@ -174,7 +172,7 @@ Example:
174172
This makes typos or near-misses much easier to debug.
175173
For full details and strategies, see the dedicated [enum documentation](./documentation/enum.md).
176174

177-
### 6. Range constraint keyword
175+
### 6. Range constraint keywords
178176
Better JSON Schema Errors consolidates multiple range-related validation errors (`minimum`, `maxLength`, `minItems`, etc.) into a single, clear message.
179177
For example, a schema like:
180178
```json
@@ -185,11 +183,11 @@ For example, a schema like:
185183
}
186184
```
187185
Instance:-
188-
```Json
186+
```json
189187
2
190188
```
191189
BetterJSONSchemaErrors Output:-
192-
``` Json
190+
```json
193191
{
194192
"errors": [
195193
{
@@ -200,17 +198,20 @@ BetterJSONSchemaErrors Output:-
200198
]
201199
}
202200
```
203-
Instead of 2 error message it manage to give single concise error message. For details, see the dedicated [Range documenetation](./documentation/range-handler.md)
201+
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)
204202

205203
### 6. Custom Keywords and Error Handlers
206-
In order to create the custom keywords and error handlers we need create and register two types of handler: **Normalization Handler** 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**.
207206

208-
1. Normalization: This phase takes the raw, often deeply nested, error tree from the validator and converts it into a NormalizedOutput (can check type of normalizedOutput in the index.d.ts file).
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).
209210

210-
2. Error Handling: This phase takes the normalized data and uses it to generate the final error messages. This is the job of the Error Handlers.
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.
211212

212213
Fist step -: Creating the keywordHandler
213-
```Js
214+
```js
214215
/**
215216
* @import { KeywordHandler } from "@hyperjump/better-json-schema-errors"
216217
*/
@@ -227,7 +228,7 @@ export default multipleOfTen;
227228
```
228229

229230
Second step -: Creating the errorHandler
230-
```Js
231+
```js
231232
import { getSchema } from "@hyperjump/json-schema/experimental";
232233
import * as Schema from "@hyperjump/browser";
233234
import * as Instance from "@hyperjump/json-schema/instance/experimental";
@@ -261,11 +262,11 @@ const ErrorHandler = async (normalizedErrors, instance, localization) => {
261262

262263
Step 3:- Register the handlers:
263264

264-
```Js
265+
```js
265266
import { setNormalizationHandler, addErrorHandler } from "@hyperjump/better-json-schema-errors";
266-
const KEYWORD_URI = "https://example.com/keyword/multipleOfTen";
267+
const KEYWORD_URI = "https://json-schema.org/keyword/multipleOfTen";
267268

268-
setNormalizationHandler(CUSTOM_KEYWORD_URI, multipleOften);
269+
setNormalizationHandler(KEYWORD_URI, multipleOften);
269270

270271
addErrorHandler(errorHandler);
271272
```

documentation/anyOf.md

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
## Handling `anyOf` with Clarity
22

3-
**Better JSON Schema Errors** intelligently simplifies error output, providing clear, consolidated error messages that are easier to debug.
3+
`better-json-schema-errors` intelligently simplifies error output, providing clear, consolidated error messages that are easier to debug.
44
Here are the differnt cases and how better-json-schema-errors handles them to produces better errors.
55

66
---
@@ -20,11 +20,11 @@ If the instance's type doesn't match any of the alternatives in an `anyOf`, the
2020
```
2121

2222
Invalid Instance:-
23-
``` Json
23+
```json
2424
false
2525
```
2626
BetterJSONSchemaErrors Output:-
27-
``` Json
27+
```json
2828
{
2929
"errors": [
3030
{
@@ -51,11 +51,11 @@ If the instance's type matches one of the `anyOf` alternatives but fails a subse
5151
```
5252

5353
Invalid Instance:-
54-
``` Json
54+
```json
5555
"abc"
5656
```
5757
BetterJSONSchemaErrors Output:-
58-
``` Json
58+
```json
5959
{
6060
"errors": [
6161
{
@@ -98,15 +98,15 @@ When an instance matches multiple `anyOf` alternatives type, the library priorit
9898
```
9999

100100
Invalid Instance:-
101-
``` Json
101+
```json
102102
{
103103
"title": "Clean Code",
104104
"author": "Robert Martin",
105105
"ID": "NotValidId"
106106
}
107107
```
108108
BetterJSONSchemaErrors Output:-
109-
``` Json
109+
```json
110110
{
111111
"errors": [
112112
{
@@ -133,11 +133,11 @@ When an instance fails all enum or const options in an anyOf, the library merges
133133
```
134134

135135
Invalid Instance:-
136-
``` Json
136+
```json
137137
2
138138
```
139139
BetterJSONSchemaErrors Output:-
140-
``` Json
140+
```json
141141
{
142142
"errors": [
143143
{
@@ -159,21 +159,31 @@ When `anyOf` uses a discriminator, the library leverages it to give precise erro
159159
```json
160160
{
161161
"anyOf": [
162-
{ "properties": { "type": { "const": "a" }, "apple": { "type": "string" } } },
163-
{ "properties": { "type": { "const": "b" }, "banana": { "type": "string" } } }
162+
{
163+
"properties": {
164+
"type": { "const": "a" },
165+
"apple": { "type": "string" }
166+
}
167+
},
168+
{
169+
"properties": {
170+
"type": { "const": "b" },
171+
"banana": { "type": "string" }
172+
}
173+
}
164174
]
165175
}
166176
```
167177

168178
Invalid Instance:-
169-
``` Json
179+
```json
170180
{
171181
"type": "a",
172182
"apple": 42
173183
}
174184
```
175185
BetterJSONSchemaErrors Output:-
176-
``` Json
186+
```json
177187
{
178188
"errors": [
179189
{
@@ -183,5 +193,4 @@ BetterJSONSchemaErrors Output:-
183193
}
184194
]
185195
}
186-
187196
```

documentation/enum.md

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,16 @@ Example Schema:
1414
**Schema:**
1515
```json
1616
{
17-
"type": "string",
1817
"enum": ["apple", "banana", "orange"]
1918
}
2019
```
2120

2221
Invalid Instance:-
23-
``` Json
22+
```json
2423
{ "fruit": "appl" }
2524
```
2625
BetterJSONSchemaErrors Output:-
27-
``` Json
26+
```json
2827
{
2928
"errros": {
3029
"message": "Unexpected value 'appl'. Did you mean 'apple'?",
@@ -41,17 +40,16 @@ Example Schema:
4140
**Schema:**
4241
```json
4342
{
44-
"type": "string",
4543
"enum": ["apple", "banana", "orange"]
4644
}
4745
```
4846

4947
Invalid Instance:-
50-
``` Json
48+
```json
5149
{ "fruit": "grape" }
5250
```
5351
BetterJSONSchemaErrors Output:-
54-
``` Json
52+
```json
5553
{
5654
"errros": {
5755
"message": "Unexpected value 'grape'. Expected one of: 'apple', 'banana', or 'orange'.",

documentation/range-handler.md

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Range Constraint Keywords
22

3-
better-json-schema-errros processes validation errors for keywords that define a numeric range or count, such as `minimum`/`maximum`/`exclusiveMinimum`/`exculsiveMaximum` for numbers, `minLength`/`maxLength` for strings, `minItems`/`maxItems` for arrays, and `minProperties`/`maxProperties` for objects.
3+
`better-json-schema-errors` processes validation errors for keywords that define a numeric range or count, such as `minimum`/`maximum`/`exclusiveMinimum`/`exculsiveMaximum` for numbers, `minLength`/`maxLength` for strings, `minItems`/`maxItems` for arrays, and `minProperties`/`maxProperties` for objects.
44
The primary goal is to consolidate multiple, separate validation errors related to these constraints into a single, clear, and human-readable message that describes the effective valid range for the instance.
55

66
---
@@ -26,7 +26,7 @@ This handler improves the user experience by implementing a unified consolidatio
2626
### Examples
2727
### 1. **Number** (`minimum`, `maximum`, `exclusiveMinimum`, `exclusiveMaximum`)
2828
Schema:
29-
```Json
29+
```json
3030
{
3131
"allOf": [
3232
{ "minimum": 3 },
@@ -35,11 +35,11 @@ Schema:
3535
}
3636
```
3737
Instance:-
38-
```Json
38+
```json
3939
2
4040
```
4141
BetterJSONSchemaErrors Output:-
42-
``` Json
42+
```json
4343
{
4444
"errors": [
4545
{
@@ -54,7 +54,7 @@ BetterJSONSchemaErrors Output:-
5454

5555
### 2. **String** (`minLength`, `maxLength`)
5656
Schema:
57-
```Json
57+
```json
5858
{
5959
"allOf": [
6060
{ "minLength": 3 },
@@ -63,11 +63,11 @@ Schema:
6363
}
6464
```
6565
Instance:-
66-
```Json
66+
```json
6767
"helo"
6868
```
69-
BetterJSONSchemaErrors Output:-
70-
``` Json
69+
`better-json-schema-errors` Output:-
70+
```json
7171
{
7272
"errors": [
7373
{
@@ -82,7 +82,7 @@ BetterJSONSchemaErrors Output:-
8282

8383
### 3. **Array** (`minItems`, `maxItems`)
8484
Schema:
85-
```Json
85+
```json
8686
{
8787
"allOf": [
8888
{ "minItems": 3 },
@@ -91,11 +91,11 @@ Schema:
9191
}
9292
```
9393
Instance:-
94-
```Json
94+
```json
9595
[1,2]
9696
```
9797
BetterJSONSchemaErrors Output:-
98-
``` Json
98+
```json
9999
{
100100
"errors": [
101101
{
@@ -112,7 +112,7 @@ BetterJSONSchemaErrors Output:-
112112
```
113113
### 4. **Object** (`minProperties`, `maxProperties`)
114114
Schema:
115-
```Json
115+
```json
116116
{
117117
"allOf": [
118118
{ "minProperties": 2 },
@@ -121,11 +121,11 @@ Schema:
121121
}
122122
```
123123
Instance:-
124-
```Json
124+
```json
125125
{"a": 1}
126126
```
127127
BetterJSONSchemaErrors Output:-
128-
``` Json
128+
```json
129129
{
130130
"errors": [
131131
{

0 commit comments

Comments
 (0)