From: Jérôme Benoit Date: Wed, 15 Nov 2023 20:12:53 +0000 (+0100) Subject: refactor: factor out JSON schema validation function getter X-Git-Tag: v1.2.25~24 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=0b0ca54f0a74240531646827584b7c2738aa7ea1;p=e-mobility-charging-stations-simulator.git refactor: factor out JSON schema validation function getter Signed-off-by: Jérôme Benoit --- diff --git a/src/charging-station/ocpp/OCPPIncomingRequestService.ts b/src/charging-station/ocpp/OCPPIncomingRequestService.ts index 28de5077..4b27f9d8 100644 --- a/src/charging-station/ocpp/OCPPIncomingRequestService.ts +++ b/src/charging-station/ocpp/OCPPIncomingRequestService.ts @@ -92,10 +92,7 @@ export abstract class OCPPIncomingRequestService extends AsyncResource { if (chargingStation.stationInfo?.ocppStrictCompliance === false) { return true; } - if (this.jsonValidateFunctions.has(commandName) === false) { - this.jsonValidateFunctions.set(commandName, this.ajv.compile(schema).bind(this)); - } - const validate = this.jsonValidateFunctions.get(commandName)!; + const validate = this.getJsonIncomingRequestValidateFunction(commandName, schema); if (validate(payload)) { return true; } @@ -118,6 +115,16 @@ export abstract class OCPPIncomingRequestService extends AsyncResource { return OCPPConstants.OCPP_RESPONSE_REJECTED; } + private getJsonIncomingRequestValidateFunction( + commandName: IncomingRequestCommand, + schema: JSONSchemaType, + ) { + if (this.jsonValidateFunctions.has(commandName) === false) { + this.jsonValidateFunctions.set(commandName, this.ajv.compile(schema).bind(this)); + } + return this.jsonValidateFunctions.get(commandName)!; + } + // eslint-disable-next-line @typescript-eslint/no-unused-vars public abstract incomingRequestHandler( chargingStation: ChargingStation, diff --git a/src/charging-station/ocpp/OCPPRequestService.ts b/src/charging-station/ocpp/OCPPRequestService.ts index 98c30811..69fb503e 100644 --- a/src/charging-station/ocpp/OCPPRequestService.ts +++ b/src/charging-station/ocpp/OCPPRequestService.ts @@ -212,13 +212,7 @@ export abstract class OCPPRequestService { ); return true; } - if (this.jsonValidateFunctions.has(commandName as RequestCommand) === false) { - this.jsonValidateFunctions.set( - commandName as RequestCommand, - this.ajv.compile(this.jsonSchemas.get(commandName as RequestCommand)!).bind(this), - ); - } - const validate = this.jsonValidateFunctions.get(commandName as RequestCommand)!; + const validate = this.getJsonRequestValidateFunction(commandName as RequestCommand); payload = cloneObject(payload); OCPPServiceUtils.convertDateToISOString(payload); if (validate(payload)) { @@ -237,6 +231,16 @@ export abstract class OCPPRequestService { ); } + private getJsonRequestValidateFunction(commandName: RequestCommand) { + if (this.jsonValidateFunctions.has(commandName) === false) { + this.jsonValidateFunctions.set( + commandName, + this.ajv.compile(this.jsonSchemas.get(commandName)!).bind(this), + ); + } + return this.jsonValidateFunctions.get(commandName)!; + } + private validateIncomingRequestResponsePayload( chargingStation: ChargingStation, commandName: RequestCommand | IncomingRequestCommand, @@ -255,25 +259,9 @@ export abstract class OCPPRequestService { ); return true; } - if ( - this.ocppResponseService.jsonIncomingRequestResponseValidateFunctions.has( - commandName as IncomingRequestCommand, - ) === false - ) { - this.ocppResponseService.jsonIncomingRequestResponseValidateFunctions.set( - commandName as IncomingRequestCommand, - this.ajv - .compile( - this.ocppResponseService.jsonIncomingRequestResponseSchemas.get( - commandName as IncomingRequestCommand, - )!, - ) - .bind(this), - ); - } - const validate = this.ocppResponseService.jsonIncomingRequestResponseValidateFunctions.get( + const validate = this.getJsonRequestResponseValidateFunction( commandName as IncomingRequestCommand, - )!; + ); payload = cloneObject(payload); OCPPServiceUtils.convertDateToISOString(payload); if (validate(payload)) { @@ -292,6 +280,23 @@ export abstract class OCPPRequestService { ); } + private getJsonRequestResponseValidateFunction( + commandName: IncomingRequestCommand, + ) { + if ( + this.ocppResponseService.jsonIncomingRequestResponseValidateFunctions.has(commandName) === + false + ) { + this.ocppResponseService.jsonIncomingRequestResponseValidateFunctions.set( + commandName, + this.ajv + .compile(this.ocppResponseService.jsonIncomingRequestResponseSchemas.get(commandName)!) + .bind(this), + ); + } + return this.ocppResponseService.jsonIncomingRequestResponseValidateFunctions.get(commandName)!; + } + private async internalSendMessage( chargingStation: ChargingStation, messageId: string, diff --git a/src/charging-station/ocpp/OCPPResponseService.ts b/src/charging-station/ocpp/OCPPResponseService.ts index 0aedbb1e..b73fee58 100644 --- a/src/charging-station/ocpp/OCPPResponseService.ts +++ b/src/charging-station/ocpp/OCPPResponseService.ts @@ -71,10 +71,7 @@ export abstract class OCPPResponseService { if (chargingStation.stationInfo?.ocppStrictCompliance === false) { return true; } - if (this.jsonRequestValidateFunctions.has(commandName) === false) { - this.jsonRequestValidateFunctions.set(commandName, this.ajv.compile(schema).bind(this)); - } - const validate = this.jsonRequestValidateFunctions.get(commandName)!; + const validate = this.getJsonRequestValidateFunction(commandName, schema); if (validate(payload)) { return true; } @@ -94,6 +91,16 @@ export abstract class OCPPResponseService { /* This is intentional */ } + private getJsonRequestValidateFunction( + commandName: RequestCommand, + schema: JSONSchemaType, + ) { + if (this.jsonRequestValidateFunctions.has(commandName) === false) { + this.jsonRequestValidateFunctions.set(commandName, this.ajv.compile(schema).bind(this)); + } + return this.jsonRequestValidateFunctions.get(commandName)!; + } + public abstract responseHandler( chargingStation: ChargingStation, commandName: RequestCommand, diff --git a/ui/web/pnpm-lock.yaml b/ui/web/pnpm-lock.yaml index 64373176..8db2db1c 100644 --- a/ui/web/pnpm-lock.yaml +++ b/ui/web/pnpm-lock.yaml @@ -1390,7 +1390,7 @@ packages: hasBin: true dependencies: caniuse-lite: 1.0.30001562 - electron-to-chromium: 1.4.582 + electron-to-chromium: 1.4.583 node-releases: 2.0.13 update-browserslist-db: 1.0.13(browserslist@4.22.1) dev: true @@ -1712,8 +1712,8 @@ packages: resolution: {integrity: sha512-WMwm9LhRUo+WUaRN+vRuETqG89IgZphVSNkdFgeb6sS/E4OrDIN7t48CAewSHXc6C8lefD8KKfr5vY61brQlow==} dev: false - /electron-to-chromium@1.4.582: - resolution: {integrity: sha512-89o0MGoocwYbzqUUjc+VNpeOFSOK9nIdC5wY4N+PVUarUK0MtjyTjks75AZS2bW4Kl8MdewdFsWaH0jLy+JNoA==} + /electron-to-chromium@1.4.583: + resolution: {integrity: sha512-93y1gcONABZ7uqYe/JWDVQP/Pj/sQSunF0HVAPdlg/pfBnOyBMLlQUxWvkqcljJg1+W6cjvPuYD+r1Th9Tn8mA==} dev: true /emoji-regex@8.0.0: @@ -3011,7 +3011,7 @@ packages: acorn: 8.11.2 pathe: 1.1.1 pkg-types: 1.0.3 - ufo: 1.3.1 + ufo: 1.3.2 dev: true /ms@2.0.0: @@ -3837,8 +3837,8 @@ packages: engines: {node: '>=14.17'} hasBin: true - /ufo@1.3.1: - resolution: {integrity: sha512-uY/99gMLIOlJPwATcMVYfqDSxUR9//AUcgZMzwfSTJPDKzA1S8mX4VLqa+fiAtveraQUBCz4FFcwVZBGbwBXIw==} + /ufo@1.3.2: + resolution: {integrity: sha512-o+ORpgGwaYQXgqGDwd+hkS4PuZ3QnmqMMxRuajK/a38L6fTpcE5GPIfrf+L/KemFzfUpeUQc1rRS1iDBozvnFA==} dev: true /unbox-primitive@1.0.2: