|
1 | | -import lodash from "lodash"; |
2 | 1 | import mergeAllOf from "json-schema-merge-allof"; |
| 2 | +import lodash from "lodash"; |
3 | 3 |
|
4 | 4 | // import { ESSE } from "@exabyte-io/esse.js"; |
5 | 5 | import { deepClone } from "../utils/clone"; |
6 | | -import { getSchemaByClassName, getMixSchemasByClassName } from "../utils/schemas"; |
7 | | - |
| 6 | +import { getMixSchemasByClassName, getSchemaByClassName } from "../utils/schemas"; |
8 | 7 |
|
9 | 8 | // TODO: https://exabyte.atlassian.net/browse/SOF-5946 |
10 | 9 | // const schemas = new ESSE().schemas; |
@@ -175,36 +174,61 @@ export class InMemoryEntity { |
175 | 174 | return filtered[0]; |
176 | 175 | } |
177 | 176 |
|
| 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 | + */ |
178 | 193 | static get customJsonSchemaProperties() { |
179 | 194 | return null; |
180 | 195 | } |
181 | 196 |
|
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() { |
185 | 203 | if (!this.customJsonSchemaProperties) { |
186 | | - return originalSchema; |
| 204 | + return getSchemaByClassName(this.name); |
187 | 205 | } |
188 | 206 |
|
| 207 | + const { properties, ...schema } = getSchemaByClassName(this.name); |
| 208 | + |
189 | 209 | return { |
190 | | - ...originalSchema, |
| 210 | + ...schema, |
191 | 211 | properties: { |
192 | | - ...originalSchema.properties, |
193 | | - ...this.customJsonSchemaProperties |
194 | | - } |
195 | | - } |
| 212 | + ...properties, |
| 213 | + ...this.customJsonSchemaProperties, |
| 214 | + }, |
| 215 | + }; |
196 | 216 | } |
197 | 217 |
|
| 218 | + /** |
| 219 | + * Returns resolved JSON schema with custom properties and all mixes from schemas.js |
| 220 | + * @returns {Object} schema |
| 221 | + */ |
198 | 222 | 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 | + } |
210 | 234 | } |
0 commit comments