json-schema-library 7.4.5 → 8.0.0-rc2
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 +63 -59
- package/TASKS.md +2 -0
- package/dist/index.d.ts +16 -10
- package/dist/jsonSchemaLibrary.js +1 -1
- package/dist/lib/SchemaService.d.ts +8 -8
- package/dist/lib/addRemoteSchema.d.ts +2 -2
- package/dist/lib/addValidator.d.ts +10 -10
- package/dist/lib/compile/getRef.d.ts +2 -2
- package/dist/lib/compile/index.d.ts +2 -2
- package/dist/lib/compile/types.d.ts +3 -3
- package/dist/lib/createSchemaOf.d.ts +2 -2
- package/dist/lib/draft/index.d.ts +42 -41
- package/dist/lib/draft04/index.d.ts +2 -2
- package/dist/lib/draft06/compile/index.d.ts +2 -2
- package/dist/lib/draft06/index.d.ts +2 -2
- package/dist/lib/draft06/validation/keyword.d.ts +2 -2
- package/dist/lib/draft06/validation/type.d.ts +3 -3
- package/dist/lib/draft07/index.d.ts +2 -2
- package/dist/lib/each.d.ts +5 -5
- package/dist/lib/eachSchema.d.ts +3 -3
- package/dist/lib/features/allOf.d.ts +18 -0
- package/dist/lib/features/anyOf.d.ts +19 -0
- package/dist/lib/features/dependencies.d.ts +18 -0
- package/dist/lib/features/if.d.ts +18 -0
- package/dist/lib/features/oneOf.d.ts +27 -0
- package/dist/lib/getChildSchemaSelection.d.ts +3 -3
- package/dist/lib/getSchema.d.ts +2 -2
- package/dist/lib/getTemplate.d.ts +3 -3
- package/dist/lib/isValid.d.ts +4 -4
- package/dist/lib/jsoneditor/index.d.ts +2 -2
- package/dist/lib/mergeSchema.d.ts +6 -0
- package/dist/lib/reduceSchema.d.ts +11 -0
- package/dist/lib/resolveDynamicSchema.d.ts +21 -0
- package/dist/lib/resolveRef.merge.d.ts +2 -2
- package/dist/lib/resolveRef.strict.d.ts +2 -2
- package/dist/lib/schema/createOneOfSchemaResult.d.ts +4 -0
- package/dist/lib/schema/getTypeDefs.d.ts +3 -3
- package/dist/lib/schema/getTypeId.d.ts +2 -2
- package/dist/lib/step.d.ts +4 -4
- package/dist/lib/types.d.ts +10 -10
- package/dist/lib/utils/copy.d.ts +2 -2
- package/dist/lib/utils/createCustomError.d.ts +4 -4
- package/dist/lib/utils/filter.d.ts +3 -3
- package/dist/lib/utils/isObject.d.ts +1 -0
- package/dist/lib/utils/omit.d.ts +11 -0
- package/dist/lib/utils/uniqueItems.d.ts +4 -0
- package/dist/lib/validate.d.ts +5 -5
- package/dist/lib/validateAsync.d.ts +8 -8
- package/dist/lib/validation/format.d.ts +2 -2
- package/dist/lib/validation/keyword.d.ts +2 -2
- package/dist/lib/validation/type.d.ts +3 -3
- package/dist/module/index.js +14 -8
- package/dist/module/lib/SchemaService.js +6 -6
- package/dist/module/lib/addValidator.js +15 -15
- package/dist/module/lib/config/strings.js +2 -1
- package/dist/module/lib/draft/index.js +7 -6
- package/dist/module/lib/draft04/index.js +3 -3
- package/dist/module/lib/draft06/index.js +3 -3
- package/dist/module/lib/draft06/validation/keyword.js +24 -34
- package/dist/module/lib/draft06/validation/type.js +15 -15
- package/dist/module/lib/draft07/index.js +3 -3
- package/dist/module/lib/each.js +7 -7
- package/dist/module/lib/eachSchema.js +1 -1
- package/dist/module/lib/features/allOf.js +60 -0
- package/dist/module/lib/features/anyOf.js +56 -0
- package/dist/module/lib/features/dependencies.js +81 -0
- package/dist/module/lib/features/if.js +31 -0
- package/dist/module/lib/features/oneOf.js +204 -0
- package/dist/module/lib/getChildSchemaSelection.js +12 -8
- package/dist/module/lib/getSchema.js +2 -2
- package/dist/module/lib/getTemplate.js +99 -100
- package/dist/module/lib/isValid.js +3 -3
- package/dist/module/lib/jsoneditor/index.js +2 -2
- package/dist/module/lib/mergeSchema.js +27 -0
- package/dist/module/lib/reduceSchema.js +20 -0
- package/dist/module/lib/resolveDynamicSchema.js +68 -0
- package/dist/module/lib/schema/createOneOfSchemaResult.js +11 -0
- package/dist/module/lib/schema/getTypeDefs.js +1 -1
- package/dist/module/lib/schema/getTypeId.js +1 -1
- package/dist/module/lib/step.js +47 -105
- package/dist/module/lib/types.js +2 -2
- package/dist/module/lib/utils/__.js +2 -1
- package/dist/module/lib/utils/copy.js +2 -1
- package/dist/module/lib/utils/filter.js +3 -3
- package/dist/module/lib/utils/isObject.js +4 -0
- package/dist/module/lib/utils/omit.js +25 -0
- package/dist/module/lib/utils/render.js +12 -1
- package/dist/module/lib/utils/uniqueItems.js +6 -0
- package/dist/module/lib/validate.js +14 -14
- package/dist/module/lib/validateAsync.js +8 -8
- package/dist/module/lib/validation/errors.js +2 -1
- package/dist/module/lib/validation/format.js +21 -21
- package/dist/module/lib/validation/keyword.js +73 -129
- package/dist/module/lib/validation/type.js +22 -22
- package/index.ts +23 -17
- package/lib/SchemaService.ts +13 -13
- package/lib/addRemoteSchema.ts +2 -2
- package/lib/addValidator.ts +18 -18
- package/lib/compile/getRef.ts +2 -2
- package/lib/compile/index.ts +5 -5
- package/lib/compile/types.ts +3 -3
- package/lib/config/strings.ts +2 -1
- package/lib/createSchemaOf.ts +3 -3
- package/lib/draft/index.ts +47 -41
- package/lib/draft04/index.ts +5 -5
- package/lib/draft06/compile/index.ts +4 -4
- package/lib/draft06/index.ts +5 -5
- package/lib/draft06/validation/keyword.ts +28 -41
- package/lib/draft06/validation/type.ts +24 -24
- package/lib/draft07/index.ts +5 -5
- package/lib/each.ts +12 -12
- package/lib/eachSchema.ts +8 -10
- package/lib/features/allOf.ts +73 -0
- package/lib/features/anyOf.ts +70 -0
- package/lib/features/dependencies.ts +108 -0
- package/lib/features/if.ts +43 -0
- package/lib/features/oneOf.ts +253 -0
- package/lib/getChildSchemaSelection.ts +14 -10
- package/lib/getSchema.ts +8 -8
- package/lib/getTemplate.ts +131 -152
- package/lib/isValid.ts +7 -7
- package/lib/jsoneditor/index.ts +4 -4
- package/lib/mergeSchema.ts +30 -0
- package/lib/reduceSchema.ts +24 -0
- package/lib/resolveDynamicSchema.ts +82 -0
- package/lib/resolveRef.merge.ts +2 -4
- package/lib/resolveRef.strict.ts +2 -2
- package/lib/schema/createOneOfSchemaResult.ts +17 -0
- package/lib/schema/getTypeDefs.ts +4 -5
- package/lib/schema/getTypeId.ts +3 -3
- package/lib/step.ts +63 -149
- package/lib/types.ts +14 -14
- package/lib/utils/__.ts +1 -2
- package/lib/utils/copy.ts +2 -1
- package/lib/utils/createCustomError.ts +4 -4
- package/lib/utils/filter.ts +5 -5
- package/lib/utils/isObject.ts +5 -0
- package/lib/utils/omit.ts +25 -0
- package/lib/utils/render.ts +14 -1
- package/lib/utils/uniqueItems.ts +6 -0
- package/lib/validate.ts +20 -19
- package/lib/validateAsync.ts +14 -14
- package/lib/validation/errors.ts +2 -1
- package/lib/validation/format.ts +24 -24
- package/lib/validation/keyword.ts +81 -150
- package/lib/validation/type.ts +37 -38
- package/package.json +18 -17
- package/dist/lib/resolveAllOf.d.ts +0 -3
- package/dist/lib/resolveAnyOf.d.ts +0 -3
- package/dist/lib/resolveOneOf.fuzzy.d.ts +0 -12
- package/dist/lib/resolveOneOf.strict.d.ts +0 -12
- package/dist/module/lib/resolveAllOf.js +0 -44
- package/dist/module/lib/resolveAnyOf.js +0 -19
- package/dist/module/lib/resolveOneOf.fuzzy.js +0 -111
- package/dist/module/lib/resolveOneOf.strict.js +0 -80
- package/lib/resolveAllOf.ts +0 -55
- package/lib/resolveAnyOf.ts +0 -29
- package/lib/resolveOneOf.fuzzy.ts +0 -142
- package/lib/resolveOneOf.strict.ts +0 -98
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
## Changelog
|
|
2
2
|
|
|
3
|
+
- [Breaking] renamed `JSON` types and variables to `Json`
|
|
4
|
+
- [Breaking] remove `oneOfSchema` helper property in favor of `getOneOfOrigin()` non-enumerable function
|
|
5
|
+
- [Breaking] `getTemplate` will add only required properties per default. Use `addOptionalProps:true` to change this behaviour
|
|
6
|
+
- [add] introduce `mergeSchema`, `reduceSchema` and `resolveDynamicSchema`
|
|
7
|
+
- [refactor] move dynamic resolvers `anyOf`, `allOf`, `oneOf`, `dependencies` and `if` to features modules
|
|
8
|
+
|
|
3
9
|
### 7.0.0
|
|
4
10
|
|
|
5
11
|
- changed core interface to draft for simpler configuration using a configuration map
|
package/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
<p
|
|
1
|
+
<p style="text-align:center"><img src="./docs/json-schema-library.png" width="256" alt="json-schema-library"></p>
|
|
2
2
|
|
|
3
3
|
|
|
4
4
|
**Customizable and hackable json-validator and json-schema utilities for traversal, data generation and validation**
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
> lessens the pain to build custom tools around json-schema.
|
|
10
10
|
|
|
11
11
|
|
|
12
|
-
<p
|
|
12
|
+
<p style="text-align:center">
|
|
13
13
|
<a href="#draft-methods">draft methods</a> | <a href="#draft-extensions">draft extensions</a> | <a href="#draft-customization">draft customization</a> | <a href="#breaking-changes">breaking changes</a>
|
|
14
14
|
</p>
|
|
15
15
|
|
|
@@ -26,7 +26,7 @@ This library currently supports draft4, draft6 and draft7 features [@see benchma
|
|
|
26
26
|
|
|
27
27
|
**usage**
|
|
28
28
|
|
|
29
|
-
`json-schema-library` exposes a `Draft` for most json-schema drafts. Each `Draft` can be instantiated and offers a common set of actions working on the specified json-schema version.
|
|
29
|
+
`json-schema-library` exposes a `Draft` for most json-schema drafts. Each `Draft` can be instantiated and offers a common set of actions working on the specified json-schema version. Example:
|
|
30
30
|
|
|
31
31
|
```ts
|
|
32
32
|
import { Draft04, Draft06, Draft07, Draft, JSONError } from "json-schema-library";
|
|
@@ -58,25 +58,25 @@ What follows is a description of the main draft methods.
|
|
|
58
58
|
|
|
59
59
|
### validate
|
|
60
60
|
|
|
61
|
-
`validate` is a complete _json-schema validator_ for your input data. Calling _validate_ will return a list of validation errors for the passed data
|
|
61
|
+
`validate` is a complete _json-schema validator_ for your input data. Calling _validate_ will return a list of validation errors for the passed data.
|
|
62
62
|
|
|
63
63
|
```ts
|
|
64
64
|
const jsonSchema = new Draft07(myJsonSchema);
|
|
65
65
|
const errors: JSONError[] = jsonSchema.validate(myData);
|
|
66
66
|
```
|
|
67
67
|
|
|
68
|
-
Additionally, you can validate a
|
|
68
|
+
Additionally, you can validate a sub-schema and its data. Doing this, the intial schema will be used as rootSchema (for example, to resolve `$ref` from _definitions_).
|
|
69
69
|
|
|
70
70
|
```ts
|
|
71
71
|
const jsonSchema = new Draft07(myJsonSchema);
|
|
72
72
|
const errors: JSONError[] = jsonSchema.validate("my-string", { type: "number" });
|
|
73
73
|
```
|
|
74
74
|
|
|
75
|
-
>
|
|
75
|
+
> To prevent some errors when using helper methods with an independent sub-schema, please use `compileSchema` if it is not retrieved from a draft-method directly (which was compiled by passing it to Draft). Specifically, if the schema contains a $ref you need to use `compileSchema`. More details in [compileSchema](#compileSchema).
|
|
76
76
|
|
|
77
77
|
<details><summary>About type JSONError</summary>
|
|
78
78
|
|
|
79
|
-
In `json-schema-library` all errors are in the format of a `JSONError
|
|
79
|
+
In `json-schema-library` all errors are in the format of a `JSONError`:
|
|
80
80
|
|
|
81
81
|
```ts
|
|
82
82
|
type JSONError = {
|
|
@@ -88,7 +88,7 @@ type JSONError = {
|
|
|
88
88
|
};
|
|
89
89
|
```
|
|
90
90
|
|
|
91
|
-
|
|
91
|
+
In almost all cases, a _json-pointer_ is given on _error.data.pointer_, which points to the source within data where the error occured.
|
|
92
92
|
|
|
93
93
|
For more details on how to work with errors, refer to section [custom errors](#custom-errors).
|
|
94
94
|
|
|
@@ -155,7 +155,7 @@ expect(errors).to.deep.equal([
|
|
|
155
155
|
|
|
156
156
|
### isValid
|
|
157
157
|
|
|
158
|
-
`isValid` will return
|
|
158
|
+
`isValid` will return `true` if the given json-data is valid against the json-schema.
|
|
159
159
|
|
|
160
160
|
```ts
|
|
161
161
|
const jsonSchema = new Draft07(myJsonSchema);
|
|
@@ -165,9 +165,9 @@ const isValid: boolean = jsonSchema.isValid(myData);
|
|
|
165
165
|
|
|
166
166
|
### validateAsync
|
|
167
167
|
|
|
168
|
-
>
|
|
168
|
+
> This method is not yet exposed by a draft directly as the API of this is yet unsatisfactory. Nonetheless, this function is in production and can be used reliably.
|
|
169
169
|
|
|
170
|
-
Optional support for onError helper, which is invoked for each error (after being resolved)
|
|
170
|
+
Optional support for `onError` helper, which is invoked for each error (after being resolved):
|
|
171
171
|
|
|
172
172
|
```ts
|
|
173
173
|
import { Draft07, JSONError, validateAsync } from "json-schema-library";
|
|
@@ -181,21 +181,21 @@ validateAsync(draft, "", { onError: (err: JSONError) => {}, schema: draft.getSch
|
|
|
181
181
|
|
|
182
182
|
### getTemplate
|
|
183
183
|
|
|
184
|
-
`getTemplate` creates input data from a json-schema that is valid to the schema. Where possible the json-schema
|
|
184
|
+
`getTemplate` creates input data from a json-schema that is valid to the schema. Where possible, the json-schema `default` property will be used to initially setup input data. Otherwise, the first values encountered (enum values, initial values, etc.) are user to build up the json-data.
|
|
185
185
|
|
|
186
186
|
```ts
|
|
187
187
|
const jsonSchema = new Draft07(myJsonSchema);
|
|
188
188
|
const myData = jsonSchema.getTemplate();
|
|
189
189
|
```
|
|
190
190
|
|
|
191
|
-
Additionally, you can pass input data. `getTemplate` will then complement any missing values from the schema, while keeping the initial values
|
|
191
|
+
Additionally, you can pass input data. `getTemplate` will then complement any missing values from the schema, while keeping the initial values.
|
|
192
192
|
|
|
193
193
|
```ts
|
|
194
194
|
const jsonSchema = new Draft07(myJsonSchema);
|
|
195
195
|
const myData = jsonSchema.getTemplate({ name: "input-data" });
|
|
196
196
|
```
|
|
197
197
|
|
|
198
|
-
**Note
|
|
198
|
+
**Note:** If you are using references in your schema, `getTemplate` will only resolve the first _$ref_ in each path, ensuring no inifinte data structures are created. In case the limit of **1** _$ref_ resolution is too low, you can modify the value globally one by adjusting the json-schema-library settings:
|
|
199
199
|
|
|
200
200
|
```ts
|
|
201
201
|
import { settings } from "json-schema-library";
|
|
@@ -209,6 +209,7 @@ import { Draft07, JSONSchema } from 'json-schema-library';
|
|
|
209
209
|
|
|
210
210
|
const myJsonSchema: JSONSchema = {
|
|
211
211
|
type: 'object',
|
|
212
|
+
required: ['name', 'option', 'list'],
|
|
212
213
|
properties: {
|
|
213
214
|
name: { type: 'string' },
|
|
214
215
|
option: {
|
|
@@ -245,6 +246,7 @@ import { Draft07, JSONSchema } from "json-schema-library";
|
|
|
245
246
|
|
|
246
247
|
const myJsonSchema: JSONSchema = {
|
|
247
248
|
type: "object",
|
|
249
|
+
required: ['name', 'option', 'list'],
|
|
248
250
|
properties: {
|
|
249
251
|
name: { type: "string" },
|
|
250
252
|
option: {
|
|
@@ -316,7 +318,7 @@ expect(calls).to.deep.equal([
|
|
|
316
318
|
|
|
317
319
|
### eachSchema
|
|
318
320
|
|
|
319
|
-
`eachSchema` emits each sub
|
|
321
|
+
`eachSchema` emits each sub-schema definition to a callback.
|
|
320
322
|
|
|
321
323
|
```ts
|
|
322
324
|
const jsonSchema = new Draft07(mySchema);
|
|
@@ -365,7 +367,7 @@ expect(calls).to.deep.equal([
|
|
|
365
367
|
|
|
366
368
|
### getSchema
|
|
367
369
|
|
|
368
|
-
`getSchema` retrieves the json-schema of a specific location in data. The location in data is given by a _json-pointer_. In many cases the schema can be retrieved without passing the actual data, but in situations where the schema is dynamic
|
|
370
|
+
`getSchema` retrieves the json-schema of a specific location in data. The location in data is given by a _json-pointer_. In many cases the schema can be retrieved without passing the actual data, but in situations where the schema is dynamic (e.g., in _oneOf_, _dependencies_, etc.), the data is required or will return a _JSONError_ if the location cannot be found.
|
|
369
371
|
|
|
370
372
|
```ts
|
|
371
373
|
const jsonSchema = new Draft07(mySchema);
|
|
@@ -388,11 +390,11 @@ const mySchema = {
|
|
|
388
390
|
{
|
|
389
391
|
type: "object",
|
|
390
392
|
required: ["name"],
|
|
391
|
-
properties: {
|
|
392
|
-
name: {
|
|
393
|
-
type: "string",
|
|
394
|
-
title: "name of item"
|
|
395
|
-
}
|
|
393
|
+
properties: {
|
|
394
|
+
name: {
|
|
395
|
+
type: "string",
|
|
396
|
+
title: "name of item"
|
|
397
|
+
}
|
|
396
398
|
}
|
|
397
399
|
},
|
|
398
400
|
{
|
|
@@ -433,16 +435,18 @@ expect(schemaOfItem).to.deep.equal({
|
|
|
433
435
|
|
|
434
436
|
<details><summary>About JSONPointer</summary>
|
|
435
437
|
|
|
436
|
-
**[JSON-Pointer](https://tools.ietf.org/html/rfc6901)** defines a string syntax for identifying a specific value within a JSON document and is [supported by JSON-Schema](https://json-schema.org/understanding-json-schema/structuring.html). Given a JSON document, it behaves similar to a [lodash path](https://lodash.com/docs/4.17.5#get) (`a[0].b.c`), which follows JS-syntax, but instead uses `/` separators
|
|
438
|
+
**[JSON-Pointer](https://tools.ietf.org/html/rfc6901)** defines a string syntax for identifying a specific value within a JSON document and is [supported by JSON-Schema](https://json-schema.org/understanding-json-schema/structuring.html). Given a JSON document, it behaves similar to a [lodash path](https://lodash.com/docs/4.17.5#get) (`a[0].b.c`), which follows JS-syntax, but instead uses `/` separators (e.g., `a/0/b/c`). In the end, you describe a path into the JSON data to a specific point.
|
|
437
439
|
|
|
438
440
|
</details>
|
|
439
441
|
|
|
440
442
|
|
|
441
443
|
### getChildSchemaSelection
|
|
442
444
|
|
|
443
|
-
`getChildSchemaSelection` returns a list of available sub
|
|
445
|
+
`getChildSchemaSelection` returns a list of available sub-schemas for the given property. In many cases, a single schema will be returned. For _oneOf_-schemas, a list of possible options is returned.
|
|
446
|
+
|
|
447
|
+
This helper always returns a list of schemas.
|
|
444
448
|
|
|
445
|
-
**Note
|
|
449
|
+
**Note:** This helper currenly supports a subset of json-schema for multiple results, mainly _oneOf_-definitions
|
|
446
450
|
|
|
447
451
|
```ts
|
|
448
452
|
const jsonSchema = new Draft07(mySchema);
|
|
@@ -475,7 +479,7 @@ expect(schemas).to.deep.equal([
|
|
|
475
479
|
])
|
|
476
480
|
```
|
|
477
481
|
|
|
478
|
-
</details>
|
|
482
|
+
</details>
|
|
479
483
|
|
|
480
484
|
|
|
481
485
|
|
|
@@ -519,11 +523,11 @@ expect(res).to.deep.eq({ type: "number" });
|
|
|
519
523
|
|
|
520
524
|
### addRemoteSchema
|
|
521
525
|
|
|
522
|
-
`addRemoteSchema` lets you add additional schemas that can be referenced by an
|
|
526
|
+
`addRemoteSchema` lets you add additional schemas that can be referenced by an URL using `$ref`. Use this to combine multiple schemas without changing the actual schema.
|
|
523
527
|
|
|
524
528
|
```ts
|
|
525
|
-
const jsonSchema = new Draft07({
|
|
526
|
-
$ref: "http://json-schema.org/draft-07/schema#definitions/nonNegativeInteger"
|
|
529
|
+
const jsonSchema = new Draft07({
|
|
530
|
+
$ref: "http://json-schema.org/draft-07/schema#definitions/nonNegativeInteger"
|
|
527
531
|
});
|
|
528
532
|
jsonSchema.addRemoteSchema("http://json-schema.org/draft-07/schema", draft07Schema);
|
|
529
533
|
```
|
|
@@ -563,7 +567,7 @@ const schema: JSONSchema = jsonSchema.createSchemaOf({ title: "initial value" })
|
|
|
563
567
|
|
|
564
568
|
### compileSchema
|
|
565
569
|
|
|
566
|
-
`compileSchema` adds _$ref_ resolution support to a json-schema. Internally, each draft compiles a passed schema on its own, but when passing additional schemas to individual functions, `compileSchema` has to be called manually for json-schemas containing $
|
|
570
|
+
`compileSchema` adds _$ref_ resolution support to a json-schema. Internally, each draft compiles a passed schema on its own, but when passing additional schemas to individual functions, `compileSchema` has to be called manually for json-schemas containing _$ref_-references.
|
|
567
571
|
|
|
568
572
|
```ts
|
|
569
573
|
const jsonSchema = new Draft07(mySchema);
|
|
@@ -571,7 +575,7 @@ const compiledSchema = jsonSchema.compileSchema({ $ref: "/$defs/table" });
|
|
|
571
575
|
const tableSchema = compiledSchema.getRef();
|
|
572
576
|
```
|
|
573
577
|
|
|
574
|
-
**Note
|
|
578
|
+
**Note:** that `draft.compileSchema` compiles a schema under the current rootSchema. That is, definitions from root schema will be copied to the local schema, to enable _$ref_ resolutions.
|
|
575
579
|
|
|
576
580
|
|
|
577
581
|
## Draft extensions
|
|
@@ -587,9 +591,9 @@ expression, the example will be printed in the error message.
|
|
|
587
591
|
|
|
588
592
|
### oneOfProperty
|
|
589
593
|
|
|
590
|
-
For `oneOf` resolution json-schema states that
|
|
594
|
+
For `oneOf` resolution, json-schema states that data is valid if it validates against exactly one of those sub-schemas. In some scenarios this is unwanted behaviour, as the actual `oneOf` schema is known and only validation errors of this exact sub-schema should be returned.
|
|
591
595
|
|
|
592
|
-
For an explicit `oneOf` resolution the json-schema may be extended by a property `oneOfProperty`. This will always associate an entry with a matching value (instead of schema validation) and return only this schema or validation errors, depending on the current task
|
|
596
|
+
For an explicit `oneOf` resolution, the json-schema may be extended by a property `oneOfProperty`. This will always associate an entry with a matching value (instead of schema validation) and return only this schema or validation errors, depending on the current task. For example:
|
|
593
597
|
|
|
594
598
|
```ts
|
|
595
599
|
const schema = {
|
|
@@ -613,9 +617,9 @@ const schema = {
|
|
|
613
617
|
const resolvedSchema = jsonSchema.resolveOneOf({ id: "2", title: "not a number" }, schema);
|
|
614
618
|
|
|
615
619
|
// will always return (even if invalid)
|
|
616
|
-
expect(resolvedSchema).to.deep.eq({
|
|
617
|
-
type: "object",
|
|
618
|
-
properties: { id: { const: "2" }, title: { type: "number" } }
|
|
620
|
+
expect(resolvedSchema).to.deep.eq({
|
|
621
|
+
type: "object",
|
|
622
|
+
properties: { id: { const: "2" }, title: { type: "number" } }
|
|
619
623
|
});
|
|
620
624
|
```
|
|
621
625
|
|
|
@@ -625,7 +629,7 @@ expect(resolvedSchema).to.deep.eq({
|
|
|
625
629
|
[custom resolvers](#custom-resolvers) | [custom validators](#custom-validators) | [custom errors](#custom-errors)
|
|
626
630
|
|
|
627
631
|
|
|
628
|
-
Each `Draft` in `json-schema-library` is build around a [DraftConfig](./lib/draft/index.ts#19). A `DraftConfig` holds all _functions_ and _configurations_ for each json-schema drafts. The `DraftConfig` is your main way to alter or extend behaviour for `json-schema-library`. You can either create your own _draftConfig_ or adjust any existing _draftConfig_. For the current drafts (4-7), each _draftConfig_ is exposed along with its actual _class_
|
|
632
|
+
Each `Draft` in `json-schema-library` is build around a [DraftConfig](./lib/draft/index.ts#19). A `DraftConfig` holds all _functions_ and _configurations_ for each json-schema drafts. The `DraftConfig` is your main way to alter or extend behaviour for `json-schema-library`. You can either create your own _draftConfig_ or adjust any existing _draftConfig_. For the current drafts (4-7), each _draftConfig_ is exposed along with its actual _class_. For example:
|
|
629
633
|
|
|
630
634
|
```ts
|
|
631
635
|
import { Draft, Draft07, draft07Config } from 'json-schema-library';
|
|
@@ -636,9 +640,9 @@ new Draft07(mySchema, {});
|
|
|
636
640
|
new Draft07(mySchema);
|
|
637
641
|
```
|
|
638
642
|
|
|
639
|
-
|
|
643
|
+
All draft configurations for specific `Draft` classes accept a partial configuration that lets you overwrite default behaviour:
|
|
640
644
|
|
|
641
|
-
> replace the strict resolveOneOf behaviour to use fuzzy search instead:
|
|
645
|
+
> replace the strict `resolveOneOf` behaviour to use fuzzy search instead:
|
|
642
646
|
|
|
643
647
|
```ts
|
|
644
648
|
import { Draft07, draft07Config, resolveOneOfFuzzy } from 'json-schema-library';
|
|
@@ -650,12 +654,12 @@ new Draft({ ...draft07Config, resolveOneOf: resolveOneOfFuzzy }, mySchema);
|
|
|
650
654
|
|
|
651
655
|
### custom resolvers
|
|
652
656
|
|
|
653
|
-
A _resolver_ is a simple method implementing a specific feature of json-schema to retrieve a sub
|
|
657
|
+
A _resolver_ is a simple method implementing a specific feature of json-schema to retrieve a sub-schema. Implementing the signature of each resolver you can create and pass your own resolvers.
|
|
654
658
|
|
|
655
659
|
|
|
656
660
|
#### `resolveRef` with merge
|
|
657
661
|
|
|
658
|
-
The default json-schema behaviour for `$ref` resolution is to replace the schema where a `$ref` is defined. In some scenarios you what to add context
|
|
662
|
+
The default json-schema behaviour for `$ref` resolution is to replace the schema where a `$ref` is defined. In some scenarios you what to add context-specific information (e.g., a specific _title_). For this, a modified `$ref`-resolver is exposed by `json-schema-library`:
|
|
659
663
|
|
|
660
664
|
```ts
|
|
661
665
|
import { Draft07, draft07Config, resolveRefMerge } from 'json-schema-library';
|
|
@@ -664,7 +668,7 @@ const jsonSchema = new Draft07(mySchema, { resolveRef: resolveRefMerge });
|
|
|
664
668
|
|
|
665
669
|
`resolveRefMerge` performs a shallow merge (first level of properties), adding the local schemas properties last.
|
|
666
670
|
|
|
667
|
-
**
|
|
671
|
+
**Caution:** With this resolver, it is possible to overwrite json-schema behavioural properties. Treat with care.
|
|
668
672
|
|
|
669
673
|
<details><summary>Example</summary>
|
|
670
674
|
|
|
@@ -700,7 +704,7 @@ expect(subHeaderSchema).to.eq({
|
|
|
700
704
|
|
|
701
705
|
#### `resolveOneOf` fuzzy search
|
|
702
706
|
|
|
703
|
-
The default json-schema behaviour for `oneOf` resolution is to validate all contained _oneOf_-schemas and return the one schema that validates against the given input data. If no item validates completely an error returned, containing all validation errors of all schemas. When you are interested in the actual error (
|
|
707
|
+
The default json-schema behaviour for `oneOf` resolution is to validate all contained _oneOf_-schemas and return the one schema that validates against the given input data. If no item validates completely an error returned, containing all validation errors of all schemas. When you are interested in the actual error (rather than simply determining “Is the data is valid or not?”), this is behaviour is not very helpful as the result is hard to read.
|
|
704
708
|
|
|
705
709
|
`json-schema-library` exposes a method `resolveOneOfFuzzy`, which will return a single schema in cases where no valid schema could be resolved. `resolveOneOfFuzzy` uses a simple scoring mechanism to return the best fitting schema for the given input data. Thus, `resolveOneOfFuzzy` may return schemas that do not validate a given input data.
|
|
706
710
|
|
|
@@ -712,15 +716,15 @@ const jsonSchema = new Draft07(mySchema, { resolveOneOf: resolveOneOfFuzzy });
|
|
|
712
716
|
|
|
713
717
|
### custom validators
|
|
714
718
|
|
|
715
|
-
All json-schema validation is done using validator functions for _keywords_ and _formats_.
|
|
719
|
+
All json-schema validation is done using validator functions for _keywords_ and _formats_.
|
|
716
720
|
|
|
717
|
-
**keyword validators** are called for each keyword defined on a json-schema,
|
|
721
|
+
**keyword validators** are called for each keyword defined on a json-schema. For example, the following schema will run two keyword-validators (one for `items` and one of `minItems`) which are defined in `draft.validateKeyword.items` and `draft.validateKeyword.minItems`.
|
|
718
722
|
|
|
719
723
|
```ts
|
|
720
724
|
{ type: "object", items: {}, minItems: 1 }
|
|
721
725
|
```
|
|
722
726
|
|
|
723
|
-
Since valid json-schema keywords vary by their `type` an additional mapping registers, which keyword should be tested per schema-type. This mapping is defined in `draft.typeKeywords
|
|
727
|
+
Since valid json-schema keywords vary by their `type` an additional mapping registers, which keyword should be tested per schema-type. This mapping is defined in `draft.typeKeywords`:
|
|
724
728
|
|
|
725
729
|
```ts
|
|
726
730
|
import { draft07Config } from "json-schema-library";
|
|
@@ -732,7 +736,7 @@ console.log(draft07Config.typeKeywords.array);
|
|
|
732
736
|
|
|
733
737
|
> The keyword **format** is also registered in `draft.validateKeyword.format`, but each actual format validation is defined as follows:
|
|
734
738
|
|
|
735
|
-
**format validators** are called on each
|
|
739
|
+
**format validators** are called on each occurrence of a property format in a json-schema. In the next example, the schema will run the _email_-validator given in `draft.validateFormat.email`:
|
|
736
740
|
|
|
737
741
|
```ts
|
|
738
742
|
{ type: "string", format: "email" }
|
|
@@ -740,9 +744,9 @@ console.log(draft07Config.typeKeywords.array);
|
|
|
740
744
|
|
|
741
745
|
#### add custom keyword validator
|
|
742
746
|
|
|
743
|
-
To add or overwrite a keyword validator you
|
|
747
|
+
To add or overwrite a keyword validator, you must add a validator function on your draft config in `validateKeyword`.
|
|
744
748
|
|
|
745
|
-
|
|
749
|
+
Using specific Draft configuration, where draft configuration objects will be merged:
|
|
746
750
|
|
|
747
751
|
```ts
|
|
748
752
|
import { Draft07, JSONValidator } from "json-schema-library";
|
|
@@ -759,7 +763,7 @@ const jsonSchema = new Draft07(mySchema, {
|
|
|
759
763
|
});
|
|
760
764
|
```
|
|
761
765
|
|
|
762
|
-
|
|
766
|
+
**Example:** Manually extending draft configuration:
|
|
763
767
|
|
|
764
768
|
```ts
|
|
765
769
|
import { Draft, draft07Config, JSONValidator } from "json-schema-library";
|
|
@@ -785,7 +789,7 @@ const jsonSchema = new Draft(myDraftConfiguration, mySchema);
|
|
|
785
789
|
|
|
786
790
|
#### add custom format validator
|
|
787
791
|
|
|
788
|
-
To add or overwrite a format validator you
|
|
792
|
+
To add or overwrite a format validator you must add a validator function on your draft config in `validateFormat`.
|
|
789
793
|
|
|
790
794
|
```ts
|
|
791
795
|
import { Draft07, JSONValidator } from "json-schema-library";
|
|
@@ -818,10 +822,10 @@ Each error message in `config.strings` receives the `data`-property of an error.
|
|
|
818
822
|
import { render } from "json-schema-library";
|
|
819
823
|
|
|
820
824
|
render(
|
|
821
|
-
"Expected given value `{{value}}` in `{{pointer}}` to be one of `{{values}}`",
|
|
825
|
+
"Expected given value `{{value}}` in `{{pointer}}` to be one of `{{values}}`",
|
|
822
826
|
{ pointer: "[A]", value: "[B]" }
|
|
823
827
|
);
|
|
824
|
-
// "Expected given value `[B]` in `[A]` to be one of ``"
|
|
828
|
+
// "Expected given value `[B]` in `[A]` to be one of ``"
|
|
825
829
|
```
|
|
826
830
|
|
|
827
831
|
|
|
@@ -864,16 +868,16 @@ const error: JSONError = createError("EnumError", { data: { pointer: "#/location
|
|
|
864
868
|
|
|
865
869
|
### v7.0.0
|
|
866
870
|
|
|
867
|
-
|
|
871
|
+
With version `v7.0.0`, library export and Draft API has changed heavily. The API is now more consistent across draft-versions and offers a simple and consistent configuration interface for existing and custom drafts. In addition, most standalone functions are no longer exposed separately, but under its current _draftConfigs_ and mainly on each draft-instance. This will help to reduce confusion when consuming this API.
|
|
868
872
|
|
|
869
|
-
The above documentation reflects all
|
|
873
|
+
The above documentation reflects all these changes. Just reach out if you have troubles migrating to the latest version.
|
|
870
874
|
|
|
871
|
-
<details><summary>Details
|
|
875
|
+
<details><summary>Details of breaking changes</summary>
|
|
872
876
|
|
|
873
877
|
- replaced `Core` interface by new `Draft` interface
|
|
874
878
|
- changed export of `Interface` to `Draft`
|
|
875
879
|
- renamed `addSchema` to `addRemoteSchema`
|
|
876
|
-
- changed
|
|
880
|
+
- changed API of `compileSchema` to have an additional schema-parameter for rootSchema reference
|
|
877
881
|
- changed `compileSchema` and `addRemoteSchema` to work on instance state, instead of global state
|
|
878
882
|
- `addRemoteSchema`, `compileSchema` now requires draft instance as first parameter
|
|
879
883
|
- removed direct export of following functions: `addValidator`, `compileSchema`, `createSchemaOf`, `each`, `eachSchema`, `getChildSchemaSelection`, `getSchema`, `getTemplate`, `isValid`, `step`, `validate`. They are still accessible under the draftConfigs of each draft-version
|
|
@@ -884,14 +888,14 @@ The above documentation reflects all those changes. Just reach out if you have t
|
|
|
884
888
|
|
|
885
889
|
### v6.0.0
|
|
886
890
|
|
|
887
|
-
|
|
891
|
+
With version `v6.0.0` supported json schema drafts are exported directly as `Draft04`, `Draft06`, `Draft07`. Example use: `import { Draft07 } from "json-schema-library"`.
|
|
888
892
|
|
|
889
893
|
|
|
890
894
|
### v5.0.0
|
|
891
895
|
|
|
892
|
-
|
|
896
|
+
With version `v5.0.0` the API has changed to es6 modules, where there is no `default` export, only named exports. Additionally all code has been rewritten in TypeScript. When directly accessing files, switch to `dist/module/*.js`-files for plain js-modules.
|
|
893
897
|
|
|
894
898
|
|
|
895
899
|
### v4.0.0
|
|
896
900
|
|
|
897
|
-
|
|
901
|
+
With version `v4.0.0` the API has changed in order to use the defined (root) schema in draft as default where possible. This means most methods have a changed signature, where `data` is passed before an optional `schema` argument.
|
package/TASKS.md
CHANGED
package/dist/index.d.ts
CHANGED
|
@@ -1,37 +1,43 @@
|
|
|
1
1
|
import { createError, createCustomError } from "./lib/utils/createCustomError";
|
|
2
2
|
import getTypeOf from "./lib/getTypeOf";
|
|
3
|
-
import resolveOneOf from "./lib/
|
|
4
|
-
import resolveAllOf from "./lib/
|
|
5
|
-
import resolveOneOfFuzzy from "./lib/resolveOneOf.fuzzy";
|
|
3
|
+
import { resolveOneOf, resolveOneOfFuzzy } from "./lib/features/oneOf";
|
|
4
|
+
import { resolveAllOf } from "./lib/features/allOf";
|
|
6
5
|
import resolveRef from "./lib/resolveRef.strict";
|
|
7
6
|
import resolveRefMerge from "./lib/resolveRef.merge";
|
|
8
7
|
import SchemaService from "./lib/SchemaService";
|
|
9
8
|
import settings from "./lib/config/settings";
|
|
10
9
|
import validateAsync from "./lib/validateAsync";
|
|
10
|
+
import { reduceSchema } from "./lib/reduceSchema";
|
|
11
|
+
import { resolveDynamicSchema, isDynamicSchema } from "./lib/resolveDynamicSchema";
|
|
12
|
+
import { mergeSchema } from "./lib/mergeSchema";
|
|
11
13
|
import render from "./lib/utils/render";
|
|
12
14
|
import { Draft } from "./lib/draft";
|
|
13
15
|
import { Draft04, draft04Config } from "./lib/draft04";
|
|
14
16
|
import { Draft06, draft06Config } from "./lib/draft06";
|
|
15
17
|
import { Draft07, draft07Config } from "./lib/draft07";
|
|
16
18
|
import { JsonEditor, draftJsonEditorConfig } from "./lib/jsoneditor";
|
|
17
|
-
import {
|
|
19
|
+
import { isJsonError } from "./lib/types";
|
|
18
20
|
declare const config: {
|
|
19
21
|
strings: Record<string, string>;
|
|
20
22
|
};
|
|
21
|
-
export { config, Draft, Draft04, // core implementing draft04 specs
|
|
23
|
+
export { config, createCustomError, createError, Draft, Draft04, // core implementing draft04 specs
|
|
22
24
|
draft04Config, // config implementing draft04 specs
|
|
23
25
|
Draft06, // core implementing draft06 specs
|
|
24
26
|
draft06Config, // config implementing draft06 specs
|
|
25
27
|
Draft07, // core implementing draft07 specs
|
|
26
28
|
draft07Config, // config implementing draft07 specs
|
|
27
|
-
JsonEditor, // adjusted core of draft04 to better support the json-editor
|
|
28
29
|
draftJsonEditorConfig, // adjusted config of draft04 to better support the json-editor
|
|
29
|
-
|
|
30
|
-
|
|
30
|
+
getTypeOf, // returns the javascript datatype
|
|
31
|
+
isDynamicSchema, // NEW
|
|
32
|
+
isJsonError, JsonEditor, // adjusted core of draft04 to better support the json-editor
|
|
33
|
+
mergeSchema, // NEW
|
|
34
|
+
reduceSchema, // NEW
|
|
35
|
+
render, resolveAllOf, resolveDynamicSchema, // NEW
|
|
36
|
+
resolveOneOf, resolveOneOfFuzzy, resolveRef, resolveRefMerge, SchemaService, settings, validateAsync };
|
|
31
37
|
import { DraftConfig } from "./lib/draft";
|
|
32
38
|
import { EachCallback } from "./lib/each";
|
|
33
39
|
import { EachSchemaCallback } from "./lib/eachSchema";
|
|
34
40
|
import { ErrorData, CreateError } from "./lib/utils/createCustomError";
|
|
35
|
-
import {
|
|
41
|
+
import { JsonSchema, JsonPointer, JsonError, JsonValidator, JsonTypeValidator } from "./lib/types";
|
|
36
42
|
import { JSType } from "./lib/getTypeOf";
|
|
37
|
-
export type { CreateError, DraftConfig, EachCallback, EachSchemaCallback, ErrorData,
|
|
43
|
+
export type { CreateError, DraftConfig, EachCallback, EachSchemaCallback, ErrorData, JsonError, JsonPointer, JsonSchema, JsonTypeValidator, JsonValidator, JSType };
|