Skip to content

Commit d16c68f

Browse files
committed
dist: update
1 parent 5640e2d commit d16c68f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+671
-273
lines changed

Diff for: dist/index.d.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1+
export { compileSchema } from "./src/compileSchema";
12
export type { CompileOptions } from "./src/compileSchema";
23
export type { Context, SchemaNode, GetSchemaOptions } from "./src/SchemaNode";
34
export type { DataNode } from "./src/methods/toDataNodes";
45
export type { Draft, DraftVersion } from "./src/Draft";
56
export type { JsonError, JsonPointer, JsonSchema, OptionalNodeAndError } from "./src/types";
67
export type { Keyword, ValidationPath, JsonSchemaReducerParams, JsonSchemaReducer, JsonSchemaResolverParams, JsonSchemaResolver, JsonSchemaValidatorParams, JsonSchemaValidator } from "./src/Keyword";
7-
export { compileSchema } from "./src/compileSchema";
88
export { draft04 } from "./src/draft04";
99
export { draft06 } from "./src/draft06";
1010
export { draft07 } from "./src/draft07";
@@ -14,7 +14,7 @@ export { oneOfFuzzyKeyword, oneOfKeyword } from "./src/keywords/oneOf";
1414
export { render } from "./src/errors/render";
1515
export type { ErrorData, ErrorConfig } from "./src/types";
1616
export { dashCase } from "./src/utils/dashCase";
17-
export { default as getTypeOf } from "./src/utils/getTypeOf";
17+
export { getTypeOf } from "./src/utils/getTypeOf";
1818
export { isReduceable } from "./src/SchemaNode";
1919
export { isJsonError, isSchemaNode } from "./src/types";
2020
export { extendDraft, addKeywords } from "./src/Draft";

Diff for: dist/jsonSchemaLibrary.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: dist/module/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export { oneOfFuzzyKeyword, oneOfKeyword } from "./src/keywords/oneOf";
1111
export { render } from "./src/errors/render";
1212
export { dashCase } from "./src/utils/dashCase";
1313
// utilities
14-
export { default as getTypeOf } from "./src/utils/getTypeOf";
14+
export { getTypeOf } from "./src/utils/getTypeOf";
1515
export { isReduceable } from "./src/SchemaNode";
1616
export { isJsonError, isSchemaNode } from "./src/types";
1717
export { extendDraft, addKeywords } from "./src/Draft";

Diff for: dist/module/src/SchemaNode.js

