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
-**`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.
13
+
-**`schemaLocation`** - A JSON 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
14
15
15
-**`instanceLocation`** - JSON Pointer to the invalid piece of input data.
16
16
@@ -38,37 +38,34 @@ environments, so this module may be less stable in those environments.
38
38
39
39
40
40
## Examples and Basic Usage
41
-
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).
41
+
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).
42
42
In this example, we’ll showcase it with the [Hyperjump JSON Schema Validator](https://github.com/hyperjump-io/json-schema).
43
43
44
44
Now let's define a schema and some invalid data, then run the validation and process the output with `better-json-schema-errors`. :-
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.
84
+
88
85
### 1. Works with All Output Formats
89
86
Supports all three standardized output formats:
90
87
-**BASIC** — The "Basic" structure is a flat list of output units
@@ -95,10 +92,7 @@ schema.
95
92
96
93
No matter which output format your validator produces, Better JSON Schema Errors can process it.
97
94
98
-
### 2. Resolves Output Ambiguities
99
-
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.
100
-
101
-
### 3. Multiple Schema Locations
95
+
### 2. Multiple Schema Locations
102
96
Sometimes a single validation issue is tied to **more than one schema keyword**.
103
97
For example, when both `minimum` and `exclusiveMinimum` apply, or when `minLength` and `maxLength` constraints overlap or when when both `minimum` and `maximum` apply.
104
98
@@ -114,138 +108,22 @@ Instead of generating duplicate error messages, It groups these into an **array
114
108
"message": "Expected a number greater than 5 and less than 10."
115
109
}
116
110
```
117
-
### 4. Localization
111
+
### 3. Localization
118
112
119
-
The library uses a [fluent/bundle](https://github.com/projectfluent/fluent.js/tree/main/fluent-bundle) to provide localized error messages. By default, only English is supported.
113
+
The library uses [fluent](https://projectfluent.org)`.ftl` files provide localized error messages. By default, only English is supported.
120
114
121
115
We need contributions from different countries to add more languages.
122
116
123
117
To change the language, pass a language option to the betterJsonSchemaErrors function, like this:
The `anyOf` keyword is a powerful but complex JSON Schema feature. When an instance fails to validate against all subschemas within an `anyOf`, standard validator output can be verbose and confusing, often returning an error for each failed subschema.
131
-
132
-
**Better JSON Schema Errors** intelligently simplifies this output, providing clear, consolidated error messages that are easier to debug.
133
-
134
-
---
135
-
136
-
#### 1. Mismatched Types
137
-
138
-
If the instance's type doesn't match any of the alternatives in an `anyOf`, the library provides a concise error message listing the expected types.
If the instance's type matches one of the `anyOf`alternatives but fails a subsequent constraint (like `minLength`), our library correctly identifies the specific rule that was violated.
123
+
### 4. Handling `anyOf`with Clarity
170
124
171
-
**Schema:**
172
-
```json
173
-
{
174
-
"anyOf": [
175
-
{ "type": "string", "minLength": 5 },
176
-
{ "type": "number" }
177
-
]
178
-
}
179
-
```
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. For detailed examples, see the dedicated [**anyOf** documentation](./docs/anyOf.md).
"message": "Expected a string at least 5 characters long."
193
-
}
194
-
]
195
-
}
196
-
197
-
```
198
-
199
-
#### 3. Multiple types match, pick based on field overlap
200
-
201
-
When an instance matches multiple `anyOf` alternatives type, the library prioritizes the one with the most relevant error based on the instance's fields.
"message": "The instance should be of type 'string' or 'number' but found 'boolean'."
34
+
}
35
+
]
36
+
}
37
+
38
+
```
39
+
#### 2. Partial Match with a Failed Constraint
40
+
41
+
If the instance's type matches one of the `anyOf` alternatives but fails a subsequent constraint (like `minLength`), our library correctly identifies the specific rule that was violated.
"message": "Expected a string at least 5 characters long."
65
+
}
66
+
]
67
+
}
68
+
69
+
```
70
+
71
+
#### 3. Multiple types match, pick based on field overlap
72
+
73
+
When an instance matches multiple `anyOf` alternatives type, the library prioritizes the one with the most relevant error based on the instance's fields.
0 commit comments