Skip to content

Commit 444901e

Browse files
committed
change: rename reduce to reduceSchema
1 parent 03cdce0 commit 444901e

15 files changed

+106
-71
lines changed

README.md

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ Please note that these benchmarks refer to validation only. _json-schema-library
163163

164164
## SchemaNode methods
165165

166-
[**validate**](#validate) · [**validateAsync**](#validateasync) · [**getData**](#getData) · [**getSchema**](#getschema) · [**getChild**](#getchild) · [**reduce**](#reduce) · [**toDataNodes**](#todatanodes) · [**toSchemaNodes**](#toschemanodes) · [**addRemote**](#addremote) · [**compileSchema**](#compileSchema-1) · [createSchema](#createSchema) · [getChildSchemaSelection](#getchildschemaselection)
166+
[**validate**](#validate) · [**validateAsync**](#validateasync) · [**getData**](#getdata) · [**getSchema**](#getschema) · [**getChild**](#getchild) · [**reduceSchema**](#reduceschema) · [**toDataNodes**](#todatanodes) · [**toSchemaNodes**](#toschemanodes) · [**addRemote**](#addremote) · [**compileSchema**](#compileSchema-1) · [createSchema](#createSchema) · [getChildSchemaSelection](#getchildschemaselection)
167167

168168
### validate
169169

@@ -548,9 +548,38 @@ expect(schema).to.deep.eq({ type: "number" });
548548

549549
</details>
550550

551-
### reduce
551+
### reduceSchema
552552

553-
`reduce` compiles dynamic schema-keywords of a SchemaNode according to the given data.
553+
`reduceSchema` compiles dynamic JSON schema keywords of a SchemaNode according to the given data.
554+
This utility helps walking down the schema-tree with a set of data and it helps getting a mostly
555+
complete json-schema for a specific data-value.
556+
557+
```ts
558+
const reducedNode = compileSchema({
559+
properties: {
560+
trigger: { type: "boolean"}
561+
}
562+
dependentSchemas: {
563+
trigger: {
564+
required: ["title"],
565+
properties: {
566+
title: { type: "string" }
567+
}
568+
}
569+
}
570+
}).reduceSchema({ trigger: true });
571+
572+
expect(reducedNode.schema).to.deep.eq({
573+
required: ["title"],
574+
properties: {
575+
trigger: { type: "boolean"},
576+
title: { type: "string" }
577+
}
578+
});
579+
```
580+
581+
⚠️ Please be aware that certain schema-definitions are lost when resolving or merging sub-schemas.
582+
This mainly refers to validation-properties, but also some ambigiuous schema might get overriden.
554583

555584
### toDataNodes
556585

@@ -1052,8 +1081,17 @@ expect(resolvedNode?.schema).to.deep.eq({
10521081

10531082
## Breaking Changes
10541083

1055-
- `each(data, callback)` has been replaced by `const nodes = toDataNodes(data)`
1056-
- `eachSchema(callback)` has been replaced by `const nodes = toSchemaNodes()`
1084+
- `compileSchema` is a standalone function which replaces `Draft`-Class
1085+
- all return values for JSON Schema are `SchemaNode` that contain a schema-property
1086+
- `draft.getTemplate(inputData)` has been **renamed** to `node.getData(inputData)`
1087+
- `draft.each(data, callback)` has been **replaced** by `const nodes = node.toDataNodes(data)`
1088+
- `draft.eachSchema(callback)` has been **replaced** by `const nodes = node.toSchemaNodes()`
1089+
- `draft.isValid(data)` has been **replaced** by `node.validate(data).valid`
1090+
- `draft.getSchema(options)` has been **changed** to `node.getSchema(pointer, data, options)`
1091+
- `draft.step(property, data)` has been **renamed** to `node.getChild(property, data)`
1092+
- `draft.addRemoteSchema(schema)` has been **renamed** to `node.addRemote(schema)`
1093+
- `draft.createSchemaOf(schema)` has been **renamed** to `node.createSchema(schema)`
1094+
- draft customization has completely changed
10571095

10581096
### v10.0.0
10591097

src/SchemaNode.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ export const SchemaNodeMethods = {
230230

231231
let node = this as SchemaNode;
232232
if (node.reducers.length) {
233-
const result = node.reduce(data, { key, path, pointer });
233+
const result = node.reduceSchema(data, { key, path, pointer });
234234
if (result.error) {
235235
return result;
236236
}
@@ -287,16 +287,13 @@ export const SchemaNodeMethods = {
287287
return node.context.methods.getData(node, data, opts);
288288
},
289289

290-
reduce(
290+
reduceSchema(
291291
data: unknown,
292292
options: { key?: string | number; pointer?: string; path?: ValidationPath } = {}
293293
): OptionalNodeAndError {
294294
const { key, pointer, path } = options;
295295
const resolvedNode = { ...this.resolveRef({ pointer, path }) } as SchemaNode;
296-
// const resolvedSchema = mergeSchema(this.schema, resolvedNode?.schema);
297-
// const node = (this as SchemaNode).compileSchema(resolvedSchema, this.spointer, resolvedSchema.schemaId);
298296
const node = mergeNode(this, resolvedNode, "$ref");
299-
// const node = resolvedNode;
300297

301298
// @ts-expect-error bool schema
302299
if (node.schema === false) {

src/compileSchema.getChild.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ describe("compileSchema : getChild", () => {
277277
}
278278
}).getChild("dynamicSchema");
279279

280-
const { node } = res.reduce({ trigger: true });
280+
const { node } = res.reduceSchema({ trigger: true });
281281

282282
assert.deepEqual(node.schema, {
283283
type: "object",
@@ -624,7 +624,7 @@ describe("step", () => {
624624
}
625625
}).getChild(0, [{ title: 2 }]);
626626

627-
const { node } = res.reduce({ title: 2 });
627+
const { node } = res.reduceSchema({ title: 2 });
628628

629629
assert.deepEqual(node.schema, {
630630
type: "object",
@@ -643,7 +643,7 @@ describe("step", () => {
643643
}
644644
}).getChild(1, [{ title: "two" }, { title: 4 }]);
645645

646-
const { node } = res.reduce({ title: 4 });
646+
const { node } = res.reduceSchema({ title: 4 });
647647

648648
assert.deepEqual(node.schema, { type: "object", properties: { title: { type: "number" } } });
649649
});
@@ -660,7 +660,7 @@ describe("step", () => {
660660
}
661661
}).getChild(1, [{ title: "two" }, { title: 4 }]);
662662

663-
const { node } = res.reduce({ title: 4 });
663+
const { node } = res.reduceSchema({ title: 4 });
664664

665665
assert.deepEqual(node.schema, { type: "object", properties: { title: { type: "number", minimum: 2 } } });
666666
});
@@ -676,7 +676,7 @@ describe("step", () => {
676676
}
677677
}).getChild(1, [{ title: "two" }, { title: 4 }]);
678678

679-
const { node } = res.reduce({ title: "two" });
679+
const { node } = res.reduceSchema({ title: "two" });
680680

681681
assert.deepEqual(node.schema, {
682682
type: "object",

0 commit comments

Comments
 (0)