Skip to content

Commit 9469773

Browse files
committed
fix: conflicts
2 parents 41c6098 + 67a28d1 commit 9469773

File tree

3 files changed

+110
-14
lines changed

3 files changed

+110
-14
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
"@babel/preset-react": "7.16.7",
4949
"@babel/register": "^7.16.0",
5050
"@babel/runtime-corejs3": "7.16.8",
51-
"@exabyte-io/esse.js": "git+https://github.com/Exabyte-io/esse.git#9745f43ab21c1b5774da6ce7601b017ff92d03b0",
51+
"@exabyte-io/esse.js": "2022.10.21-1",
5252
"crypto-js": "^4.1.1",
5353
"json-schema-merge-allof": "^0.8.1",
5454
"lodash": "^4.17.21",

src/JSONSchemasInterface.js

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,43 @@ import { schemas } from "@exabyte-io/esse.js/schemas";
22
import mergeAllOf from "json-schema-merge-allof";
33

44
const schemasCache = new Map();
5+
6+
/**
7+
* We assume that each schema in the application has its own unique schemaId
8+
* Unfortunately, mergeAllOf keeps schemaId after merging, and this results in multiple different schemas with the same schemaId
9+
* Hence this function
10+
*/
11+
function removeSchemaIdsAfterAllOf(schema, clean = false) {
12+
if (clean) {
13+
const { schemaId, ...restSchema } = schema;
14+
15+
return restSchema;
16+
}
17+
18+
if (Array.isArray(schema)) {
19+
return schema.map((item) => removeSchemaIdsAfterAllOf(item));
20+
}
21+
22+
if (typeof schema !== "object") {
23+
return schema;
24+
}
25+
26+
if (schema.allOf) {
27+
const { allOf, ...restSchema } = schema;
28+
29+
return {
30+
allOf: allOf.map((innerSchema) => removeSchemaIdsAfterAllOf(innerSchema, true)),
31+
...restSchema,
32+
};
33+
}
34+
35+
return Object.fromEntries(
36+
Object.entries(schema).map(([key, value]) => {
37+
return [key, removeSchemaIdsAfterAllOf(value)];
38+
}),
39+
);
40+
}
41+
542
export class JSONSchemasInterface {
643
/**
744
*
@@ -16,13 +53,7 @@ export class JSONSchemasInterface {
1653
throw new Error(`Schema not found: ${schemaId}`);
1754
}
1855

19-
const schema = mergeAllOf(originalSchema, {
20-
resolvers: {
21-
defaultResolver: mergeAllOf.options.resolvers.title,
22-
},
23-
});
24-
25-
schemasCache.set(schemaId, schema);
56+
this.registerSchema(originalSchema);
2657
}
2758

2859
return schemasCache.get(schemaId);
@@ -32,8 +63,16 @@ export class JSONSchemasInterface {
3263
*
3364
* @param {Object} - external schema
3465
*/
35-
static registerSchema(schema) {
66+
static registerSchema(originalSchema) {
67+
const schema = mergeAllOf(removeSchemaIdsAfterAllOf(originalSchema), {
68+
resolvers: {
69+
defaultResolver: mergeAllOf.options.resolvers.title,
70+
},
71+
});
72+
3673
schemasCache.set(schema.schemaId, schema);
74+
75+
return schema;
3776
}
3877

3978
/**

tests/JSONSchemasInterface.tests.js

Lines changed: 62 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { expect } from "chai";
1+
import { assert, expect } from "chai";
22

33
import { JSONSchemasInterface } from "../src/JSONSchemasInterface";
44

@@ -18,17 +18,74 @@ describe("JSONSchemasInterface", () => {
1818
expect(schema).to.be.an("object");
1919
});
2020

21-
it("can find registered schemas", () => {
21+
it("can find registered schemas; the schema is merged and clean", () => {
2222
JSONSchemasInterface.registerSchema({
23-
schemaId: "test-schema-id",
23+
schemaId: "system/in-set",
24+
$schema: "http://json-schema.org/draft-04/schema#",
25+
title: "System in-set schema",
2426
properties: {
25-
testProp: {
27+
inSet: {
28+
type: "array",
29+
items: {
30+
allOf: [
31+
{
32+
schemaId: "system/entity-reference",
33+
$schema: "http://json-schema.org/draft-04/schema#",
34+
title: "entity reference schema",
35+
properties: {
36+
_id: {
37+
description: "entity identity",
38+
type: "string",
39+
},
40+
cls: {
41+
description: "entity class",
42+
type: "string",
43+
},
44+
slug: {
45+
description: "entity slug",
46+
type: "string",
47+
},
48+
},
49+
},
50+
{
51+
type: "object",
52+
properties: {
53+
type: {
54+
type: "string",
55+
},
56+
index: {
57+
type: "number",
58+
},
59+
},
60+
},
61+
],
62+
},
63+
},
64+
valueMapFunction: {
65+
description: "Specifies the function to convert the currentValue in UI.",
2666
type: "string",
67+
enum: [
68+
"toString",
69+
"toContactUs",
70+
"toPlusMinusSign",
71+
"toUnlimited",
72+
"toSupportSeverity",
73+
],
74+
default: "toString",
2775
},
2876
},
2977
});
3078

31-
const schema = JSONSchemasInterface.schemaById("test-schema-id");
79+
const schema = JSONSchemasInterface.schemaById("system/in-set");
80+
3281
expect(schema).to.be.an("object");
82+
assert(schema.schemaId, "system/in-set");
83+
expect(schema.properties.inSet.items.schemaId).to.be.an("undefined");
84+
expect(schema.properties.inSet.items.properties).to.be.an("object");
85+
expect(schema.properties.valueMapFunction.enum[0]).to.be.an("string");
86+
expect(schema.properties.valueMapFunction.enum[1]).to.be.an("string");
87+
expect(schema.properties.valueMapFunction.enum[2]).to.be.an("string");
88+
expect(schema.properties.valueMapFunction.enum[3]).to.be.an("string");
89+
expect(schema.properties.valueMapFunction.enum[4]).to.be.an("string");
3390
});
3491
});

0 commit comments

Comments
 (0)