Factor out in helpers PDU payload validation
[e-mobility-charging-stations-simulator.git] / src / charging-station / ocpp / 1.6 / OCPP16RequestService.ts
index 3ff3bdcbddd6758b4020633663928747ba95004a..5a0e0bc1fd2995f951259cf308aec352f4bb79fe 100644 (file)
@@ -154,18 +154,7 @@ export default class OCPP16RequestService extends OCPPRequestService {
         commandName,
         commandParams
       );
-      if (this.jsonSchemas.has(commandName)) {
-        this.validateRequestPayload(
-          chargingStation,
-          commandName,
-          this.jsonSchemas.get(commandName),
-          requestPayload
-        );
-      } else {
-        logger.warn(
-          `${chargingStation.logPrefix()} ${moduleName}.requestHandler: No JSON schema found for command ${commandName} PDU validation`
-        );
-      }
+      this.validatePayload(chargingStation, commandName, requestPayload);
       return (await this.sendMessage(
         chargingStation,
         Utils.generateUUID(),
@@ -278,4 +267,23 @@ export default class OCPP16RequestService extends OCPPRequestService {
         );
     }
   }
+
+  private validatePayload<Request extends JsonType>(
+    chargingStation: ChargingStation,
+    commandName: OCPP16RequestCommand,
+    requestPayload: Request
+  ): boolean {
+    if (this.jsonSchemas.has(commandName)) {
+      return this.validateRequestPayload(
+        chargingStation,
+        commandName,
+        this.jsonSchemas.get(commandName),
+        requestPayload
+      );
+    }
+    logger.warn(
+      `${chargingStation.logPrefix()} ${moduleName}.requestHandler: No JSON schema found for command ${commandName} PDU validation`
+    );
+    return false;
+  }
 }