json-schema-library 9.3.3 → 9.3.5

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.
@@ -7,7 +7,7 @@ import settings from "./config/settings";
7
7
  import { isJsonError } from "./types";
8
8
  import { isEmpty } from "./utils/isEmpty";
9
9
  import { resolveIfSchema } from "./features/if";
10
- import { mergeAllOfSchema } from "./features/allOf";
10
+ import { mergeAllOfSchema, resolveSchema } from "./features/allOf";
11
11
  import { resolveDependencies } from "./features/dependencies";
12
12
  import { mergeSchema } from "./mergeSchema";
13
13
  const defaultOptions = settings.templateDefaultOptions;
@@ -57,7 +57,7 @@ function convertValue(type, value) {
57
57
  * @param pointer
58
58
  * @return resolved json-schema or input-schema
59
59
  */
60
- function createTemplateSchema(draft, schema, data, pointer) {
60
+ function createTemplateSchema(draft, schema, data, pointer, opts) {
61
61
  // invalid schema
62
62
  if (getTypeOf(schema) !== "object") {
63
63
  return Object.assign({ pointer }, schema);
@@ -85,7 +85,16 @@ function createTemplateSchema(draft, schema, data, pointer) {
85
85
  .map((allOf, index) => shouldResolveRef(allOf, `${pointer}/allOf/${index}`))
86
86
  .reduceRight((next, before) => next && before, true);
87
87
  if (mayResolve) {
88
- const resolvedSchema = mergeAllOfSchema(draft, schema);
88
+ // before merging all-of, we need to resolve all if-then-else statesments
89
+ // we need to udpate data on the way to trigger if-then-else schemas sequentially.
90
+ // Note that this will make if-then-else order-dependent
91
+ const allOf = [];
92
+ let extendedData = copy(data);
93
+ for (let i = 0; i < schema.allOf.length; i += 1) {
94
+ allOf.push(resolveSchema(draft, schema.allOf[i], extendedData));
95
+ extendedData = getTemplate(draft, extendedData, { type: schema.type, ...allOf[i] }, `${pointer}/allOf/${i}`, opts);
96
+ }
97
+ const resolvedSchema = mergeAllOfSchema(draft, { allOf });
89
98
  if (resolvedSchema) {
90
99
  templateSchema = mergeSchema(templateSchema, resolvedSchema);
91
100
  }
@@ -112,7 +121,7 @@ function getTemplate(draft, data, _schema, pointer, opts) {
112
121
  throw new Error("Missing pointer");
113
122
  }
114
123
  // resolve $ref references, allOf and first anyOf definitions
115
- let schema = createTemplateSchema(draft, _schema, data, pointer);
124
+ let schema = createTemplateSchema(draft, _schema, data, pointer, opts);
116
125
  if (!isJsonSchema(schema)) {
117
126
  return undefined;
118
127
  }
@@ -273,7 +282,7 @@ const TYPE = {
273
282
  return d;
274
283
  }
275
284
  // resolve allOf and first anyOf definition
276
- const templateSchema = createTemplateSchema(draft, schema.items, data, pointer);
285
+ const templateSchema = createTemplateSchema(draft, schema.items, data, pointer, opts);
277
286
  if (templateSchema === false) {
278
287
  return d;
279
288
  }
@@ -78,7 +78,13 @@ const stepType = {
78
78
  if (targetSchema && Array.isArray(targetSchema.oneOf)) {
79
79
  // @special case: this is a mix of a schema and optional definitions
80
80
  // we resolve the schema here and add the original schema to `oneOfSchema`
81
- return draft.resolveOneOf(data[key], targetSchema, `${pointer}/${key}`);
81
+ const resolvedSchema = draft.resolveOneOf(data[key], targetSchema, `${pointer}/${key}`);
82
+ for (const p in targetSchema) {
83
+ if (p !== "oneOf" && resolvedSchema[p] === undefined) {
84
+ resolvedSchema[p] = targetSchema[p];
85
+ }
86
+ }
87
+ return resolvedSchema;
82
88
  }
83
89
  // resolved schema or error
84
90
  if (targetSchema) {
@@ -12,7 +12,7 @@ import { resolveIfSchema } from "./if";
12
12
  * resolves schema
13
13
  * when complete this will have much duplication to step.object etc
14
14
  */
15
- function resolveSchema(draft: Draft, schemaToResolve: JsonSchema, data: unknown): JsonSchema {
15
+ export function resolveSchema(draft: Draft, schemaToResolve: JsonSchema, data: unknown): JsonSchema {
16
16
  const schema = { ...(draft.resolveRef(schemaToResolve) ?? {}) };
17
17
  const ifSchema = resolveIfSchema(draft, schema, data);
18
18
  if (ifSchema) {
@@ -8,7 +8,7 @@ import { JsonSchema, JsonPointer, isJsonError } from "./types";
8
8
  import { Draft } from "./draft";
9
9
  import { isEmpty } from "./utils/isEmpty";
10
10
  import { resolveIfSchema } from "./features/if";
11
- import { mergeAllOfSchema } from "./features/allOf";
11
+ import { mergeAllOfSchema, resolveSchema } from "./features/allOf";
12
12
  import { resolveDependencies } from "./features/dependencies";
13
13
  import { mergeSchema } from "./mergeSchema";
14
14
 
@@ -78,7 +78,8 @@ function createTemplateSchema(
78
78
  draft: Draft,
79
79
  schema: JsonSchema,
80
80
  data: unknown,
81
- pointer: JsonPointer
81
+ pointer: JsonPointer,
82
+ opts: TemplateOptions
82
83
  ): JsonSchema | false {
83
84
  // invalid schema
84
85
  if (getTypeOf(schema) !== "object") {
@@ -111,7 +112,17 @@ function createTemplateSchema(
111
112
  .reduceRight((next, before) => next && before, true);
112
113
 
113
114
  if (mayResolve) {
114
- const resolvedSchema = mergeAllOfSchema(draft, schema);
115
+ // before merging all-of, we need to resolve all if-then-else statesments
116
+ // we need to udpate data on the way to trigger if-then-else schemas sequentially.
117
+ // Note that this will make if-then-else order-dependent
118
+ const allOf = [];
119
+ let extendedData = copy(data);
120
+ for (let i = 0; i < schema.allOf.length; i += 1) {
121
+ allOf.push(resolveSchema(draft, schema.allOf[i], extendedData));
122
+ extendedData = getTemplate(draft, extendedData, { type: schema.type, ...allOf[i] }, `${pointer}/allOf/${i}`, opts);
123
+ }
124
+
125
+ const resolvedSchema = mergeAllOfSchema(draft, { allOf });
115
126
  if (resolvedSchema) {
116
127
  templateSchema = mergeSchema(templateSchema, resolvedSchema);
117
128
  }
@@ -148,7 +159,7 @@ function getTemplate(
148
159
  }
149
160
 
150
161
  // resolve $ref references, allOf and first anyOf definitions
151
- let schema = createTemplateSchema(draft, _schema, data, pointer);
162
+ let schema = createTemplateSchema(draft, _schema, data, pointer, opts);
152
163
  if (!isJsonSchema(schema)) {
153
164
  return undefined;
154
165
  }
@@ -376,7 +387,7 @@ const TYPE: Record<
376
387
  }
377
388
 
378
389
  // resolve allOf and first anyOf definition
379
- const templateSchema = createTemplateSchema(draft, schema.items, data, pointer);
390
+ const templateSchema = createTemplateSchema(draft, schema.items, data, pointer, opts);
380
391
  if (templateSchema === false) {
381
392
  return d;
382
393
  }
package/lib/step.ts CHANGED
@@ -108,7 +108,17 @@ const stepType: Record<string, StepFunction> = {
108
108
  if (targetSchema && Array.isArray(targetSchema.oneOf)) {
109
109
  // @special case: this is a mix of a schema and optional definitions
110
110
  // we resolve the schema here and add the original schema to `oneOfSchema`
111
- return draft.resolveOneOf(data[key], targetSchema, `${pointer}/${key}`);
111
+ const resolvedSchema = draft.resolveOneOf(
112
+ data[key],
113
+ targetSchema,
114
+ `${pointer}/${key}`
115
+ );
116
+ for (const p in targetSchema) {
117
+ if (p !== "oneOf" && resolvedSchema[p] === undefined) {
118
+ resolvedSchema[p] = targetSchema[p];
119
+ }
120
+ }
121
+ return resolvedSchema;
112
122
  }
113
123
 
114
124
  // resolved schema or error
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "json-schema-library",
3
- "version": "9.3.3",
3
+ "version": "9.3.5",
4
4
  "description": "Customizable and hackable json-validator and json-schema utilities for traversal, data generation and validation",
5
5
  "module": "dist/module/index.js",
6
6
  "types": "dist/index.d.ts",