From d5490a13c341153af159d4b2ed745d32dc38452f Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Fri, 26 Jan 2024 00:37:53 +0100 Subject: [PATCH] refactor: cleanup payload validation properties namespace MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- .../ocpp/1.6/OCPP16IncomingRequestService.ts | 9 +++------ .../ocpp/1.6/OCPP16RequestService.ts | 4 ++-- .../ocpp/1.6/OCPP16ResponseService.ts | 10 +++++----- .../ocpp/2.0/OCPP20IncomingRequestService.ts | 9 +++------ .../ocpp/2.0/OCPP20RequestService.ts | 4 ++-- .../ocpp/2.0/OCPP20ResponseService.ts | 10 +++++----- .../ocpp/OCPPIncomingRequestService.ts | 4 ++-- src/charging-station/ocpp/OCPPRequestService.ts | 15 +++++++-------- src/charging-station/ocpp/OCPPResponseService.ts | 6 +++--- 9 files changed, 32 insertions(+), 39 deletions(-) diff --git a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts index 5d6172c4..289f03be 100644 --- a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts @@ -113,10 +113,7 @@ import { OCPPIncomingRequestService } from '../OCPPIncomingRequestService.js' const moduleName = 'OCPP16IncomingRequestService' export class OCPP16IncomingRequestService extends OCPPIncomingRequestService { - protected jsonSchemasValidateFunction: Map< - OCPP16IncomingRequestCommand, - ValidateFunction - > + protected payloadValidateFunctions: Map> private readonly incomingRequestHandlers: Map< OCPP16IncomingRequestCommand, @@ -198,7 +195,7 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService { this.handleRequestCancelReservation.bind(this) as unknown as IncomingRequestHandler ] ]) - this.jsonSchemasValidateFunction = new Map< + this.payloadValidateFunctions = new Map< OCPP16IncomingRequestCommand, ValidateFunction >([ @@ -498,7 +495,7 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService { commandName: OCPP16IncomingRequestCommand, commandPayload: JsonType ): boolean { - if (this.jsonSchemasValidateFunction.has(commandName)) { + if (this.payloadValidateFunctions.has(commandName)) { return this.validateIncomingRequestPayload(chargingStation, commandName, commandPayload) } logger.warn( diff --git a/src/charging-station/ocpp/1.6/OCPP16RequestService.ts b/src/charging-station/ocpp/1.6/OCPP16RequestService.ts index a7bacae9..74c00daa 100644 --- a/src/charging-station/ocpp/1.6/OCPP16RequestService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16RequestService.ts @@ -32,14 +32,14 @@ import type { OCPPResponseService } from '../OCPPResponseService.js' const moduleName = 'OCPP16RequestService' export class OCPP16RequestService extends OCPPRequestService { - protected jsonSchemasValidateFunction: Map> + protected payloadValidateFunctions: Map> public constructor (ocppResponseService: OCPPResponseService) { // if (new.target.name === moduleName) { // throw new TypeError(`Cannot construct ${new.target.name} instances directly`) // } super(OCPPVersion.VERSION_16, ocppResponseService) - this.jsonSchemasValidateFunction = new Map>([ + this.payloadValidateFunctions = new Map>([ [ OCPP16RequestCommand.AUTHORIZE, this.ajv diff --git a/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts b/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts index 3b91fba1..11be8493 100644 --- a/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts @@ -57,12 +57,12 @@ import { OCPPResponseService } from '../OCPPResponseService.js' const moduleName = 'OCPP16ResponseService' export class OCPP16ResponseService extends OCPPResponseService { - public jsonSchemasIncomingRequestResponseValidateFunction: Map< + public incomingRequestResponsePayloadValidateFunctions: Map< OCPP16IncomingRequestCommand, ValidateFunction > - protected jsonSchemasValidateFunction: Map> + protected payloadValidateFunctions: Map> private readonly responseHandlers: Map public constructor () { @@ -97,7 +97,7 @@ export class OCPP16ResponseService extends OCPPResponseService { [OCPP16RequestCommand.DATA_TRANSFER, this.emptyResponseHandler], [OCPP16RequestCommand.FIRMWARE_STATUS_NOTIFICATION, this.emptyResponseHandler] ]) - this.jsonSchemasValidateFunction = new Map>([ + this.payloadValidateFunctions = new Map>([ [ OCPP16RequestCommand.BOOT_NOTIFICATION, this.ajv @@ -219,7 +219,7 @@ export class OCPP16ResponseService extends OCPPResponseService { .bind(this) ] ]) - this.jsonSchemasIncomingRequestResponseValidateFunction = new Map< + this.incomingRequestResponsePayloadValidateFunctions = new Map< OCPP16IncomingRequestCommand, ValidateFunction >([ @@ -485,7 +485,7 @@ export class OCPP16ResponseService extends OCPPResponseService { commandName: OCPP16RequestCommand, payload: JsonType ): boolean { - if (this.jsonSchemasValidateFunction.has(commandName)) { + if (this.payloadValidateFunctions.has(commandName)) { return this.validateResponsePayload(chargingStation, commandName, payload) } logger.warn( diff --git a/src/charging-station/ocpp/2.0/OCPP20IncomingRequestService.ts b/src/charging-station/ocpp/2.0/OCPP20IncomingRequestService.ts index cfc58323..94401593 100644 --- a/src/charging-station/ocpp/2.0/OCPP20IncomingRequestService.ts +++ b/src/charging-station/ocpp/2.0/OCPP20IncomingRequestService.ts @@ -19,10 +19,7 @@ import { OCPPIncomingRequestService } from '../OCPPIncomingRequestService.js' const moduleName = 'OCPP20IncomingRequestService' export class OCPP20IncomingRequestService extends OCPPIncomingRequestService { - protected jsonSchemasValidateFunction: Map< - OCPP20IncomingRequestCommand, - ValidateFunction - > + protected payloadValidateFunctions: Map> private readonly incomingRequestHandlers: Map< OCPP20IncomingRequestCommand, @@ -37,7 +34,7 @@ export class OCPP20IncomingRequestService extends OCPPIncomingRequestService { this.incomingRequestHandlers = new Map([ [OCPP20IncomingRequestCommand.CLEAR_CACHE, this.handleRequestClearCache.bind(this)] ]) - this.jsonSchemasValidateFunction = new Map< + this.payloadValidateFunctions = new Map< OCPP20IncomingRequestCommand, ValidateFunction >([ @@ -145,7 +142,7 @@ export class OCPP20IncomingRequestService extends OCPPIncomingRequestService { commandName: OCPP20IncomingRequestCommand, commandPayload: JsonType ): boolean { - if (this.jsonSchemasValidateFunction.has(commandName)) { + if (this.payloadValidateFunctions.has(commandName)) { return this.validateIncomingRequestPayload(chargingStation, commandName, commandPayload) } logger.warn( diff --git a/src/charging-station/ocpp/2.0/OCPP20RequestService.ts b/src/charging-station/ocpp/2.0/OCPP20RequestService.ts index 20bd333f..a61fc2ae 100644 --- a/src/charging-station/ocpp/2.0/OCPP20RequestService.ts +++ b/src/charging-station/ocpp/2.0/OCPP20RequestService.ts @@ -24,14 +24,14 @@ import type { OCPPResponseService } from '../OCPPResponseService.js' const moduleName = 'OCPP20RequestService' export class OCPP20RequestService extends OCPPRequestService { - protected jsonSchemasValidateFunction: Map> + protected payloadValidateFunctions: Map> public constructor (ocppResponseService: OCPPResponseService) { // if (new.target.name === moduleName) { // throw new TypeError(`Cannot construct ${new.target.name} instances directly`) // } super(OCPPVersion.VERSION_20, ocppResponseService) - this.jsonSchemasValidateFunction = new Map>([ + this.payloadValidateFunctions = new Map>([ [ OCPP20RequestCommand.BOOT_NOTIFICATION, this.ajv diff --git a/src/charging-station/ocpp/2.0/OCPP20ResponseService.ts b/src/charging-station/ocpp/2.0/OCPP20ResponseService.ts index d5b3e7d1..bb12b006 100644 --- a/src/charging-station/ocpp/2.0/OCPP20ResponseService.ts +++ b/src/charging-station/ocpp/2.0/OCPP20ResponseService.ts @@ -25,12 +25,12 @@ import { OCPPResponseService } from '../OCPPResponseService.js' const moduleName = 'OCPP20ResponseService' export class OCPP20ResponseService extends OCPPResponseService { - public jsonSchemasIncomingRequestResponseValidateFunction: Map< + public incomingRequestResponsePayloadValidateFunctions: Map< OCPP20IncomingRequestCommand, ValidateFunction > - protected jsonSchemasValidateFunction: Map> + protected payloadValidateFunctions: Map> private readonly responseHandlers: Map public constructor () { @@ -46,7 +46,7 @@ export class OCPP20ResponseService extends OCPPResponseService { [OCPP20RequestCommand.HEARTBEAT, this.emptyResponseHandler], [OCPP20RequestCommand.STATUS_NOTIFICATION, this.emptyResponseHandler] ]) - this.jsonSchemasValidateFunction = new Map>([ + this.payloadValidateFunctions = new Map>([ [ OCPP20RequestCommand.BOOT_NOTIFICATION, this.ajv @@ -84,7 +84,7 @@ export class OCPP20ResponseService extends OCPPResponseService { .bind(this) ] ]) - this.jsonSchemasIncomingRequestResponseValidateFunction = new Map< + this.incomingRequestResponsePayloadValidateFunctions = new Map< OCPP20IncomingRequestCommand, ValidateFunction >([ @@ -158,7 +158,7 @@ export class OCPP20ResponseService extends OCPPResponseService { commandName: OCPP20RequestCommand, payload: JsonType ): boolean { - if (this.jsonSchemasValidateFunction.has(commandName)) { + if (this.payloadValidateFunctions.has(commandName)) { return this.validateResponsePayload(chargingStation, commandName, payload) } logger.warn( diff --git a/src/charging-station/ocpp/OCPPIncomingRequestService.ts b/src/charging-station/ocpp/OCPPIncomingRequestService.ts index d0ff0229..72035697 100644 --- a/src/charging-station/ocpp/OCPPIncomingRequestService.ts +++ b/src/charging-station/ocpp/OCPPIncomingRequestService.ts @@ -24,7 +24,7 @@ export abstract class OCPPIncomingRequestService { private static instance: OCPPIncomingRequestService | null = null private readonly version: OCPPVersion protected readonly ajv: Ajv - protected abstract jsonSchemasValidateFunction: Map< + protected abstract payloadValidateFunctions: Map< IncomingRequestCommand, ValidateFunction > @@ -77,7 +77,7 @@ export abstract class OCPPIncomingRequestService { if (chargingStation.stationInfo?.ocppStrictCompliance === false) { return true } - const validate = this.jsonSchemasValidateFunction.get(commandName) + const validate = this.payloadValidateFunctions.get(commandName) if (validate?.(payload) === true) { return true } diff --git a/src/charging-station/ocpp/OCPPRequestService.ts b/src/charging-station/ocpp/OCPPRequestService.ts index f956d381..17fc5484 100644 --- a/src/charging-station/ocpp/OCPPRequestService.ts +++ b/src/charging-station/ocpp/OCPPRequestService.ts @@ -47,7 +47,7 @@ export abstract class OCPPRequestService { private readonly version: OCPPVersion private readonly ocppResponseService: OCPPResponseService protected readonly ajv: Ajv - protected abstract jsonSchemasValidateFunction: Map> + protected abstract payloadValidateFunctions: Map> protected constructor (version: OCPPVersion, ocppResponseService: OCPPResponseService) { this.version = version @@ -158,13 +158,13 @@ export abstract class OCPPRequestService { if (chargingStation.stationInfo?.ocppStrictCompliance === false) { return true } - if (!this.jsonSchemasValidateFunction.has(commandName as RequestCommand)) { + if (!this.payloadValidateFunctions.has(commandName as RequestCommand)) { logger.warn( `${chargingStation.logPrefix()} ${moduleName}.validateRequestPayload: No JSON schema found for command '${commandName}' PDU validation` ) return true } - const validate = this.jsonSchemasValidateFunction.get(commandName as RequestCommand) + const validate = this.payloadValidateFunctions.get(commandName as RequestCommand) payload = clone(payload) OCPPServiceUtils.convertDateToISOString(payload) if (validate?.(payload) === true) { @@ -192,7 +192,7 @@ export abstract class OCPPRequestService { return true } if ( - !this.ocppResponseService.jsonSchemasIncomingRequestResponseValidateFunction.has( + !this.ocppResponseService.incomingRequestResponsePayloadValidateFunctions.has( commandName as IncomingRequestCommand ) ) { @@ -201,10 +201,9 @@ export abstract class OCPPRequestService { ) return true } - const validate = - this.ocppResponseService.jsonSchemasIncomingRequestResponseValidateFunction.get( - commandName as IncomingRequestCommand - ) + const validate = this.ocppResponseService.incomingRequestResponsePayloadValidateFunctions.get( + commandName as IncomingRequestCommand + ) payload = clone(payload) OCPPServiceUtils.convertDateToISOString(payload) if (validate?.(payload) === true) { diff --git a/src/charging-station/ocpp/OCPPResponseService.ts b/src/charging-station/ocpp/OCPPResponseService.ts index 45a3e1f2..a526c28c 100644 --- a/src/charging-station/ocpp/OCPPResponseService.ts +++ b/src/charging-station/ocpp/OCPPResponseService.ts @@ -23,8 +23,8 @@ export abstract class OCPPResponseService { private readonly version: OCPPVersion protected readonly ajv: Ajv protected readonly ajvIncomingRequest: Ajv - protected abstract jsonSchemasValidateFunction: Map> - public abstract jsonSchemasIncomingRequestResponseValidateFunction: Map< + protected abstract payloadValidateFunctions: Map> + public abstract incomingRequestResponsePayloadValidateFunctions: Map< IncomingRequestCommand, ValidateFunction > @@ -60,7 +60,7 @@ export abstract class OCPPResponseService { if (chargingStation.stationInfo?.ocppStrictCompliance === false) { return true } - const validate = this.jsonSchemasValidateFunction.get(commandName) + const validate = this.payloadValidateFunctions.get(commandName) if (validate?.(payload) === true) { return true } -- 2.34.1