Factor out in helpers PDU payload validation
[e-mobility-charging-stations-simulator.git] / src / charging-station / ocpp / 1.6 / OCPP16IncomingRequestService.ts
index 3238722568d9cc92bae51cd1476a1e90a10d0b77..6609fa4184ec226d1d75224e9c374a5a7db9e2cb 100644 (file)
@@ -311,18 +311,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
         ChargingStationUtils.isIncomingRequestCommandSupported(commandName, chargingStation)
       ) {
         try {
-          if (this.jsonSchemas.has(commandName)) {
-            this.validateIncomingRequestPayload(
-              chargingStation,
-              commandName,
-              this.jsonSchemas.get(commandName),
-              commandPayload
-            );
-          } else {
-            logger.warn(
-              `${chargingStation.logPrefix()} ${moduleName}.incomingRequestHandler: No JSON schema found for command ${commandName} PDU validation`
-            );
-          }
+          this.validatePayload(chargingStation, commandName, commandPayload);
           // Call the method to build the response
           response = await this.incomingRequestHandlers.get(commandName)(
             chargingStation,
@@ -367,6 +356,25 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
     );
   }
 
+  private validatePayload(
+    chargingStation: ChargingStation,
+    commandName: OCPP16IncomingRequestCommand,
+    commandPayload: JsonType
+  ): boolean {
+    if (this.jsonSchemas.has(commandName)) {
+      return this.validateIncomingRequestPayload(
+        chargingStation,
+        commandName,
+        this.jsonSchemas.get(commandName),
+        commandPayload
+      );
+    }
+    logger.warn(
+      `${chargingStation.logPrefix()} ${moduleName}.incomingRequestHandler: No JSON schema found for command ${commandName} PDU validation`
+    );
+    return false;
+  }
+
   // Simulate charging station restart
   private handleRequestReset(
     chargingStation: ChargingStation,