Skip to content

Commit 4ce829a

Browse files
Merge pull request #22 from Exabyte-io/update/SOF-6251
SOF-6251: custom schema resolving
2 parents dfed549 + 387ccc3 commit 4ce829a

File tree

4 files changed

+66
-10
lines changed

4 files changed

+66
-10
lines changed

src/JSONSchemasInterface.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ export class JSONSchemasInterface {
2424
return schemasCache.get(schemaId);
2525
}
2626

27+
/**
28+
*
29+
* @param {Object} - external schema
30+
*/
31+
static registerSchema(schema) {
32+
schemasCache.set(schema.schemaId, schema);
33+
}
34+
2735
/**
2836
* @example <caption>Search by schemaId regex</caption>
2937
* JSONSchemasInterface.matchSchema({

src/utils/schemas.js

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,31 +24,35 @@ export const baseSchemas = {
2424
Unit: "workflow-unit",
2525
};
2626

27-
const entityMix = [
27+
export const entityMix = [
2828
"system-description-object",
2929
"system-base-entity-set",
3030
"system-sharing",
3131
"system-metadata",
3232
"system-defaultable",
3333
];
3434

35-
const subWorkflowMix = ["system-system-name", "system-is-multi-material"];
35+
export const subWorkflowMix = ["system-system-name", "system-is-multi-material"];
3636

37-
const workflowMix = ["workflow-base-flow", "system-history", "system-is-outdated"];
37+
export const workflowMix = ["workflow-base-flow", "system-history", "system-is-outdated"];
3838

39-
const bankMaterialMix = ["material-conventional", "system-creator-account"];
39+
export const bankMaterialMix = ["material-conventional", "system-creator-account"];
4040

41-
const bankWorkflowMix = ["system-creator-account"];
41+
export const bankWorkflowMix = ["system-creator-account"];
4242

43-
const jobMix = ["system-status", "system-job-extended"];
43+
export const jobMix = ["system-status", "system-job-extended"];
4444

45-
const unitMix = ["system-unit-extended", "system-status", "workflow-unit-runtime-runtime-items"];
45+
export const unitMix = [
46+
"system-unit-extended",
47+
"system-status",
48+
"workflow-unit-runtime-runtime-items",
49+
];
4650

47-
const assignmentUnitMix = ["system-scope"];
51+
export const assignmentUnitMix = ["system-scope"];
4852

49-
const flavorMix = ["system-is-multi-material"];
53+
export const flavorMix = ["system-is-multi-material"];
5054

51-
const systemEntityMix = ["system-entity"];
55+
export const systemEntityMix = ["system-entity"];
5256

5357
export const mixSchemas = {
5458
Entity: [...entityMix],
@@ -83,3 +87,14 @@ export function getMixSchemasByClassName(className) {
8387
? mixSchemas[className].map((schemaId) => JSONSchemasInterface.schemaById(schemaId))
8488
: [];
8589
}
90+
91+
/**
92+
* Register additional Entity classes to be resolved with jsonSchema property
93+
* @param {String} className - class name derived from InMemoryEntity
94+
* @param {String} classBaseSchema - base schemaId
95+
* @param {Array} classMixSchemas - array of schemaId to mix
96+
*/
97+
export function registerClassName(className, classBaseSchema, classMixSchemas) {
98+
baseSchemas[className] = classBaseSchema;
99+
mixSchemas[className] = classMixSchemas;
100+
}

tests/JSONSchemasInterface.tests.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,18 @@ describe("JSONSchemasInterface", () => {
3030

3131
expect(schema).to.be.an("object");
3232
});
33+
34+
it("can find registered schemas", () => {
35+
JSONSchemasInterface.registerSchema({
36+
schemaId: "test-schema-id",
37+
properties: {
38+
testProp: {
39+
type: "string",
40+
},
41+
},
42+
});
43+
44+
const schema = JSONSchemasInterface.schemaById("test-schema-id");
45+
expect(schema).to.be.an("object");
46+
});
3347
});

tests/in_memory.tests.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { expect } from "chai";
22

33
import { InMemoryEntity } from "../src/entity/in_memory";
4+
import { entityMix, registerClassName } from "../src/utils/schemas";
45

56
describe("InMemoryEntity", () => {
67
const obj = {
@@ -60,4 +61,22 @@ describe("InMemoryEntity", () => {
6061
expect(Entity.jsonSchema).to.have.nested.property("properties.isDefault"); // check mix schemas
6162
expect(Entity.jsonSchema).to.have.nested.property("properties.nested.type"); // check custom properties
6263
});
64+
65+
it("jsonSchema returns correct registered schema", () => {
66+
class RegisteredEntity extends InMemoryEntity {
67+
static get customJsonSchemaProperties() {
68+
return {
69+
nested: {
70+
type: "string",
71+
},
72+
};
73+
}
74+
}
75+
76+
registerClassName(RegisteredEntity.name, "system-entity", entityMix);
77+
78+
expect(RegisteredEntity.jsonSchema).to.be.an("object");
79+
expect(RegisteredEntity.jsonSchema).to.have.nested.property("properties.isDefault"); // check mix schemas
80+
expect(RegisteredEntity.jsonSchema).to.have.nested.property("properties.nested.type"); // check custom properties
81+
});
6382
});

0 commit comments

Comments
 (0)