+58-13
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,35 @@ function getDraft(drafts, $schema) {
3131
var _a;
3232
return (_a = drafts.find((d) => new RegExp(d.$schemaRegEx).test($schema))) !== null && _a !== void 0 ? _a : drafts[drafts.length - 1];
3333
}
34+
export function joinDynamicId(a, b) {
35+
if (a == b) {
36+
return a !== null && a !== void 0 ? a : "";
37+
}
38+
if (a == null || b == null) {
39+
return a || b;
40+
}
41+
if (a.startsWith(b)) {
42+
return a;
43+
}
44+
if (b.startsWith(a)) {
45+
return b;
46+
}
47+
return `${a}+${b}`;
48+
}
3449
export const SchemaNodeMethods = {
35-
/** Compiles a child-schema of this node to its context */
36-
compileSchema(schema, spointer = this.spointer, schemaId) {
50+
/**
51+
* Compiles a child-schema of this node to its context
52+
* @returns SchemaNode representing the passed JSON Schema
53+
*/
54+
compileSchema(schema, spointer = this.spointer, schemaId, dynamicId) {
3755
const nextFragment = spointer.split("/$ref")[0];
3856
const parentNode = this;
3957
const node = {
4058
lastIdPointer: parentNode.lastIdPointer, // ref helper
4159
context: parentNode.context,
4260
parent: parentNode,
4361
spointer,
62+
dynamicId: joinDynamicId(parentNode.dynamicId, dynamicId),
4463
schemaId: schemaId !== null && schemaId !== void 0 ? schemaId : join(parentNode.schemaId, nextFragment),
4564
reducers: [],
4665
resolvers: [],
@@ -60,17 +79,17 @@ export const SchemaNodeMethods = {
6079
}
6180
errorMessage = render(error !== null && error !== void 0 ? error : name, data);
6281
}
63-
return { type: "error", name, code: dashCase(name), message: errorMessage, data };
82+
return { type: "error", code: dashCase(name), message: errorMessage, data };
6483
},
6584
createSchema,
6685
getChildSchemaSelection(property) {
6786
const node = this;
6887
return node.context.methods.getChildSchemaSelection(node, property);
6988
},
7089
/**
71-
* Returns a node containing json-schema of a data-json-pointer.
90+
* Returns a node containing JSON Schema of a data-JSON Pointer.
7291
*
73-
* To resolve dynamic schema where the type of json-schema is evaluated by
92+
* To resolve dynamic schema where the type of JSON Schema is evaluated by
7493
* its value, a data object has to be passed in options.
7594
*
7695
* Per default this function will return `undefined` schema for valid properties
@@ -115,10 +134,16 @@ export const SchemaNodeMethods = {
115134
const result = currentNode.resolveRef(options);
116135
return isJsonError(result) ? { node: undefined, error: result } : { node: result, error: undefined };
117136
},
137+
/**
138+
* @returns for $ref, the corresponding SchemaNode or undefined
139+
*/
118140
getRef($ref) {
119141
const node = this;
120142
return node.compileSchema({ $ref }).resolveRef();
121143
},
144+
/**
145+
* @returns child node identified by property as SchemaNode
146+
*/
122147
getChild(key, data, options = {}) {
123148
var _a, _b, _c;
124149
options.path = (_a = options.path) !== null && _a !== void 0 ? _a : [];
@@ -158,10 +183,15 @@ export const SchemaNodeMethods = {
158183
}
159184
return { node: undefined, error: undefined };
160185
},
186+
/**
187+
* @returns draft version this JSON Schema is evaluated by
188+
*/
161189
getDraftVersion() {
162190
return this.context.version;
163191
},
164-
/** Creates data that is valid to the schema of this node */
192+
/**
193+
* @returns data that is valid to the schema of this node
194+
*/
165195
getData(data, options) {
166196
const node = this;
167197
const opts = {
@@ -172,10 +202,12 @@ export const SchemaNodeMethods = {
172202
};
173203
return node.context.methods.getData(node, data, opts);
174204
},
205+
/**
206+
* @returns SchemaNode with a reduced JSON Schema matching the given data
207+
*/
175208
reduceSchema(data, options = {}) {
209+
const node = this;
176210
const { key, pointer, path } = options;
177-
const resolvedNode = { ...this.resolveRef({ pointer, path }) };
178-
const node = mergeNode(this, resolvedNode, "$ref");
179211
// @ts-expect-error bool schema
180212
if (node.schema === false) {
181213
return { node, error: undefined };
@@ -187,10 +219,12 @@ export const SchemaNodeMethods = {
187219
return { node: nextNode, error: undefined };
188220
}
189221
let schema;
190-
let workingNode = node;
222+
// we need to copy node to prevent modification of source
223+
// @todo does mergeNode break immutability?
224+
let workingNode = node.compileSchema(node.schema, node.spointer, node.schemaId);
191225
const reducers = node.reducers;
192226
for (let i = 0; i < reducers.length; i += 1) {
193-
const result = reducers[i]({ data, key, node, pointer });
227+
const result = reducers[i]({ data, key, node, pointer, path });
194228
if (isJsonError(result)) {
195229
return { node: undefined, error: result };
196230
}
@@ -206,7 +240,6 @@ export const SchemaNodeMethods = {
206240
}
207241
}
208242
if (schema === false) {
209-
console.log("return boolean schema `false`");
210243
// @ts-expect-error bool schema
211244
return { node: { ...node, schema: false, reducers: [] }, error: undefined };
212245
}
@@ -219,7 +252,9 @@ export const SchemaNodeMethods = {
219252
DYNAMIC_PROPERTIES.forEach((prop) => (workingNode[prop] = undefined));
220253
return { node: workingNode, error: undefined };
221254
},
222-
/** Creates a new node with all dynamic schema properties merged according to the passed in data */
255+
/**
256+
* @returns validation result of data validated by this node's JSON Schema
257+
*/
223258
validate(data, pointer = "#", path = []) {
224259
var _a;
225260
const errors = (_a = validateNode(this, data, pointer, path)) !== null && _a !== void 0 ? _a : [];
@@ -229,6 +264,9 @@ export const SchemaNodeMethods = {
229264
errors: flatErrorList
230265
};
231266
},
267+
/**
268+
* @returns a promise which resolves to validation-result
269+
*/
232270
async validateAsync(data, pointer = "#", path = []) {
233271
var _a;
234272
const errors = (_a = validateNode(this, data, pointer, path)) !== null && _a !== void 0 ? _a : [];
@@ -240,7 +278,7 @@ export const SchemaNodeMethods = {
240278
};
241279
},
242280
/**
243-
* Register a json-schema as a remote-schema to be resolved by $ref, $anchor, etc
281+
* Register a JSON Schema as a remote-schema to be resolved by $ref, $anchor, etc
244282
* @returns the current node (not the remote schema-node)
245283
*/
246284
addRemote(url, schema) {
@@ -253,6 +291,7 @@ export const SchemaNodeMethods = {
253291
spointer: "#",
254292
lastIdPointer: "#",
255293
schemaId: "#",
294+
dynamicId: "",
256295
reducers: [],
257296
resolvers: [],
258297
validators: [],
@@ -270,9 +309,15 @@ export const SchemaNodeMethods = {
270309
addKeywords(node);
271310
return this;
272311
},
312+
/**
313+
* @returns a list of all sub-schema as SchemaNode
314+
*/
273315
toSchemaNodes() {
274316
return toSchemaNodes(this);
275317
},
318+
/**
319+
* @returns a list of values (including objects and arrays) and their corresponding JSON Schema as SchemaNode
320+
*/
276321
toDataNodes(data, pointer) {
277322
const node = this;
278323
return node.context.methods.toDataNodes(node, data, pointer);

0 commit comments

Comments
 (0)