Skip to content

Commit f92bbdb

Browse files
committed
publish: bump version to v10.2.0
1 parent b3f427d commit f92bbdb

File tree

8 files changed

+55
-31
lines changed

8 files changed

+55
-31
lines changed

dist/jsonSchemaLibrary.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/module/src/draft2019-09/methods/getData.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -137,11 +137,11 @@ export function getData(node, data, opts) {
137137
return templateData === undefined ? defaultData : templateData;
138138
}
139139
const TYPE = {
140-
null: (node, data) => getDefault(node, data, null),
141-
string: (node, data) => getDefault(node, data, ""),
142-
number: (node, data) => getDefault(node, data, 0),
143-
integer: (node, data) => getDefault(node, data, 0),
144-
boolean: (node, data) => getDefault(node, data, false),
140+
null: (node, data, opts) => getDefault(node, data, null, opts.useTypeDefaults),
141+
string: (node, data, opts) => getDefault(node, data, "", opts.useTypeDefaults),
142+
number: (node, data, opts) => getDefault(node, data, 0, opts.useTypeDefaults),
143+
integer: (node, data, opts) => getDefault(node, data, 0, opts.useTypeDefaults),
144+
boolean: (node, data, opts) => getDefault(node, data, false, opts.useTypeDefaults),
145145
// object: (draft, schema, data: Record<string, unknown> | undefined, pointer: JsonPointer, opts: TemplateOptions) => {
146146
object: (node, data, opts) => {
147147
var _a;
@@ -157,7 +157,10 @@ const TYPE = {
157157
const value = data === undefined || input === undefined ? getValue(template, propertyName) : input;
158158
// Omit adding a property if it is not required or optional props should be added
159159
if (value != null || isRequired || opts.addOptionalProps) {
160-
d[propertyName] = propertyNode.getData(value, opts);
160+
const propertyValue = propertyNode.getData(value, opts);
161+
if (propertyValue !== undefined || opts.useTypeDefaults !== false) {
162+
d[propertyName] = propertyValue;
163+
}
161164
}
162165
});
163166
}
@@ -282,7 +285,7 @@ const TYPE = {
282285
return d;
283286
}
284287
};
285-
function getDefault({ schema }, templateValue, initValue) {
288+
function getDefault({ schema }, templateValue, initValue, useTypeDefaults) {
286289
if (templateValue !== undefined) {
287290
return convertValue(schema.type, templateValue);
288291
}
@@ -292,7 +295,7 @@ function getDefault({ schema }, templateValue, initValue) {
292295
else if (schema.default === undefined && Array.isArray(schema.enum)) {
293296
return schema.enum[0];
294297
}
295-
else if (schema.default === undefined) {
298+
else if (schema.default === undefined && useTypeDefaults !== false) {
296299
return initValue;
297300
}
298301
return schema.default;

dist/module/src/keywords/$ref.js

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { isObject } from "../utils/isObject";
55
import { validateNode } from "../validateNode";
66
import { get, split } from "@sagold/json-pointer";
77
import { mergeNode } from "../mergeNode";
8+
import { pick } from "../utils/pick";
89
export const $refKeyword = {
910
id: "$ref",
1011
keyword: "$ref",
@@ -109,48 +110,53 @@ function resolveRecursiveRef(node, path) {
109110
const nonMatchingDynamicAnchor = node.context.dynamicAnchors[refInCurrentScope] == null;
110111
if (nonMatchingDynamicAnchor) {
111112
if (node.context.anchors[refInCurrentScope]) {
112-
return compileNext(node.context.anchors[refInCurrentScope], node.evaluationPath);
113+
return compileNext(node.context.anchors[refInCurrentScope], node);
113114
}
114115
}
115116
for (let i = 0; i < history.length; i += 1) {
116117
// A $dynamicRef that initially resolves to a schema with a matching $dynamicAnchor resolves to the first $dynamicAnchor in the dynamic scope
117118
if (history[i].node.schema.$dynamicAnchor) {
118-
return compileNext(history[i].node, node.evaluationPath);
119+
return compileNext(history[i].node, node);
119120
}
120121
// A $dynamicRef only stops at a $dynamicAnchor if it is in the same dynamic scope.
121122
const refWithoutScope = node.schema.$dynamicRef.split("#").pop();
122123
const ref = joinId(history[i].node.$id, `#${refWithoutScope}`);
123124
const anchorNode = node.context.dynamicAnchors[ref];
124125
if (anchorNode) {
125-
return compileNext(node.context.dynamicAnchors[ref], node.evaluationPath);
126+
return compileNext(node.context.dynamicAnchors[ref], node);
126127
}
127128
}
128129
// A $dynamicRef without a matching $dynamicAnchor in the same schema resource behaves like a normal $ref to $anchor
129130
const nextNode = getRef(node, refInCurrentScope);
130131
return nextNode;
131132
}
132-
function compileNext(referencedNode, evaluationPath = referencedNode.evaluationPath) {
133-
const referencedSchema = isObject(referencedNode.schema)
134-
? omit(referencedNode.schema, "$id")
135-
: referencedNode.schema;
136-
return referencedNode.compileSchema(referencedSchema, `${evaluationPath}/$ref`, referencedNode.schemaLocation);
133+
const PROPERTIES_TO_MERGE = ["title", "description", "options", "readOnly", "writeOnly"];
134+
function compileNext(referencedNode, sourceNode) {
135+
let referencedSchema = referencedNode.schema;
136+
if (isObject(referencedNode.schema)) {
137+
referencedSchema = {
138+
...omit(referencedNode.schema, "$id"),
139+
...pick(sourceNode.schema, ...PROPERTIES_TO_MERGE)
140+
};
141+
}
142+
return referencedNode.compileSchema(referencedSchema, `${sourceNode.evaluationPath}/$ref`, referencedNode.schemaLocation);
137143
}
138144
export function getRef(node, $ref = node === null || node === void 0 ? void 0 : node.$ref) {
139145
if ($ref == null) {
140146
return node;
141147
}
142148
// resolve $ref by json-evaluationPath
143149
if (node.context.refs[$ref]) {
144-
return compileNext(node.context.refs[$ref], node.evaluationPath);
150+
return compileNext(node.context.refs[$ref], node);
145151
}
146152
// resolve $ref from $anchor
147153
if (node.context.anchors[$ref]) {
148-
return compileNext(node.context.anchors[$ref], node.evaluationPath);
154+
return compileNext(node.context.anchors[$ref], node);
149155
}
150156
// resolve $ref from $dynamicAnchor
151157
if (node.context.dynamicAnchors[$ref]) {
152158
// A $ref to a $dynamicAnchor in the same schema resource behaves like a normal $ref to an $anchor
153-
return compileNext(node.context.dynamicAnchors[$ref], node.evaluationPath);
159+
return compileNext(node.context.dynamicAnchors[$ref], node);
154160
}
155161
// check for remote-host + pointer pair to switch rootSchema
156162
const fragments = splitRef($ref);
@@ -162,7 +168,7 @@ export function getRef(node, $ref = node === null || node === void 0 ? void 0 :
162168
const $ref = fragments[0];
163169
// this is a reference to remote-host root node
164170
if (node.context.remotes[$ref]) {
165-
return compileNext(node.context.remotes[$ref], node.evaluationPath);
171+
return compileNext(node.context.remotes[$ref], node);
166172
}
167173
if ($ref[0] === "#") {
168174
// support refOfUnknownKeyword

dist/module/src/methods/getData.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -129,11 +129,11 @@ export function getData(node, data, opts) {
129129
return templateData === undefined ? defaultData : templateData;
130130
}
131131
const TYPE = {
132-
null: (node, data) => getDefault(node, data, null),
133-
string: (node, data) => getDefault(node, data, ""),
134-
number: (node, data) => getDefault(node, data, 0),
135-
integer: (node, data) => getDefault(node, data, 0),
136-
boolean: (node, data) => getDefault(node, data, false),
132+
null: (node, data, opts) => getDefault(node, data, null, opts.useTypeDefaults),
133+
string: (node, data, opts) => getDefault(node, data, "", opts.useTypeDefaults),
134+
number: (node, data, opts) => getDefault(node, data, 0, opts.useTypeDefaults),
135+
integer: (node, data, opts) => getDefault(node, data, 0, opts.useTypeDefaults),
136+
boolean: (node, data, opts) => getDefault(node, data, false, opts.useTypeDefaults),
137137
// object: (draft, schema, data: Record<string, unknown> | undefined, pointer: JsonPointer, opts: TemplateOptions) => {
138138
object: (node, data, opts) => {
139139
var _a;
@@ -149,7 +149,10 @@ const TYPE = {
149149
const value = data === undefined || input === undefined ? getValue(template, propertyName) : input;
150150
// Omit adding a property if it is not required or optional props should be added
151151
if (value != null || isRequired || opts.addOptionalProps) {
152-
d[propertyName] = propertyNode.getData(value, opts);
152+
const propertyValue = propertyNode.getData(value, opts);
153+
if (propertyValue !== undefined || opts.useTypeDefaults !== false) {
154+
d[propertyName] = propertyValue;
155+
}
153156
}
154157
});
155158
}
@@ -279,7 +282,7 @@ const TYPE = {
279282
return d;
280283
}
281284
};
282-
function getDefault({ schema }, templateValue, initValue) {
285+
function getDefault({ schema }, templateValue, initValue, useTypeDefaults) {
283286
if (templateValue !== undefined) {
284287
return convertValue(schema.type, templateValue);
285288
}
@@ -289,7 +292,7 @@ function getDefault({ schema }, templateValue, initValue) {
289292
else if (schema.default === undefined && Array.isArray(schema.enum)) {
290293
return schema.enum[0];
291294
}
292-
else if (schema.default === undefined) {
295+
else if (schema.default === undefined && useTypeDefaults !== false) {
293296
return initValue;
294297
}
295298
return schema.default;

dist/module/src/utils/pick.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ export function pick(value, ...properties) {
44
return value;
55
}
66
const result = {};
7-
properties.forEach((property) => (result[property] = value[property]));
7+
properties.forEach((property) => {
8+
if (value[property] !== undefined) {
9+
result[property] = value[property];
10+
}
11+
});
812
return result;
913
}

dist/src/draft2019-09/methods/getData.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ export type TemplateOptions = {
1010
* regardless of minItems settings.
1111
*/
1212
extendDefaults?: boolean;
13+
/**
14+
* Set to false to not use type specific initial values.Defaults to true
15+
*/
16+
useTypeDefaults?: boolean;
1317
/**
1418
* Limits how often a $ref should be followed before aborting. Prevents infinite data-structure.
1519
* Defaults to 1

dist/src/methods/getData.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ export type TemplateOptions = {
1010
* regardless of minItems settings.
1111
*/
1212
extendDefaults?: boolean;
13+
/**
14+
* Set to false to not use type specific initial values.Defaults to true
15+
*/
16+
useTypeDefaults?: boolean;
1317
/**
1418
* Limits how often a $ref should be followed before aborting. Prevents infinite data-structure.
1519
* Defaults to 1

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "json-schema-library",
3-
"version": "10.1.2",
3+
"version": "10.2.0",
44
"description": "Customizable and hackable json-validator and json-schema utilities for traversal, data generation and validation",
55
"module": "dist/module/index.js",
66
"types": "dist/index.d.ts",

0 commit comments

Comments
 (0)