Skip to content

Commit

Permalink
remove: support for getOneOfOrigin
Browse files Browse the repository at this point in the history
  • Loading branch information
sagold committed Apr 17, 2024
1 parent eadfb7c commit 773ace8
Show file tree
Hide file tree
Showing 14 changed files with 13 additions and 139 deletions.
2 changes: 1 addition & 1 deletion dist/jsonSchemaLibrary.js

Large diffs are not rendered by default.

4 changes: 0 additions & 4 deletions dist/lib/schema/createOneOfSchemaResult.d.ts

This file was deleted.

16 changes: 5 additions & 11 deletions dist/module/lib/features/oneOf.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
*/
import flattenArray from "../utils/flattenArray";
import settings from "../config/settings";
import { createOneOfSchemaResult } from "../schema/createOneOfSchemaResult";
import { errorOrPromise } from "../utils/filter";
import { isJsonError } from "../types";
import { isObject } from "../utils/isObject";
Expand Down Expand Up @@ -49,8 +48,7 @@ export function resolveOneOf(node, data) {
errors.push(...result);
}
else {
const nextSchema = createOneOfSchemaResult(schema, oneNode.schema, i); // return resolved schema
return resultNode.next(nextSchema);
return resultNode.next(oneNode.schema);
}
}
return draft.errors.oneOfPropertyError({
Expand All @@ -75,8 +73,7 @@ export function resolveOneOf(node, data) {
}
}
if (matches.length === 1) {
const nextSchema = createOneOfSchemaResult(schema, matches[0].schema, matches[0].index); // return resolved schema
return node.next(nextSchema);
return node.next(matches[0].schema);
}
if (matches.length > 1) {
return draft.errors.multipleOneOfError({
Expand Down Expand Up @@ -165,8 +162,7 @@ export function resolveOneOfFuzzy(node, data) {
errors.push(...result);
}
else {
const nextSchema = createOneOfSchemaResult(schema, oneNode.schema, i);
return resultNode.next(nextSchema);
return resultNode.next(oneNode.schema);
}
}
return draft.errors.oneOfPropertyError({
Expand All @@ -187,8 +183,7 @@ export function resolveOneOfFuzzy(node, data) {
}
}
if (matches.length === 1) {
const nextSchema = createOneOfSchemaResult(schema, matches[0].schema, matches[0].index);
return node.next(nextSchema);
return node.next(matches[0].schema);
}
// fuzzy match oneOf
if (isObject(data)) {
Expand All @@ -212,8 +207,7 @@ export function resolveOneOfFuzzy(node, data) {
oneOf: schema.oneOf
});
}
const nextSchema = createOneOfSchemaResult(schema, schemaOfItem, schemaOfIndex);
return node.next(nextSchema);
return node.next(schemaOfItem);
}
if (matches.length > 1) {
return draft.errors.multipleOneOfError({ matches, pointer, schema, value: data });
Expand Down
8 changes: 0 additions & 8 deletions dist/module/lib/mergeSchema.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import getTypeOf from "./getTypeOf";
import { isObject } from "./utils/isObject";
export function mergeSchema(a, b, ...omit) {
var _a;
if ((b === null || b === void 0 ? void 0 : b.type) === "error") {
return b;
}
Expand All @@ -17,13 +16,6 @@ export function mergeSchema(a, b, ...omit) {
for (let i = 0; i < omit.length; i += 1) {
delete schema[omit[i]];
}
if (!isObject(schema)) {
return schema;
}
const originalOrigin = (_a = b.getOneOfOrigin) !== null && _a !== void 0 ? _a : a.getOneOfOrigin;
if (originalOrigin) {
Object.defineProperty(schema, "getOneOfOrigin", { enumerable: false, value: originalOrigin });
}
return schema;
}
export function mergeSchema2(a, b, property) {
Expand Down
11 changes: 0 additions & 11 deletions dist/module/lib/schema/createOneOfSchemaResult.js

This file was deleted.

3 changes: 0 additions & 3 deletions dist/module/lib/utils/omit.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,5 @@ export function omit(object, ...keysToOmit) {
result[key] = object[key];
}
});
if (object.getOneOfOrigin) {
Object.defineProperty(result, "getOneOfOrigin", { enumerable: false, value: object.getOneOfOrigin });
}
return result;
}
4 changes: 1 addition & 3 deletions dist/module/lib/utils/shallowCloneSchema.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
export function shallowCloneSchemaNode(node) {
const result = { ...node };
Object.defineProperty(result, "getOneOfOrigin", { enumerable: false, value: node.getOneOfOrigin });
return result;
return { ...node };
}
16 changes: 5 additions & 11 deletions lib/features/oneOf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
*/
import flattenArray from "../utils/flattenArray";
import settings from "../config/settings";
import { createOneOfSchemaResult } from "../schema/createOneOfSchemaResult";
import { errorOrPromise } from "../utils/filter";
import { JsonSchema, JsonError, isJsonError } from "../types";
import { isObject } from "../utils/isObject";
Expand Down Expand Up @@ -58,8 +57,7 @@ export function resolveOneOf(node: SchemaNode, data: any): SchemaNode | JsonErro
if (result.length > 0) {
errors.push(...result);
} else {
const nextSchema = createOneOfSchemaResult(schema, oneNode.schema, i); // return resolved schema
return resultNode.next(nextSchema);
return resultNode.next(oneNode.schema);
}
}

Expand Down Expand Up @@ -87,8 +85,7 @@ export function resolveOneOf(node: SchemaNode, data: any): SchemaNode | JsonErro
}

