json-schema-library 11.0.5 → 11.1.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.
Files changed (58) hide show
  1. package/CHANGELOG.md +11 -0
  2. package/README.md +112 -0
  3. package/dist/index.cjs +1 -1
  4. package/dist/index.d.cts +93 -16
  5. package/dist/index.d.mts +93 -16
  6. package/dist/index.mjs +1 -1
  7. package/dist/jlib.js +3 -3
  8. package/index.ts +10 -1
  9. package/package.json +1 -1
  10. package/src/Draft.ts +1 -1
  11. package/src/Keyword.ts +36 -10
  12. package/src/SchemaNode.ts +75 -16
  13. package/src/compileSchema.ts +53 -4
  14. package/src/errors/errors.ts +4 -1
  15. package/src/keywords/$defs.ts +34 -8
  16. package/src/keywords/$ref.ts +12 -0
  17. package/src/keywords/additionalProperties.ts +19 -8
  18. package/src/keywords/allOf.ts +44 -18
  19. package/src/keywords/anyOf.ts +38 -19
  20. package/src/keywords/contains.ts +21 -9
  21. package/src/keywords/dependencies.ts +37 -17
  22. package/src/keywords/dependentRequired.ts +56 -38
  23. package/src/keywords/dependentSchemas.ts +37 -13
  24. package/src/keywords/deprecated.ts +32 -8
  25. package/src/keywords/enum.ts +30 -8
  26. package/src/keywords/exclusiveMaximum.ts +21 -2
  27. package/src/keywords/exclusiveMinimum.ts +22 -3
  28. package/src/keywords/format.ts +21 -2
  29. package/src/keywords/ifthenelse.ts +49 -5
  30. package/src/keywords/items.ts +27 -13
  31. package/src/keywords/maxItems.ts +22 -2
  32. package/src/keywords/maxLength.ts +30 -9
  33. package/src/keywords/maxProperties.ts +30 -9
  34. package/src/keywords/maximum.ts +28 -8
  35. package/src/keywords/minItems.ts +30 -9
  36. package/src/keywords/minLength.ts +30 -9
  37. package/src/keywords/minProperties.ts +26 -5
  38. package/src/keywords/minimum.ts +32 -13
  39. package/src/keywords/multipleOf.ts +33 -12
  40. package/src/keywords/not.ts +23 -10
  41. package/src/keywords/oneOf.ts +29 -9
  42. package/src/keywords/pattern.ts +35 -9
  43. package/src/keywords/properties.ts +35 -12
  44. package/src/keywords/propertyDependencies.test.ts +180 -0
  45. package/src/keywords/propertyDependencies.ts +173 -0
  46. package/src/keywords/propertyNames.ts +26 -14
  47. package/src/keywords/required.ts +31 -8
  48. package/src/keywords/type.ts +53 -16
  49. package/src/keywords/unevaluatedItems.ts +24 -8
  50. package/src/keywords/unevaluatedProperties.ts +24 -7
  51. package/src/keywords/uniqueItems.ts +23 -4
  52. package/src/mergeNode.ts +9 -4
  53. package/src/settings.ts +2 -1
  54. package/src/types.ts +1 -1
  55. package/src/utils/isListOfStrings.ts +3 -0
  56. package/src/validate.test.ts +0 -2
  57. package/src/validateNode.ts +2 -2
  58. package/src/validateSchema.test.ts +312 -0
package/CHANGELOG.md CHANGED
@@ -1,8 +1,19 @@
1
1
  ## Changelog
2
2
 
3
+ ### v11.1.0
4
+
5
+ - introduced upcoming keyword `propertyDependenciesKeyword` to package export
6
+ - added support for keyword `deprecatedMessage`
7
+ - introduced schema validation
8
+
9
+ ### v11.0.4, v11.0.5
10
+
11
+ - removed enforcing packageManager as this failed build-jobs
12
+
3
13
  ### v11.0.3
4
14
 
5
15
  - fixed type of main input-schema to support boolean
16
+ - enforced yarn as a package manager
6
17
 
7
18
  ### v11.0.2
8
19
 
