Skip to content

Commit

Permalink
feat: prevent pages in pages
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicolas Moreau committed Oct 15, 2024
1 parent 4404bfe commit 498353c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/services/smart-action-form-layout-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@ const validLayoutComponents = ['Row', 'Page', 'Separator', 'HtmlBlock'];
class SmartActionFormLayoutService {
static validateLayoutElement(element) {
if (!validLayoutComponents.includes(element.component)) throw new Error(`${element.component} is not a valid component. Valid components are ${validLayoutComponents.join(' or ')}`);
if (element.component === 'Page' && !Array.isArray(element.elements)) {
throw new Error('Page components must contain an array of fields or layout elements in property \'elements\'');
if (element.component === 'Page') {
if (!Array.isArray(element.elements)) {
throw new Error('Page components must contain an array of fields or layout elements in property \'elements\'');
}
if (element.elements.some((innerElement) => innerElement.component === 'Page')) {
throw new Error('Pages cannot contain other pages');
}
}
if (element.component === 'Row') {
if (!Array.isArray(element.fields)) {
Expand Down
3 changes: 3 additions & 0 deletions test/services/smart-action-form-layout-service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ describe('services > smart-action-form-layout', () => {
it('should throw with message when Page does not contain elements', async () => {
expect(() => SmartActionFormLayoutService.validateLayoutElement({ component: 'Page' })).toThrow('Page components must contain an array of fields or layout elements in property \'elements\'');
});
it('should throw with message when Page contains pages', async () => {
expect(() => SmartActionFormLayoutService.validateLayoutElement({ component: 'Page', elements: [{ type: 'Layout', component: 'Page', elements: [] }] })).toThrow('Pages cannot contain other pages');
});

it('should throw with message when Row does not contain fields', async () => {
expect(() => SmartActionFormLayoutService.validateLayoutElement({ component: 'Row' })).toThrow('Row components must contain an array of fields in property \'fields\'');
Expand Down

0 comments on commit 498353c

Please sign in to comment.