Skip to content

Commit

Permalink
Better error handling for resource generating
Browse files Browse the repository at this point in the history
  • Loading branch information
ex7r3me committed Nov 26, 2024
1 parent 7e47a46 commit 26b3983
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 10 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@lolocompany/ra-lolo-cli",
"version": "0.2.3",
"version": "0.3.0",
"description": "CLI tool to build react-admin components based on json-schema",
"main": "index.js",
"bin": {
Expand Down
36 changes: 30 additions & 6 deletions services/componentService.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,45 @@
export const generateFields = (properties, disableChoices = false) => {
if (!Array.isArray(properties)) {
throw new Error("Invalid properties: must be an array.");
}

return properties.map((field) => {
if (!field || !field.component || !field.value) {
console.warn("Invalid field definition:", field);
return null;
}

// Handle NestedObjectSection
if (field.component === "NestedObjectSection") {
if (!Array.isArray(field.properties)) {
console.warn("Invalid NestedObjectSection properties:", field);
return null;
}
const nestedFields = field.properties
.map(
(nestedField) =>
`<${nestedField.component} source="${nestedField.value}" />`
nestedField.component && nestedField.value
? `<${nestedField.component} source="${nestedField.value}" />`
: null
)
.filter(Boolean)
.join("\n");
return `<>
<h3>${field.name}</h3>
<h3>${field.name || "Section"}</h3>
${nestedFields}
</>`;
}

// Handle ArrayObjectSimpleFormIterator
if (field.component === "ArrayObjectSimpleFormIterator") {
if (!Array.isArray(field.items)) {
console.warn("Invalid items for ArrayObjectSimpleFormIterator:", field);
return null;
}
return `<>
<ArrayInput source="${field.value}">
<SimpleFormIterator>
${generateFields(field.items).join("\n")}
${generateFields(field.items, disableChoices).join("\n")}
</SimpleFormIterator>
</ArrayInput>
</>`;
Expand All @@ -39,12 +59,16 @@ export const generateFields = (properties, disableChoices = false) => {
return `<ReferenceInput source="${
field.value
}" reference="${field.value.replace(/Id$/, "s")}">
<AutocompleteInput source="devices" optionText="name" />
<AutocompleteInput optionText="name" />
</ReferenceInput>`;
}

// Handle CheckboxGroupInput for createView
if (field.component === "CheckboxGroupInput") {
if (!disableChoices && !Array.isArray(field.choices)) {
console.warn("Invalid choices for CheckboxGroupInput:", field);
return null;
}
return `<CheckboxGroupInput source="${field.value}"${
!disableChoices ? ` choices={${JSON.stringify(field.choices)}}` : ""
} />`;
Expand All @@ -61,5 +85,5 @@ export const generateFields = (properties, disableChoices = false) => {
? ` choices={${JSON.stringify(field.choices)}}`
: ""
} />`;
});
};
}).filter(Boolean); // Remove null/invalid fields from the result
};
13 changes: 12 additions & 1 deletion services/schemaService.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,17 @@ export const getProperties = (schema, parentKey = "") => {
filter: [],
};

// Validate schema and schema.properties
if (
!schema ||
typeof schema !== "object" ||
!schema.properties ||
typeof schema.properties !== "object"
) {
console.warn("Invalid schema or schema.properties is not an object.");
return views; // Return empty views if invalid
}

Object.keys(schema.properties).forEach((key) => {
const property = schema.properties[key];
const fieldName = parentKey ? `${parentKey}.${key}` : key;
Expand Down Expand Up @@ -76,4 +87,4 @@ export const getProperties = (schema, parentKey = "") => {
});

return views;
};
};

0 comments on commit 26b3983

Please sign in to comment.