package/README.md CHANGED
@@ -94,6 +94,79 @@ compileSchema(mySchema, { getDataDefaultOptions: { addOptionalProps: true } });
94
94
  Details on _drafts_ are documented in [draft customization](#draft-customization).
95
95
  Details on `getDataDefaultOptions` are documented in [getData](#getData).
96
96
 
97
+ ### validate input schema
98
+
99
+ All JSON Schema passed to `compileSchema` are validated automatically. To retrieve any schema errors you can access the property `schemaErrors` of the main node:
100
+
101
+ ```ts
102
+ const root = compileSchema(mySchema);
103
+ const { schemaErrors } = root; // JsonError[]
104
+ ```
105
+
106
+ Use the option `throwOnInvalidSchema:true` of `compileSchema` to throw an Error for a input schema containing errors:
107
+
108
+ ```ts
109
+ const root = compileSchema({ properties: 123 }, { throwOnInvalidSchema: true });
110
+ // throws Error
111
+ ```
112
+
113
+ <details><summary>Example for validation errors</summary>
114
+
115
+ ---
116
+
117
+ ```ts
118
+ const { schemaErrors } = compileSchema({ $defs: 999 });
119
+ console.log(schemaErrors[0]);
120
+ {
121
+ type: 'error',
122
+ code: 'schema-error',
123
+ message: 'Invalid schema found at #: $defs must be an object - received: number',
124
+ data: {
125
+ pointer: '#',
126
+ schema: { '$defs': 999 },
127
+ value: 999,
128
+ message: '$defs must be an object - received: number'
129
+ }
130
+ }
131
+ ```
132
+
133
+ ---
134
+
135
+ </details>
136
+
137
+ To collect JSON Schema annotations for unused keywords you can opt in with option `withSchemaAnnotations`:
138
+
139
+ ```ts
140
+ const root = compileSchema(mySchema, { withSchemaAnnotations: true });
141
+ const { schemaAnnotations } = root; // JsonAnnotation[]
142
+ ```
143
+
144
+ 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.
145
+
146
+ <details><summary>Example for validation annotations</summary>
147
+
148
+ ---
149
+
150
+ ```ts
151
+ const { schemaErrors } = compileSchema({ options: {} }, { withSchemaAnnotations: true });
152
+ console.log(schemaErrors[0]);
153
+ {
154
+ type: 'annotation',
155
+ code: 'unknown-keyword-warning',
156
+ message: "Keyword 'options' is not a valid keyword to draft 'draft-2020-12'",
157
+ data: {
158
+ pointer: '#/options',
159
+ schema: { options: {} },
160
+ value: 'options',
161
+ draft: 'draft-2020-12'
162
+ }
163
+ }
164
+ ```
165
+
166
+ ---
167
+
168
+ </details>
169
+
97
170
  ### SchemaNode
98
171
 
99
172
  `compileSchema` builds a tree where each sub-schema becomes its own SchemaNode. Every node in the tree offers the same set of methods.
@@ -1379,6 +1452,45 @@ const dependentSchemasKeyword = draft2020.keywords.find((f) => f.keyword === "de
1379
1452
 
1380
1453
  ## Keyword extensions
1381
1454
 
1455
+ ### propertyDependencies
1456
+
1457
+ Resolves an object-schema by `propertyName:propertyValue`
1458
+
1459
+ ```ts
1460
+ {
1461
+ type: "object",
1462
+ propertyDependencies: {
1463
+ propertyName: {
1464
+ propertyValue: {
1465
+ properties: {
1466
+ id: { type: "string" }
1467
+ }
1468
+ }
1469
+ }
1470
+ }
1471
+
1472
+ // matches and returns error for id
1473
+ {
1474
+ "propertyName": "propertyValue",
1475
+ "id": 123
1476
+ }
1477
+ ```
1478
+
1479
+ Note that this keyword is not added by default as it is not part yet of the JSONSchema spec. Add this keyword with:
1480
+
1481
+ ```ts
1482
+ import { compileSchema, draft2020, extendDraft, propertyDependenciesKeyword } from "json-schema-library";
1483
+
1484
+ const draft = extendDraft(draft2020, {
1485
+ keywords: [propertyDependenciesKeyword]
1486
+ });
1487
+
1488
+ const node = compileSchema({ propertyDependencies: {} }, { drafts: [draft] });
1489
+ ```
1490
+
1491
+ - Note: this keyword may replace `OneOfProperty`
1492
+ - Reference: https://docs.google.com/presentation/d/1ajXlCQcsjjiMLsluFIILR7sN5aDRBnfqQ9DLbcFbqjI/mobilepresent?slide=id.g3ae4fb2e16d_0_15
1493
+
1382
1494
  ### oneOfProperty
1383
1495
 
1384
1496
  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.