Skip to content

Commit

Permalink
fix: trim role names before validation (#8277)
Browse files Browse the repository at this point in the history
This trims role names before validation and subsequent validation.
This fixes a bug where you could have names that were empty or that
were duplicates of other names, but with leading or trailing
spaces. (They display the same in the UI).

This does not modify how we handle descriptions in the API. While the
UI form requires you to enter a description, the API does not. As
such, we can't make that required now without it being a breaking
change.
  • Loading branch information
thomasheartman authored Sep 26, 2024
1 parent 137b8ee commit 5a874df
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/lib/schema/role-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const permissionRoleSchema = joi
export const roleSchema = joi
.object()
.keys({
name: joi.string().required(),
name: joi.string().trim().required(),
description: joi.string().optional().allow('').allow(null).default(''),
permissions: joi
.array()
Expand Down
27 changes: 27 additions & 0 deletions src/lib/services/access-service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,33 @@ test('should accept empty permissions', async () => {
});
});

test('should not accept empty names', async () => {
const { accessService } = getSetup();
const withWhitespaceName: IRoleValidation = {
name: ' ',
description: 'description',
permissions: [],
};

await expect(
accessService.validateRole(withWhitespaceName),
).rejects.toThrow('"name" is not allowed to be empty');
});

test('should trim leading and trailing whitespace from names', async () => {
const { accessService } = getSetup();
const withUntrimmedName: IRoleValidation = {
name: ' untrimmed ',
description: 'description',
permissions: [],
};
expect(await accessService.validateRole(withUntrimmedName)).toEqual({
name: 'untrimmed',
description: 'description',
permissions: [],
});
});

test('should complete environment field of permissions when not present', async () => {
const { accessService } = getSetup();
const withoutEnvironmentInPermissions: IRoleValidation = {
Expand Down

0 comments on commit 5a874df

Please sign in to comment.