json-schema-library 11.3.1 → 11.4.1

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.
Files changed (57) hide show
  1. package/CHANGELOG.md +21 -2
  2. package/README.md +66 -84
  3. package/dist/chunk-350yNsax.cjs +1 -0
  4. package/dist/formats.cjs +2 -0
  5. package/dist/formats.cjs.map +1 -0
  6. package/dist/formats.d.cts +8 -0
  7. package/dist/formats.d.mts +8 -0
  8. package/dist/formats.mjs +2 -0
  9. package/dist/formats.mjs.map +1 -0
  10. package/dist/index.cjs +2 -1
  11. package/dist/index.cjs.map +1 -0
  12. package/dist/index.d.cts +4 -4
  13. package/dist/index.d.mts +4 -4
  14. package/dist/index.mjs +2 -1
  15. package/dist/index.mjs.map +1 -0
  16. package/dist/jlib.js +2 -2
  17. package/dist/jlibFormats.iife.js +1 -0
  18. package/dist/jlibRemote.iife.js +1 -0
  19. package/dist/{remotes/index.cjs → remotes.cjs} +2 -1
  20. package/dist/remotes.cjs.map +1 -0
  21. package/dist/remotes.d.cts +8 -0
  22. package/dist/remotes.d.mts +8 -0
  23. package/dist/{remotes/index.mjs → remotes.mjs} +2 -1
  24. package/dist/remotes.mjs.map +1 -0
  25. package/dist/{types-DVyFDxCv.d.mts → types-BDjKcTVR.d.cts} +4 -2
  26. package/dist/{types-ZgoQMSny.d.cts → types-CqkCJmt8.d.mts} +4 -2
  27. package/package.json +31 -15
  28. package/src/SchemaNode.ts +12 -18
  29. package/src/compileSchema.ts +0 -3
  30. package/src/errors/errors.ts +3 -2
  31. package/src/formats/additionalFormats.ts +118 -0
  32. package/src/formats/formats.ts +3 -111
  33. package/src/keywords/$defs.ts +3 -0
  34. package/src/keywords/additionalProperties.ts +1 -0
  35. package/src/keywords/allOf.ts +2 -5
  36. package/src/keywords/anyOf.ts +2 -4
  37. package/src/keywords/dependencies.ts +2 -0
  38. package/src/keywords/dependentSchemas.ts +2 -3
  39. package/src/keywords/format.ts +8 -0
  40. package/src/keywords/ifthenelse.ts +4 -9
  41. package/src/keywords/oneOf.ts +2 -5
  42. package/src/keywords/patternProperties.ts +5 -1
  43. package/src/keywords/prefixItems.ts +14 -9
  44. package/src/keywords/properties.ts +2 -3
  45. package/src/keywords/propertyDependencies.ts +2 -3
  46. package/src/keywords/propertyNames.ts +1 -1
  47. package/src/keywords/type.test.ts +13 -0
  48. package/src/utils/collectValidationErrors.ts +9 -0
  49. package/src/utils/mergeSchema.test.ts +37 -0
  50. package/src/utils/mergeSchema.ts +24 -3
  51. package/src/validateSchema.test.ts +29 -30
  52. package/tsconfig.json +1 -0
  53. package/tsconfig.test.json +1 -0
  54. package/tsdown.config.ts +5 -2
  55. package/tsdown.iife.config.ts +29 -8
  56. package/dist/remotes/index.d.cts +0 -7
  57. package/dist/remotes/index.d.mts +0 -7
package/CHANGELOG.md CHANGED
@@ -1,9 +1,28 @@
1
1
  ## Changelog
2
2
 
3
+ ### v11.4.0
4
+
5
+ - added schema annotation on `compileSchema` for unknown format
6
+ - replaced option `withSchemaAnnotations` in favor of always creating all annotations
7
+
8
+ **Breaking change**: Moved some large format-validators to separate entry point:
9
+
10
+ - the following format-validators have been moved to a separate entry point "json-schema-library/formats": `hostname`, `idn-email`, `ipv4`, `ipv6`, `uri`, `uri-reference`, `uri-template`
11
+ - the following additional format-validators are available through "json-schema-library/formats": `iri`, `iri-reference`, `idn-hostname`
12
+
13
+ _Use the following to add the additional format validators to drafts per default:_
14
+
15
+ ```ts
16
+ import { addFormats } from "json-schema-library/formats";
17
+ import { draft04, draft06, draft07, draft2019, draft2020 } from "json-schema-library";
18
+ // add additional formats to the following drafts
19
+ addFormats([draft04, draft06, draft07, draft2019, draft2020]);
20
+ ```
21
+
3
22
  ### v11.3.0
4
23
 
5
- - added option 'draft' as fallback for a missing `$schema` id
6
- - added properties to merge when resolvinf a $ref to `settings.PROPERTIES_TO_MERGE`
24
+ - added option `draft` as fallback for a missing `$schema` id
25
+ - added setting for properties to merge when resolving a `$ref` to `settings.PROPERTIES_TO_MERGE`
7
26
 
8
27
  ### v11.2.0
9
28
 
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
+ ![draft-04](https://img.shields.io/endpoint?url=https://gist.githubusercontent.com/sagold/60138b5d728d1f8f92ba29546dee7c00/raw/jsl-draft04.json)
12
+ ![draft-06](https://img.shields.io/endpoint?url=https://gist.githubusercontent.com/sagold/60138b5d728d1f8f92ba29546dee7c00/raw/jsl-draft06.json)
13
+ ![draft-07](https://img.shields.io/endpoint?url=https://gist.githubusercontent.com/sagold/60138b5d728d1f8f92ba29546dee7c00/raw/jsl-draft07.json)
14
+ ![draft-2019-09](https://img.shields.io/endpoint?url=https://gist.githubusercontent.com/sagold/60138b5d728d1f8f92ba29546dee7c00/raw/jsl-draft2019-09.json)
15
+ ![draft-2020-12](https://img.shields.io/endpoint?url=https://gist.githubusercontent.com/sagold/60138b5d728d1f8f92ba29546dee7c00/raw/jsl-draft2020-12.json)
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
  ---
@@ -46,6 +54,11 @@ console.log(schemaNode.getDraftVersion()); // draft-07
46
54
 
47
55
  ## Overview
48
56
 
57
+ [compileSchema](#compileschema) ·
58
+ [validate input schema](#validate-input-schema) ·
59
+ [SchemaNode](#schemanode) ·
60
+ [Draft Support](#draft-support)
61
+
49
62
  ### compileSchema
50
63
 
51
64
  Use `compileSchema` once to turn a JSON Schema into a tree of SchemaNodes. After that, you'll work with individual nodes in the tree. You can also pass an options object to `compileSchema` to customize how the nodes are created.
@@ -54,25 +67,25 @@ Use `compileSchema` once to turn a JSON Schema into a tree of SchemaNodes. After
54
67
  type CompileOptions = {
55
68
  // set of drafts to use
56
69
  drafts: Draft[];
70
+ /** fallback draft version in case no draft_is specified by `schema.$schema` */
71
+ draft?: string;
57
72
  // a context to share
58
73
  remote: SchemaNode;
59
74
  // if format-validations should create errors. Defaults to true
60
75
  formatAssertion: boolean | "meta-schema";
61
76
  /** set to true to throw an Error on errors in input schema. Defaults to false */
62
77
  throwOnInvalidSchema?: boolean;
63
- /** set to true to collect unknown keywords of input schema in `node.schemaAnnotations`. Defaults to false */
64
- withSchemaAnnotations?: boolean;
65
78
  /** set to true to throw an Error when encountering an unresolvable ref */
66
79
  throwOnInvalidRef?: boolean;
67
80
  // default options for all calls to node.getData()
68
81
  getDataDefaultOptions?: {
69
- // Add all properties (required and optional) to the generated data
82
+ // add all properties (required and optional) to the generated data
70
83
  addOptionalProps?: boolean;
71
- // Remove data that does not match input schema. Defaults to false
84
+ // remove data that does not match input schema. Defaults to false
72
85
  removeInvalidData?: boolean;
73
- // Set to false to take default values as they are and not extend them. Defaults to true
86
+ // set to false to take default values as they are and not extend them. Defaults to true
74
87
  extendDefaults?: boolean;
75
- // Limits how often a $ref should be followed before aborting. Prevents infinite data-structure. Defaults to 1
88
+ // limits how often a $ref should be followed before aborting. Prevents infinite data-structure. Defaults to 1
76
89
  recursionLimit?: number;
77
90
  };
78
91
  };
@@ -102,18 +115,11 @@ Details on `getDataDefaultOptions` are documented in [getData](#getData).
102
115
 
103
116
  ### validate input schema
104
117
 
105
- 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-`.
106
119
 
107
120
  ```ts
108
121
  const root = compileSchema(mySchema);
109
- const { schemaErrors } = root; // JsonError[]
110
- ```
111
-
112
- Use the option `throwOnInvalidSchema:true` of `compileSchema` to throw an Error for a input schema containing errors:
113
-
114
- ```ts
115
- const root = compileSchema({ properties: 123 }, { throwOnInvalidSchema: true });
116
- // throws Error
122
+ const { schemaErrors, schemaAnnotations } = root; // JsonError[]
117
123
  ```
118
124
 
119
125
  <details><summary>Example for schema validation errors</summary>
@@ -140,15 +146,6 @@ console.log(schemaErrors[0]);
140
146
 
141
147
  </details>
142
148
 
143
- To collect JSON Schema annotations for unused keywords you can opt in with option `withSchemaAnnotations`:
144
-
145
- ```ts
146
- const root = compileSchema(mySchema, { withSchemaAnnotations: true });
147
- const { schemaAnnotations } = root; // JsonAnnotation[]
148
- ```
149
-
150
- 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.
151
-
152
149
  <details><summary>Example for validation annotations</summary>
153
150
 
154
151
  ---
@@ -175,6 +172,13 @@ console.log(schemaAnnotations[0]);
175
172
 
176
173
  </details>
177
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
+
178
182
  ### SchemaNode
179
183
 
180
184
  `compileSchema` builds a tree where each sub-schema becomes its own SchemaNode. Every node in the tree offers the same set of methods.
@@ -220,64 +224,39 @@ assert(root === childNode.parent);
220
224
 
221
225
  </details>
222
226
 
223
- <details><summary>All nodes share a context</summary>
224
-
225
- ---
226
-
227
- A context is shared across all nodes of a schema
228
-
229
- ```ts
230
- const root = compileSchema(mySchema);
231
- const { node: childNode } = root.getNode("#/image");
232
- assert(root.context === childNode.context);
233
- ```
234
-
235
- And some context properties are shared across all schema added as remotes. The property `rootNode` refers to the root-schema for the current node
236
-
237
- ```ts
238
- const root = compileSchema(mySchema);
239
- const { node: childNode } = root.getNode("#/image");
240
- assert(root === childNode.context.rootNode);
241
- ```
242
-
243
- Note that rootNodes will change when working across remote schema (using $ref).
244
-
245
- ---
246
-
247
- </details>
248
-
249
- > [!CAUTION]
250
- > It is not advised to work on context directly, but it might be useful in some situations
251
-
252
227
  ### Draft Support
253
228
 
254
- _json-schema-library_ fully supports all core features of draft versions draft-04, draft-06, draft-07, draft-2019-09 and draft-2020-12. Additionally, most format-validations are supported per default besides the listed format below. You can always override or extend format validation as is documented in [draft customization](#draft-customization).
229
+ _json-schema-library_ fully supports all draft versions _draft-04, draft-06, draft-07, draft-2019-09 and draft-2020-12_.
255
230
 
256
- <details><summary>Overview draft support</summary>
231
+ <details><summary>References</summary>
257
232
 
258
233
  ---
259
234
 
260
- 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).
261
-
262
- - Test results for _json-schema-library_ can be inspected in [github actions](https://github.com/sagold/json-schema-library/actions/workflows/ci.yaml)
263
- - A comparison to other validators is listed on [json-schema-benchmark](https://github.com/sagold/json-schema-benchmark)
264
-
265
- 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.
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
266
238
 
267
239
  ---
268
240
 
269
241
  </details>
270
242
 
271
- <details><summary>Overview format validation support</summary>
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.
272
244
 
273
- ---
245
+ **format validators**
274
246
 
275
- - **`❌ unsupported formats`** iri, iri-reference, idn-hostname
276
- - **`✅ supported formats`**: date, date-time, date, duration, ecmascript-regex, email, hostname, idn-email, ipv4, ipv6, json-pointer, regex, relative-json-pointer, time, unknown, uri-reference, uri-template, uri, uuid
247
+ All JSON Schema format validators are supported:
277
248
 
278
- ---
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:
279
251
 
280
- </details>
252
+ ```ts
253
+ import { addFormats } from "json-schema-library/formats";
254
+ import { draft04, draft06, draft07, draft2019, draft2020 } from "json-schema-library";
255
+ // add additional formats to the following drafts
256
+ addFormats([draft04, draft06, draft07, draft2019, draft2020]);
257
+ ```
258
+
259
+ You can always override or extend format validation as is documented in [draft customization](#draft-customization).
281
260
 
282
261
  ## SchemaNode methods
283
262
 
@@ -435,6 +414,22 @@ schemaNode.getNodeRef("https://sagold.com/remote#/properties/character");
435
414
  const someNode = node.compileSchema({ prefixItems: [{ type: "string" }, { $ref: "#/$defs/string" }] });
436
415
  ```
437
416
 
417
+ #### custom error messages
418
+
419
+ You can set custom errors messages locally by using the errors-keyword:
420
+
421
+ ```ts
422
+ const { errors } = compileSchema({
423
+ type: "array",
424
+ minItems: 2,
425
+ errorMessages: {
426
+ "min-items-error": "Custom error {{minItems}}"
427
+ }
428
+ }).validate([1]);
429
+
430
+ assert.deepEqual(errors[0].message, "Custom error 2");
431
+ ```
432
+
438
433
  ### createSchema
439
434
 
440
435
  `createSchema` returns a simple JSON Schema for the input data.
@@ -892,7 +887,8 @@ if (node) {
892
887
  }
893
888
  ```
894
889
 
895
- ---
890
+ > [!CAUTION]
891
+ > `getNode` returns the root of the current schema. If a remote schema was resolved, the returned node will be the remote-schema root - not the initial schema-root you passed in to compileSchema
896
892
 
897
893
  ### reduceNode
898
894
 
@@ -1150,7 +1146,7 @@ console.log(errors); /// [{ code: "type-error", value: "data", pointer: "#", ...
1150
1146
 
1151
1147
  ## Draft Customization
1152
1148
 
1153
- [**Extending a Draft**](#extending-a-draft) · [**Keyword**](#keyword)
1149
+ [Extending a Draft](#extending-a-draft) · [Overwrite format validator](#overwrite-a-format-validator) · [Keyword](#keyword)
1154
1150
 
1155
1151
  _json-schema-library_ uses the concept of **drafts** to support different versions of the JSON Schema specification — such as Draft 04, Draft 07, or 2020-12 — and to allow customization of schema behavior.
1156
1152
 
@@ -1549,21 +1545,7 @@ const myDraft = extendDraft(draft2020, {
1549
1545
  });
1550
1546
  ```
1551
1547
 
1552
- ### errorMessages
1553
-
1554
- You can set custom errors messages locally by using the errors-keyword:
1555
-
1556
- ```ts
1557
- const { errors } = compileSchema({
1558
- type: "array",
1559
- minItems: 2,
1560
- errorMessages: {
1561
- "min-items-error": "Custom error {{minItems}}"
1562
- }
1563
- }).validate([1]);
1564
-
1565
- assert.deepEqual(errors[0].message, "Custom error 2");
1566
- ```
1548
+ ## Settings
1567
1549
 
1568
1550
  ### regexFlags
1569
1551
 
@@ -0,0 +1 @@
1
+ var e=Object.create,t=Object.defineProperty,n=Object.getOwnPropertyDescriptor,r=Object.getOwnPropertyNames,i=Object.getPrototypeOf,a=Object.prototype.hasOwnProperty,o=(e,t)=>()=>(t||e((t={exports:{}}).exports,t),t.exports),s=(e,i,o,s)=>{if(i&&typeof i==`object`||typeof i==`function`)for(var c=r(i),l=0,u=c.length,d;l<u;l++)d=c[l],!a.call(e,d)&&d!==o&&t(e,d,{get:(e=>i[e]).bind(null,d),enumerable:!(s=n(i,d))||s.enumerable});return e},c=(n,r,a)=>(a=n==null?{}:e(i(n)),s(r||!n||!n.__esModule?t(a,`default`,{value:n,enumerable:!0}):a,n));Object.defineProperty(exports,`n`,{enumerable:!0,get:function(){return c}}),Object.defineProperty(exports,`t`,{enumerable:!0,get:function(){return o}});