diff --git a/src/lib/schema/role-schema.ts b/src/lib/schema/role-schema.ts index 4946efc90631..572e6cac93b5 100644 --- a/src/lib/schema/role-schema.ts +++ b/src/lib/schema/role-schema.ts @@ -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() diff --git a/src/lib/services/access-service.test.ts b/src/lib/services/access-service.test.ts index ac170a008695..5bfa3f229d3b 100644 --- a/src/lib/services/access-service.test.ts +++ b/src/lib/services/access-service.test.ts @@ -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 = {