Skip to content

Commit 5cffc11

Browse files
committed
chore: refactoring + tests
1 parent a8f95c1 commit 5cffc11

File tree

7 files changed

+109
-51
lines changed

7 files changed

+109
-51
lines changed

package-lock.json

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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": "github:Exabyte-io/esse#b5516137e018dd35a060fce519dcd058eeff70c9",
51+
"@exabyte-io/esse.js": "2022.7.28-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: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import { schemas } from "@exabyte-io/esse.js/schemas";
22
import mergeAllOf from "json-schema-merge-allof";
33

4-
const schemaCache = new Map();
5-
6-
export const JSONSchemasInterface = {
7-
schemas() {
8-
return schemas;
9-
},
10-
11-
schemaById(schemaId) {
12-
if (!schemaCache.has(schemaId)) {
4+
const schemasCache = new Map();
5+
export class JSONSchemasInterface {
6+
/**
7+
*
8+
* @param {string} schemaId id of JSON schema from ESSE
9+
* @returns {Object.<string, any>} resolved JSON schema
10+
*/
11+
static schemaById(schemaId) {
12+
if (!schemasCache.has(schemaId)) {
1313
const originalSchema = schemas.find((schema) => schema.schemaId === schemaId);
1414

1515
const schema = mergeAllOf(originalSchema, {
@@ -18,15 +18,11 @@ export const JSONSchemasInterface = {
1818
},
1919
});
2020

21-
schemaCache.set(schemaId, schema);
21+
schemasCache.set(schemaId, schema);
2222
}
2323

24-
return schemaCache.get(schemaId);
25-
},
26-
27-
resolvedSchemaById(schemaId) {
28-
return this.schemaById(schemaId);
29-
},
24+
return schemasCache.get(schemaId);
25+
}
3026

3127
/**
3228
* @example <caption>Search by schemaId regex</caption>
@@ -49,13 +45,13 @@ export const JSONSchemasInterface = {
4945
* @param {Object} query - An object containing mongo-like search query
5046
* @returns {Object|null} JSON schema
5147
*/
52-
matchSchema(query) {
48+
static matchSchema(query) {
5349
const searchFields = Object.keys(query);
5450
return schemas.find((schema) => {
5551
return searchFields.every((field) => {
5652
const { $regex } = query[field];
5753
return new RegExp($regex).test(schema[field]);
5854
});
5955
});
60-
},
61-
};
56+
}
57+
}

src/entity/in_memory.js

Lines changed: 47 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
import lodash from "lodash";
21
import mergeAllOf from "json-schema-merge-allof";
2+
import lodash from "lodash";
33

44
// import { ESSE } from "@exabyte-io/esse.js";
55
import { deepClone } from "../utils/clone";
6-
import { getSchemaByClassName, getMixSchemasByClassName } from "../utils/schemas";
7-
6+
import { getMixSchemasByClassName, getSchemaByClassName } from "../utils/schemas";
87

98
// TODO: https://exabyte.atlassian.net/browse/SOF-5946
109
// const schemas = new ESSE().schemas;
@@ -175,36 +174,61 @@ export class InMemoryEntity {
175174
return filtered[0];
176175
}
177176

177+
/**
178+
* @summary If there any nested in-memory entities, first resolve them
179+
* and then mix with original schema in baseJSONSchema()
180+
* @returns {Object.<string,InMemoryEntity>|null}
181+
* @example
182+
* class Workflow extends InMemoryEntity {
183+
* get customJsonSchemaProperties() {
184+
* return {
185+
* subworkflows: {
186+
* type: 'array',
187+
* items: Subworkflow.jsonSchema
188+
* }
189+
* };
190+
* }
191+
* }
192+
*/
178193
static get customJsonSchemaProperties() {
179194
return null;
180195
}
181196

182-
static getMainJsonSchema() {
183-
const originalSchema = getSchemaByClassName(this.name);
184-
197+
/**
198+
* Returns original ESSE schema with nested properties from customJsonSchemaProperties
199+
* @see customJsonSchemaProperties
200+
* @returns {Object} schema
201+
*/
202+
static get baseJSONSchema() {
185203
if (!this.customJsonSchemaProperties) {
186-
return originalSchema;
204+
return getSchemaByClassName(this.name);
187205
}
188206

207+
const { properties, ...schema } = getSchemaByClassName(this.name);
208+
189209
return {
190-
...originalSchema,
210+
...schema,
191211
properties: {
192-
...originalSchema.properties,
193-
...this.customJsonSchemaProperties
194-
}
195-
}
212+
...properties,
213+
...this.customJsonSchemaProperties,
214+
},
215+
};
196216
}
197217

