json-schema-library 11.4.0 → 11.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +6 -0
- package/README.md +29 -56
- package/dist/index.cjs +1 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +4 -1
- package/dist/index.d.mts +4 -1
- package/dist/index.mjs +1 -1
- package/dist/index.mjs.map +1 -1
- package/dist/jlib.js +2 -2
- package/package.json +11 -1
- package/src/SchemaNode.ts +5 -2
- package/src/compileSchema.test.ts +24 -0
- package/src/compileSchema.ts +15 -2
- package/src/draftEditor.ts +1 -0
- package/src/keywords/type.test.ts +13 -0
- package/src/settings.ts +2 -0
- package/src/utils/mergeSchema.test.ts +37 -0
- package/src/utils/mergeSchema.ts +24 -3
- package/src/validateSchema.test.ts +10 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
## Changelog
|
|
2
2
|
|
|
3
|
+
### 11.5.0
|
|
4
|
+
|
|
5
|
+
- added: option `remotes: JsonSchema[]` for `compileSchema` to simplify adding remotes
|
|
6
|
+
- fixed: annotation warnings should not be reported for title, description or default
|
|
7
|
+
- deprecated: DraftEditor Draft
|
|
8
|
+
|
|
3
9
|
### v11.4.0
|
|
4
10
|
|
|
5
11
|
- added schema annotation on `compileSchema` for unknown format
|
package/README.md
CHANGED
|
@@ -6,6 +6,14 @@
|
|
|
6
6
|
json-schema-library
|
|
7
7
|
</h1>
|
|
8
8
|
|
|
9
|
+
_json-schema-library_ is the [most compliant JSON Schema validator](https://bowtie.report/#/?language=typescript&language=javascript) for the web, fully supporting all major JSON Schema draft versions. Read [Draft Support](#draft-support) for more details.
|
|
10
|
+
|
|
11
|
+

|
|
12
|
+

|
|
13
|
+

|
|
14
|
+

|
|
15
|
+

|
|
16
|
+
|
|
9
17
|
> **json-schema-library** provides tools and utilities for working with JSON Schema - enabling creation, validation, and schema exploration. Unlike most validators and editors, which hide the inner workings, this library is designed for developers building custom tools around JSON Schema. It runs in both Node and browser environments, prioritizing flexibility and extensibility over minimal memory footprint or raw performance.
|
|
10
18
|
|
|
11
19
|
---
|
|
@@ -107,18 +115,11 @@ Details on `getDataDefaultOptions` are documented in [getData](#getData).
|
|
|
107
115
|
|
|
108
116
|
### validate input schema
|
|
109
117
|
|
|
110
|
-
All JSON Schema passed to `compileSchema` are validated automatically. To retrieve any schema errors you can access the property `schemaErrors` of the main node
|
|
118
|
+
All JSON Schema passed to `compileSchema` are validated automatically. To retrieve any schema errors you can access the property `schemaErrors` of the main node. `schemaAnnotations` contains all JSON Schema keywords that are not part of the used draft and any custom keyword that does not start with `x-`.
|
|
111
119
|
|
|
112
120
|
```ts
|
|
113
121
|
const root = compileSchema(mySchema);
|
|
114
|
-
const { schemaErrors } = root; // JsonError[]
|
|
115
|
-
```
|
|
116
|
-
|
|
117
|
-
Use the option `throwOnInvalidSchema:true` of `compileSchema` to throw an Error for a input schema containing errors:
|
|
118
|
-
|
|
119
|
-
```ts
|
|
120
|
-
const root = compileSchema({ properties: 123 }, { throwOnInvalidSchema: true });
|
|
121
|
-
// throws Error
|
|
122
|
+
const { schemaErrors, schemaAnnotations } = root; // JsonError[]
|
|
122
123
|
```
|
|
123
124
|
|
|
124
125
|
<details><summary>Example for schema validation errors</summary>
|
|
@@ -145,15 +146,6 @@ console.log(schemaErrors[0]);
|
|
|
145
146
|
|
|
146
147
|
</details>
|
|
147
148
|
|
|
148
|
-
To collect JSON Schema annotations for unused keywords you can opt in with option `withSchemaAnnotations`:
|
|
149
|
-
|
|
150
|
-
```ts
|
|
151
|
-
const root = compileSchema(mySchema, { withSchemaAnnotations: true });
|
|
152
|
-
const { schemaAnnotations } = root; // JsonAnnotation[]
|
|
153
|
-
```
|
|
154
|
-
|
|
155
|
-
This collects all JSON Schema keywords not part of the used draft and any custom keywords. Custom keywords starting with `x-` are allowed and thus will not create an annotation.
|
|
156
|
-
|
|
157
149
|
<details><summary>Example for validation annotations</summary>
|
|
158
150
|
|
|
159
151
|
---
|
|
@@ -180,6 +172,13 @@ console.log(schemaAnnotations[0]);
|
|
|
180
172
|
|
|
181
173
|
</details>
|
|
182
174
|
|
|
175
|
+
Use the option `throwOnInvalidSchema:true` of `compileSchema` to throw an Error for a input schema containing errors:
|
|
176
|
+
|
|
177
|
+
```ts
|
|
178
|
+
const root = compileSchema({ properties: 123 }, { throwOnInvalidSchema: true });
|
|
179
|
+
// throws Error
|
|
180
|
+
```
|
|
181
|
+
|
|
183
182
|
### SchemaNode
|
|
184
183
|
|
|
185
184
|
`compileSchema` builds a tree where each sub-schema becomes its own SchemaNode. Every node in the tree offers the same set of methods.
|
|
@@ -225,41 +224,30 @@ assert(root === childNode.parent);
|
|
|
225
224
|
|
|
226
225
|
</details>
|
|
227
226
|
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
---
|
|
231
|
-
|
|
232
|
-
A context is shared across all nodes of a schema
|
|
227
|
+
### Draft Support
|
|
233
228
|
|
|
234
|
-
|
|
235
|
-
const root = compileSchema(mySchema);
|
|
236
|
-
const { node: childNode } = root.getNode("#/image");
|
|
237
|
-
assert(root.context === childNode.context);
|
|
238
|
-
```
|
|
229
|
+
_json-schema-library_ fully supports all draft versions _draft-04, draft-06, draft-07, draft-2019-09 and draft-2020-12_.
|
|
239
230
|
|
|
240
|
-
|
|
231
|
+
<details><summary>References</summary>
|
|
241
232
|
|
|
242
|
-
|
|
243
|
-
const root = compileSchema(mySchema);
|
|
244
|
-
const { node: childNode } = root.getNode("#/image");
|
|
245
|
-
assert(root === childNode.context.rootNode);
|
|
246
|
-
```
|
|
233
|
+
---
|
|
247
234
|
|
|
248
|
-
|
|
235
|
+
- Draft support is defined by running a validator against the official [json-schema-test-suite](https://github.com/json-schema-org/JSON-Schema-Test-Suite).
|
|
236
|
+
- _json-schema-library_ passes all tests from [json-schema-test-suite](https://github.com/json-schema-org/JSON-Schema-Test-Suite) which can be inspected in [github actions](https://github.com/sagold/json-schema-library/actions/workflows/ci.yaml)
|
|
237
|
+
- To compare _json-schema-library_ to other validators refer to the official [Bowtie Report](https://bowtie.report/#/?language=typescript&language=javascript), which runs a subset of _json-schema-test-suite_ on all registered validators
|
|
249
238
|
|
|
250
239
|
---
|
|
251
240
|
|
|
252
241
|
</details>
|
|
253
242
|
|
|
254
|
-
>
|
|
255
|
-
> It is not advised to work on context directly, but it might be useful in some situations
|
|
243
|
+
> Please note that these reports refer to validation only. _json-schema-library_ offers tooling outside of validation and strives to be as spec-compliant as possible.
|
|
256
244
|
|
|
257
|
-
|
|
245
|
+
**format validators**
|
|
258
246
|
|
|
259
|
-
|
|
247
|
+
All JSON Schema format validators are supported:
|
|
260
248
|
|
|
261
|
-
-
|
|
262
|
-
- **
|
|
249
|
+
- **per default** the following formats available: `date`, `date-time`, `duration`, `email`, `json-pointer`, `relative-json-pointer`, `regex`, `time`, `url`, `uuid`
|
|
250
|
+
- **add remaining format** validators `hostname`, `idn-email`, `ipv4`, `ipv6`, `uri`, `uri-reference`, `uri-template` to drafts with:
|
|
263
251
|
|
|
264
252
|
```ts
|
|
265
253
|
import { addFormats } from "json-schema-library/formats";
|
|
@@ -270,21 +258,6 @@ addFormats([draft04, draft06, draft07, draft2019, draft2020]);
|
|
|
270
258
|
|
|
271
259
|
You can always override or extend format validation as is documented in [draft customization](#draft-customization).
|
|
272
260
|
|
|
273
|
-
<details><summary>Overview draft support</summary>
|
|
274
|
-
|
|
275
|
-
---
|
|
276
|
-
|
|
277
|
-
Draft support is defined by running a validator against the official [json-schema-test-suite](https://github.com/json-schema-org/JSON-Schema-Test-Suite).
|
|
278
|
-
|
|
279
|
-
- Test results for _json-schema-library_ can be inspected in [github actions](https://github.com/sagold/json-schema-library/actions/workflows/ci.yaml)
|
|
280
|
-
- A comparison to other validators is listed on [json-schema-benchmark](https://github.com/sagold/json-schema-benchmark)
|
|
281
|
-
|
|
282
|
-
Please note that these benchmarks refer to validation only. _json-schema-library_ offers tooling outside of validation and strives to be as spec-compliant as possible.
|
|
283
|
-
|
|
284
|
-
---
|
|
285
|
-
|
|
286
|
-
</details>
|
|
287
|
-
|
|
288
261
|
## SchemaNode methods
|
|
289
262
|
|
|
290
263
|
[addRemoteSchema](#addremoteschema) ·
|