Skip to content

Commit 45302ba

Browse files
authored
feat: add support for cross-domain nested fieldsets and new type editor (#21)
1 parent 2426a21 commit 45302ba

File tree

7 files changed

+276
-102
lines changed

7 files changed

+276
-102
lines changed

package-lock.json

Lines changed: 92 additions & 90 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
@@ -1,6 +1,6 @@
11
{
22
"name": "@makehq/forman-schema",
3-
"version": "1.6.0",
3+
"version": "1.7.0",
44
"description": "Forman Schema Tools",
55
"license": "MIT",
66
"author": "Make",

src/forman.ts

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,15 @@ export interface DomainRoot {
4848
* @param nested The nested fields to add
4949
* @param tail The tail of parameters in nested selects, required to resolve RPC payloads
5050
*/
51-
addFields: (nested: FormanSchemaField[], tail?: string[]) => void;
51+
addFields: (nested: (FormanSchemaField | string)[], tail?: string[]) => void;
5252
}
5353

5454
/**
5555
* Buffer of fields to be added to a domain before the root has been identified
5656
*/
5757
export type FormanDomainBuffer = {
5858
/** Field to add */
59-
field: FormanSchemaField;
59+
field: FormanSchemaField | string;
6060
/** Tail of parameters in nested selects, required to resolve RPC payloads */
6161
tail?: string[];
6262
};
@@ -92,6 +92,7 @@ const FORMAN_TYPE_MAP: Readonly<Record<string, JSONSchema7['type']>> = {
9292
collection: 'object',
9393
dynamicCollection: 'object',
9494
text: 'string',
95+
editor: 'string',
9596
number: 'number',
9697
boolean: 'boolean',
9798
date: 'string',
@@ -228,7 +229,15 @@ function handleCollectionType(field: FormanSchemaField, result: JSONSchema7, con
228229
required: [],
229230
});
230231

231-
function addField(subField: FormanSchemaField, tail?: string[]) {
232+
function addField(subField: FormanSchemaField | string, tail?: string[]) {
233+
if (typeof subField === 'string') {
234+
const value = { $ref: appendQueryString(subField, context.domain, tail || context.tail) };
235+
236+
result.allOf ||= [];
237+
result.allOf.push(value);
238+
return;
239+
}
240+
232241
if (isVisualType(subField.type)) {
233242
return;
234243
}
@@ -275,7 +284,7 @@ function handleCollectionType(field: FormanSchemaField, result: JSONSchema7, con
275284
const buffer = context.roots[domainRoot]?.buffer;
276285

277286
context.roots[domainRoot] = {
278-
addFields: (nested: FormanSchemaField[], tail?: string[]) => {
287+
addFields: (nested: (FormanSchemaField | string)[], tail?: string[]) => {
279288
nested.forEach(subField => addField(subField, tail));
280289
},
281290
};
@@ -483,22 +492,20 @@ function handleSelectType(field: FormanSchemaField, result: JSONSchema7, context
483492
}
484493

485494
if (nested && domain && domain !== context.domain) {
486-
if (typeof nested === 'string' || nestedContainsStrings) {
487-
throw new SchemaConversionError('Dynamic nested fields with domain change are not supported.');
488-
}
495+
const normalizedNested = typeof nested === 'string' ? [nested] : nested;
489496

490497
let root = context.roots[domain];
491498
if (!root) {
492499
const buffer: FormanDomainBuffer[] = [];
493500
root = context.roots[domain] = {
494501
buffer,
495-
addFields: (nested: FormanSchemaField[], tail?: string[]) => {
502+
addFields: (nested: (FormanSchemaField | string)[], tail?: string[]) => {
496503
buffer.push(...nested.map(field => ({ field, tail })));
497504
},
498505
};
499506
}
500507

501-
root.addFields(nested as FormanSchemaField[], [...context.tail, field.name!]);
508+
root.addFields(normalizedNested, [...context.tail, field.name!]);
502509
} else if (nested) {
503510
Object.defineProperty(result, 'x-nested', {
504511
configurable: true,

src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ export type FormanSchemaNested = (FormanSchemaField | string)[] | string | Forma
196196
*/
197197
export type FormanSchemaExtendedNested = {
198198
/** Store for the nested fields */
199-
store: FormanSchemaField[] | string;
199+
store: (FormanSchemaField | string)[] | string;
200200
/** Domain for the nested fields */
201201
domain?: string;
202202
};

0 commit comments

Comments
 (0)