218+
/**
219+
* Returns resolved JSON schema with custom properties and all mixes from schemas.js
220+
* @returns {Object} schema
221+
*/
198222
static get jsonSchema() {
199-
return mergeAllOf({
200-
allOf: [
201-
this.getMainJsonSchema(),
202-
...getMixSchemasByClassName(this.name)
203-
]
204-
}, {
205-
resolvers: {
206-
defaultResolver: mergeAllOf.options.resolvers.title
207-
}
208-
});
209-
}
223+
return mergeAllOf(
224+
{
225+
allOf: [this.baseJSONSchema, ...getMixSchemasByClassName(this.name)],
226+
},
227+
{
228+
resolvers: {
229+
defaultResolver: mergeAllOf.options.resolvers.title,
230+
},
231+
},
232+
);
233+
}
210234
}

src/utils/schemas.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { JSONSchemasInterface } from "../JSONSchemasInterface";
22

3-
const mainSchemas = {
3+
export const mainSchemas = {
44
Material: "material",
55
Entity: "system-entity",
66
BankMaterial: "material",
@@ -50,7 +50,7 @@ const flavorMix = ["system-is-multi-material"];
5050

5151
const systemEntityMix = ["system-entity"];
5252

53-
const mixSchemas = {
53+
export const mixSchemas = {
5454
Entity: [...entityMix],
5555
Material: [...entityMix],
5656
BankMaterial: [...entityMix, ...bankMaterialMix],
Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,33 @@
11
import { expect } from "chai";
2-
import { JSONSchemasInterface } from './../src/JSONSchemasInterface';
2+
3+
import { JSONSchemasInterface } from "../src/JSONSchemasInterface";
4+
import { mainSchemas, mixSchemas } from "../src/utils/schemas";
35

46
describe("JSONSchemasInterface", () => {
7+
it("can find main schema", () => {
8+
Object.values(mainSchemas).forEach((schemaId) => {
9+
const schema = JSONSchemasInterface.schemaById(schemaId);
10+
expect(schema).to.be.an("object");
11+
});
12+
});
513

6-
it("can find schema", () => {
7-
const schema = JSONSchemasInterface.schemaById('workflow');
8-
expect(schema).to.be.an('object');
14+
it("can find mix schemas", () => {
15+
Object.values(mixSchemas).forEach((schemaIds) => {
16+
schemaIds.forEach((schemaId) => {
17+
const schema = JSONSchemasInterface.schemaById(schemaId);
18+
expect(schema).to.be.an("object");
19+
});
20+
});
921
});
1022

23+
it("can match schemas", () => {
24+
const schemaId = Object.values(mainSchemas)[0];
25+
const schema = JSONSchemasInterface.matchSchema({
26+
schemaId: {
27+
$regex: schemaId,
28+
},
29+
});
30+
31+
expect(schema).to.be.an("object");
32+
});
1133
});

tests/in_memory.tests.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,19 @@ describe("InMemoryEntity", () => {
4545
const entity = new InMemoryEntity(obj);
4646
expect(JSON.stringify(entity.toJSON())).to.be.equal(JSON.stringify(obj));
4747
});
48+
49+
it("jsonSchema returns correct schema", () => {
50+
class Entity extends InMemoryEntity {
51+
static get customJsonSchemaProperties() {
52+
return {
53+
nested: {
54+
type: "string",
55+
},
56+
};
57+
}
58+
}
59+
expect(Entity.jsonSchema).to.be.an("object");
60+
expect(Entity.jsonSchema).to.have.nested.property("properties.isDefault"); // check mix schemas
61+
expect(Entity.jsonSchema).to.have.nested.property("properties.nested.type"); // check custom properties
62+
});
4863
});

0 commit comments

Comments
 (0)