Skip to content

Commit

Permalink
feature(stackbit): enhance inlining capabilities
Browse files Browse the repository at this point in the history
  • Loading branch information
julrich committed Jul 9, 2024
1 parent 099d120 commit 9e94b02
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@kickstartds/jsonschema2stackbit",
"comment": "enhance inlining capabilities",
"type": "patch"
}
],
"packageName": "@kickstartds/jsonschema2stackbit"
}
23 changes: 21 additions & 2 deletions tools/jsonschema2stackbit/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ export function configuration(
);
}

function isObjectModel(field: unknown): field is ObjectModel {
return typeof field === 'object' && field !== null && 'type' in field && field.type === 'object';
}

/**
* @param jsonSchemas - An individual schema or an array of schemas, provided
* either as Javascript objects or as JSON text.
Expand Down Expand Up @@ -79,9 +83,24 @@ export function convert({

traverse(
reduced,
({ key, value, parent }) => {
({ value, parent }) => {
if (value && value.group && value.group === 'INLINE') {
console.log(key, value, parent);
const schema = parent?.find((field: Field) => isObjectModel(field) && field.name === value.name);
if (
schema &&
isObjectModel(schema) &&
schema.fields &&
schema.fields.length > 0 &&
Array.isArray(parent)
) {
for (const field of schema.fields) {
parent.unshift({
...field,
name: `${value.name}_${field.name}`,
group: value.group
});
}
}
}
},
{
Expand Down

0 comments on commit 9e94b02

Please sign in to comment.