if (matches.length === 1) {
const nextSchema = createOneOfSchemaResult(schema, matches[0].schema, matches[0].index); // return resolved schema
return node.next(nextSchema);
return node.next(matches[0].schema);
}
if (matches.length > 1) {
return draft.errors.multipleOneOfError({
Expand Down Expand Up @@ -185,8 +182,7 @@ export function resolveOneOfFuzzy(node: SchemaNode, data: any): SchemaNode | Jso
if (result.length > 0) {
errors.push(...result);
} else {
const nextSchema = createOneOfSchemaResult(schema, oneNode.schema, i);
return resultNode.next(nextSchema);
return resultNode.next(oneNode.schema);
}
}

Expand All @@ -210,8 +206,7 @@ export function resolveOneOfFuzzy(node: SchemaNode, data: any): SchemaNode | Jso
}

if (matches.length === 1) {
const nextSchema = createOneOfSchemaResult(schema, matches[0].schema, matches[0].index);
return node.next(nextSchema);
return node.next(matches[0].schema);
}

// fuzzy match oneOf
Expand Down Expand Up @@ -240,8 +235,7 @@ export function resolveOneOfFuzzy(node: SchemaNode, data: any): SchemaNode | Jso
});
}

const nextSchema = createOneOfSchemaResult(schema, schemaOfItem, schemaOfIndex);
return node.next(nextSchema);
return node.next(schemaOfItem);
}

if (matches.length > 1) {
Expand Down
7 changes: 0 additions & 7 deletions lib/mergeSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,7 @@ export function mergeSchema<T extends JsonSchema>(a: T, b: T, ...omit: string[])
for (let i = 0; i < omit.length; i += 1) {
delete schema[omit[i]];
}
if (!isObject(schema)) {
return schema;
}

const originalOrigin = b.getOneOfOrigin ?? a.getOneOfOrigin;
if (originalOrigin) {
Object.defineProperty(schema, "getOneOfOrigin", { enumerable: false, value: originalOrigin });
}
return schema;
}

Expand Down
17 changes: 0 additions & 17 deletions lib/schema/createOneOfSchemaResult.ts

This file was deleted.

3 changes: 0 additions & 3 deletions lib/utils/omit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,5 @@ export function omit(object: Record<string, unknown>, ...keysToOmit: string[]) {
result[key] = object[key];
}
});
if (object.getOneOfOrigin) {
Object.defineProperty(result, "getOneOfOrigin", { enumerable: false, value: object.getOneOfOrigin });
}
return result;
}
4 changes: 1 addition & 3 deletions lib/utils/shallowCloneSchema.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { JsonSchema } from "../types";

export function shallowCloneSchemaNode(node: JsonSchema) {
const result = { ...node };
Object.defineProperty(result, "getOneOfOrigin", { enumerable: false, value: node.getOneOfOrigin });
return result;
return { ...node };
}
35 changes: 0 additions & 35 deletions test/unit/reduceSchema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,39 +64,4 @@ describe("reduceSchema", () => {
});
});
});

describe("oneOf", () => {
it("should return oneOf source schema for resolved oneOf object", () => {
const staticSchema = reduceSchema(
draft,
{
type: "object",
oneOfProperty: "type",
oneOf: [
{
type: "object",
required: ["type", "title"],
properties: {
type: { type: "string", const: "paragraph" },
title: { type: "string", title: "paragprah title" }
}
},
{
type: "object",
required: ["type", "title"],
properties: {
type: { type: "string", const: "section" },
title: { type: "string", title: "section title" }
}
}
]
},
{ type: "section", title: "" },
"#"
);

assert.equal(typeof staticSchema.getOneOfOrigin, "function");
assert.equal(staticSchema.getOneOfOrigin().index, 1);
});
});
});
22 changes: 0 additions & 22 deletions test/unit/step/step.oneOf.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,28 +41,6 @@ describe("step.oneof", () => {
expect(res).to.deep.eq({ type: "number", title: "Zahl" });
});

it("should return index of matching schema", () => {
const res = step(
draft,
"title",
{
type: "object",
properties: {
title: {
oneOf: [
{ type: "string", title: "Zeichenkette" },
{ type: "number", title: "Zahl" }
]
}
}
},
{ title: 111 }
);

expect(typeof res.getOneOfOrigin).to.eq("function");
expect(res.getOneOfOrigin().index).to.eq(1);
});

// PR #35 https://github.com/sagold/json-schema-library/pull/35/commits/8b6477113bdfce522081473bb0dd8fd6fe680391
it("should maintain references from a remote schema when resolving oneOf with $ref", () => {
draft.addRemoteSchema("https://my-other-schema.com/schema.json", {
Expand Down

0 comments on commit 773ace8

Please sign in to comment.