Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(application-config): Enforce uniqueness for uriPath of the submenu links. #2567

Merged
merged 6 commits into from
May 3, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/bright-hornets-enjoy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@commercetools-frontend/application-config': minor
Rhotimee marked this conversation as resolved.
Show resolved Hide resolved
---

SubmenuLinks are meant to be "unique" based on the `uriPath`
An error is thrown if there are duplicate uriPath in the submenuLinks.
Rhotimee marked this conversation as resolved.
Show resolved Hide resolved
3 changes: 3 additions & 0 deletions packages/application-config/src/transformers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { JSONSchemaForCustomApplicationConfigurationFiles } from './schema';
import type { CustomApplicationData } from './types';
import { entryPointUriPathToResourceAccesses } from './formatters';
import { validateSubmenuLinks } from './validate-config';

// The `uriPath` of each submenu link is supposed to be defined relative
// to the `entryPointUriPath`. Computing the full path is done internally to keep
Expand All @@ -24,6 +25,8 @@ function transformCustomApplicationConfigToData(
appConfig.entryPointUriPath
);

validateSubmenuLinks(appConfig.submenuLinks);

return {
id: appConfig.env.production.applicationId,
name: appConfig.name,
Expand Down
16 changes: 16 additions & 0 deletions packages/application-config/src/validate-config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { JSONSchemaForCustomApplicationConfigurationFiles } from './schema';
import Ajv, { type ErrorObject } from 'ajv';
import schemaJson from '../schema.json';
import type { CustomApplicationData } from './types';

type ErrorAdditionalProperty = ErrorObject<
'additionalProperty',
Expand All @@ -9,6 +10,7 @@ type ErrorAdditionalProperty = ErrorObject<
type ErrorEnum = ErrorObject<'enum', { allowedValues: string[] }>;

const ajv = new Ajv({ strict: true, useDefaults: true });

const validate =
ajv.compile<JSONSchemaForCustomApplicationConfigurationFiles>(schemaJson);

Expand Down Expand Up @@ -46,3 +48,17 @@ const validateConfig = (
};

export default validateConfig;

export const validateSubmenuLinks = (
Rhotimee marked this conversation as resolved.
Show resolved Hide resolved
submenuLinks: CustomApplicationData['submenuLinks']
) => {
const uriPathSet = new Set();
submenuLinks.forEach(({ uriPath }) => {
if (uriPathSet.has(uriPath)) {
throw new Error(
'Duplicate URI path. Every submenu link must have a unique URI path value'
);
}
uriPathSet.add(uriPath);
});
};
23 changes: 22 additions & 1 deletion packages/application-config/test/json-schema.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import validateConfig from '../src/validate-config';
import validateConfig, { validateSubmenuLinks } from '../src/validate-config';
import fixtureConfigSimple from './fixtures/config-simple.json';
import fixtureConfigFull from './fixtures/config-full.json';
import fixtureConfigOidc from './fixtures/config-oidc.json';
Expand Down Expand Up @@ -173,4 +173,25 @@ describe('invalid configurations', () => {
`" must have required property 'mainMenuLink'"`
);
});
it('should validate that "uriPath" for "submenuLinks" is unique', () => {
expect(() =>
validateSubmenuLinks([
{
uriPath: 'custom-app/avengers',
defaultLabel: 'avengers',
permissions: [],
labelAllLocales: [],
},

{
uriPath: 'custom-app/avengers',
defaultLabel: 'justice-league',
permissions: [],
labelAllLocales: [],
},
])
).toThrowErrorMatchingInlineSnapshot(
`"Duplicate URI path. Every submenu link must have a unique URI path value"`
);
});
});