Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/JSONSchemasInterface.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ export class JSONSchemasInterface {
return schemasCache.get(schemaId);
}

/**
*
* @param {Object} - external schema
*/
static registerSchema(schema) {
schemasCache.set(schema.schemaId, schema);
}

/**
* @example <caption>Search by schemaId regex</caption>
* JSONSchemasInterface.matchSchema({
Expand Down
35 changes: 25 additions & 10 deletions src/utils/schemas.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,31 +24,35 @@ export const baseSchemas = {
Unit: "workflow-unit",
};

const entityMix = [
export const entityMix = [
"system-description-object",
"system-base-entity-set",
"system-sharing",
"system-metadata",
"system-defaultable",
];

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

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

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

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

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

const unitMix = ["system-unit-extended", "system-status", "workflow-unit-runtime-runtime-items"];
export const unitMix = [
"system-unit-extended",
"system-status",
"workflow-unit-runtime-runtime-items",
];

const assignmentUnitMix = ["system-scope"];
export const assignmentUnitMix = ["system-scope"];

const flavorMix = ["system-is-multi-material"];
export const flavorMix = ["system-is-multi-material"];

const systemEntityMix = ["system-entity"];
export const systemEntityMix = ["system-entity"];

export const mixSchemas = {
Entity: [...entityMix],
Expand Down Expand Up @@ -83,3 +87,14 @@ export function getMixSchemasByClassName(className) {
? mixSchemas[className].map((schemaId) => JSONSchemasInterface.schemaById(schemaId))
: [];
}

/**
* Register additional Entity classes to be resolved with jsonSchema property
* @param {String} className - class name derived from InMemoryEntity
* @param {String} classBaseSchema - base schemaId
* @param {Array} classMixSchemas - array of schemaId to mix
*/
export function registerClassName(className, classBaseSchema, classMixSchemas) {
baseSchemas[className] = classBaseSchema;
mixSchemas[className] = classMixSchemas;
}
14 changes: 14 additions & 0 deletions tests/JSONSchemasInterface.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,18 @@ describe("JSONSchemasInterface", () => {

expect(schema).to.be.an("object");
});

it("can find registered schemas", () => {
JSONSchemasInterface.registerSchema({
schemaId: "test-schema-id",
properties: {
testProp: {
type: "string",
},
},
});

const schema = JSONSchemasInterface.schemaById("test-schema-id");
expect(schema).to.be.an("object");
});
});
19 changes: 19 additions & 0 deletions tests/in_memory.tests.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { expect } from "chai";

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

describe("InMemoryEntity", () => {
const obj = {
Expand Down Expand Up @@ -60,4 +61,22 @@ describe("InMemoryEntity", () => {
expect(Entity.jsonSchema).to.have.nested.property("properties.isDefault"); // check mix schemas
expect(Entity.jsonSchema).to.have.nested.property("properties.nested.type"); // check custom properties
});

it("jsonSchema returns correct registered schema", () => {
class RegisteredEntity extends InMemoryEntity {
static get customJsonSchemaProperties() {
return {
nested: {
type: "string",
},
};
}
}

registerClassName(RegisteredEntity.name, "system-entity", entityMix);

expect(RegisteredEntity.jsonSchema).to.be.an("object");
expect(RegisteredEntity.jsonSchema).to.have.nested.property("properties.isDefault"); // check mix schemas
expect(RegisteredEntity.jsonSchema).to.have.nested.property("properties.nested.type"); // check custom properties
});
});