Skip to content

Commit 021b2f1

Browse files
authored
refactor: enhance validateFieldGroups method to return sorted column list and improve error handling (#144)
1 parent efc48a1 commit 021b2f1

File tree

1 file changed

+28
-12
lines changed

1 file changed

+28
-12
lines changed

adminforth/modules/configValidator.ts

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -330,23 +330,38 @@ export default class ConfigValidator implements IConfigValidator {
330330
return showInTransformedToObject as ShowIn;
331331
}
332332

333-
validateFieldGroups(fieldGroups: {
334-
groupName: string;
335-
columns: string[];
336-
}[], resourceColumns: string[]): void {
337-
if (!fieldGroups) return;
333+
validateFieldGroups(fieldGroups: { groupName: string; columns: string[] }[], allColumnsList: string[]): string[] {
334+
if (!fieldGroups) return allColumnsList;
335+
336+
const columnPositions = new Map<string, number>();
337+
let position = 0;
338338

339339
fieldGroups.forEach((group) => {
340340
group.columns.forEach((col) => {
341-
if (!resourceColumns.includes(col)) {
342-
const similar = suggestIfTypo(resourceColumns, col);
341+
if (!allColumnsList.includes(col)) {
342+
const similar = suggestIfTypo(allColumnsList, col);
343343
throw new Error(
344-
`Group '${group.groupName}' has an unknown column '${col}'. ${similar ? `Did you mean '${similar}'?` : ''
344+
`Group '${group.groupName}' has an unknown column '${col}'. ${
345+
similar ? `Did you mean '${similar}'?` : ''
345346
}`
346347
);
347348
}
349+
if (!columnPositions.has(col)) {
350+
columnPositions.set(col, position++);
351+
}
348352
});
349353
});
354+
355+
allColumnsList.forEach((col) => {
356+
if (!columnPositions.has(col)) {
357+
columnPositions.set(col, position++);
358+
}
359+
});
360+
return allColumnsList.sort((a, b) => {
361+
const posA = columnPositions.get(a);
362+
const posB = columnPositions.get(b);
363+
return posA - posB;
364+
});
350365
}
351366

352367
validateAndNormalizeCustomActions(resInput: AdminForthResourceInput, res: Partial<AdminForthResource>, errors: string[]): any[] {
@@ -703,10 +718,11 @@ export default class ConfigValidator implements IConfigValidator {
703718
options.actions = this.validateAndNormalizeCustomActions(resInput, res, errors);
704719

705720
const allColumnsList = res.columns.map((col) => col.name);
706-
this.validateFieldGroups(options.fieldGroups, allColumnsList);
707-
this.validateFieldGroups(options.showFieldGroups, allColumnsList);
708-
this.validateFieldGroups(options.createFieldGroups, allColumnsList);
709-
this.validateFieldGroups(options.editFieldGroups, allColumnsList);
721+
const sortedColumns = this.validateFieldGroups(options.fieldGroups, allColumnsList);
722+
723+
res.columns = res.columns.sort((a, b) => {
724+
return sortedColumns.indexOf(a.name) - sortedColumns.indexOf(b.name);
725+
});
710726

711727
// if pageInjection is a string, make array with one element. Also check file exists
712728
const possibleInjections = ['beforeBreadcrumbs', 'afterBreadcrumbs', 'bottom', 'threeDotsDropdownItems', 'customActionIcons'];

0 commit comments

Comments
 (0)