From 8773ddebe3bce84b77278b352e202d0e2e89edf8 Mon Sep 17 00:00:00 2001 From: dakkar Date: Thu, 24 Jul 2025 16:52:36 +0100 Subject: [PATCH] build role schema validators early there is really no good reason to build them on-demand also, this way, if the schema itself is invalid, we get the error thrown at startup, not the first time we try to use one of the role endpoints --- packages/backend/src/core/RoleService.ts | 28 ++++++++---------------- 1 file changed, 9 insertions(+), 19 deletions(-) diff --git a/packages/backend/src/core/RoleService.ts b/packages/backend/src/core/RoleService.ts index d59e2b72ad..0e33fd065f 100644 --- a/packages/backend/src/core/RoleService.ts +++ b/packages/backend/src/core/RoleService.ts @@ -186,7 +186,7 @@ const RoleSchema: JSONSchemaType = { [policy, value]: [string, JSONSchemaType['properties']['canTrend']] ) => { return [ - policy as string, + policy, { type: 'object', additionalProperties: false, @@ -252,6 +252,14 @@ export class RoleService implements OnApplicationShutdown, OnModuleInit { // TODO additional cache for final calculation? this.redisForSub.on('message', this.onMessage); + + // this is copied from server/api/endpoint-base.ts + const ajv = new Ajv.default({ + useDefaults: true, + allErrors: true, + }); + this.defaultPoliciesValidator = ajv.compile(DefaultPoliciesSchema); + this.roleValidator = ajv.compile(RoleSchema); } @bindThis @@ -937,15 +945,6 @@ export class RoleService implements OnApplicationShutdown, OnModuleInit { public assertValidRole(role: Partial): void { if (!role.policies) return; - if (!this.roleValidator) { - // this is copied from server/api/endpoint-base.ts - const ajv = new Ajv.default({ - useDefaults: true, - allErrors: true, - }); - this.roleValidator = ajv.compile(RoleSchema); - } - if (this.roleValidator(role.policies)) return; throw new IdentifiableError( @@ -958,15 +957,6 @@ export class RoleService implements OnApplicationShutdown, OnModuleInit { @bindThis public assertValidDefaultPolicies(policies: object): void { - if (!this.defaultPoliciesValidator) { - // this is copied from server/api/endpoint-base.ts - const ajv = new Ajv.default({ - useDefaults: true, - allErrors: true, - }); - this.defaultPoliciesValidator = ajv.compile(DefaultPoliciesSchema); - } - if (this.defaultPoliciesValidator(policies)) return; throw new IdentifiableError(