Factor out in helpers PDU payload validation
authorJérôme Benoit <jerome.benoit@sap.com>
Sat, 20 Aug 2022 13:35:20 +0000 (15:35 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Sat, 20 Aug 2022 13:35:20 +0000 (15:35 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
package-lock.json
package.json
src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts
src/charging-station/ocpp/1.6/OCPP16RequestService.ts
src/charging-station/ocpp/1.6/OCPP16ResponseService.ts

index f82c80613893d00b55e24367a628cc02bcc5977b..c56b0ebe093b8e8695755d422da17ba759488260 100644 (file)
@@ -39,7 +39,7 @@
         "@types/express": "^4.17.13",
         "@types/mocha": "^9.1.1",
         "@types/mochawesome": "^6.2.1",
-        "@types/node": "^18.7.6",
+        "@types/node": "^18.7.8",
         "@types/proper-lockfile": "^4.1.2",
         "@types/tar": "^6.1.2",
         "@types/uuid": "^8.3.4",
       }
     },
     "node_modules/@types/node": {
-      "version": "18.7.6",
-      "resolved": "https://registry.npmjs.org/@types/node/-/node-18.7.6.tgz",
-      "integrity": "sha512-EdxgKRXgYsNITy5mjjXjVE/CS8YENSdhiagGrLqjG0pvA2owgJ6i4l7wy/PFZGC0B1/H20lWKN7ONVDNYDZm7A=="
+      "version": "18.7.8",
+      "resolved": "https://registry.npmjs.org/@types/node/-/node-18.7.8.tgz",
+      "integrity": "sha512-/YP55EMK2341JkODUb8DM9O0x1SIz2aBvyF33Uf1c76St3VpsMXEIW0nxuKkq/5cxnbz0RD9cfwNZHEAZQD3ag=="
     },
     "node_modules/@types/object-path": {
       "version": "0.11.1",
       }
     },
     "node_modules/new-github-release-url/node_modules/type-fest": {
-      "version": "2.18.0",
-      "resolved": "https://registry.npmjs.org/type-fest/-/type-fest-2.18.0.tgz",
-      "integrity": "sha512-pRS+/yrW5TjPPHNOvxhbNZexr2bS63WjrMU8a+VzEBhUi9Tz1pZeD+vQz3ut0svZ46P+SRqMEPnJmk2XnvNzTw==",
+      "version": "2.18.1",
+      "resolved": "https://registry.npmjs.org/type-fest/-/type-fest-2.18.1.tgz",
+      "integrity": "sha512-UKCINsd4qiATXD6OIlnQw9t1ux/n2ld+Nl0kzPbCONhCaUIS/BhJbNw14w6584HCQWf3frBK8vmWnGZq/sbPHQ==",
       "dev": true,
       "engines": {
         "node": ">=12.20"
       }
     },
     "node_modules/release-it/node_modules/boxen/node_modules/type-fest": {
-      "version": "2.18.0",
-      "resolved": "https://registry.npmjs.org/type-fest/-/type-fest-2.18.0.tgz",
-      "integrity": "sha512-pRS+/yrW5TjPPHNOvxhbNZexr2bS63WjrMU8a+VzEBhUi9Tz1pZeD+vQz3ut0svZ46P+SRqMEPnJmk2XnvNzTw==",
+      "version": "2.18.1",
+      "resolved": "https://registry.npmjs.org/type-fest/-/type-fest-2.18.1.tgz",
+      "integrity": "sha512-UKCINsd4qiATXD6OIlnQw9t1ux/n2ld+Nl0kzPbCONhCaUIS/BhJbNw14w6584HCQWf3frBK8vmWnGZq/sbPHQ==",
       "dev": true,
       "engines": {
         "node": ">=12.20"
       }
     },
     "@types/node": {
-      "version": "18.7.6",
-      "resolved": "https://registry.npmjs.org/@types/node/-/node-18.7.6.tgz",
-      "integrity": "sha512-EdxgKRXgYsNITy5mjjXjVE/CS8YENSdhiagGrLqjG0pvA2owgJ6i4l7wy/PFZGC0B1/H20lWKN7ONVDNYDZm7A=="
+      "version": "18.7.8",
+      "resolved": "https://registry.npmjs.org/@types/node/-/node-18.7.8.tgz",
+      "integrity": "sha512-/YP55EMK2341JkODUb8DM9O0x1SIz2aBvyF33Uf1c76St3VpsMXEIW0nxuKkq/5cxnbz0RD9cfwNZHEAZQD3ag=="
     },
     "@types/object-path": {
       "version": "0.11.1",
       },
       "dependencies": {
         "type-fest": {
-          "version": "2.18.0",
-          "resolved": "https://registry.npmjs.org/type-fest/-/type-fest-2.18.0.tgz",
-          "integrity": "sha512-pRS+/yrW5TjPPHNOvxhbNZexr2bS63WjrMU8a+VzEBhUi9Tz1pZeD+vQz3ut0svZ46P+SRqMEPnJmk2XnvNzTw==",
+          "version": "2.18.1",
+          "resolved": "https://registry.npmjs.org/type-fest/-/type-fest-2.18.1.tgz",
+          "integrity": "sha512-UKCINsd4qiATXD6OIlnQw9t1ux/n2ld+Nl0kzPbCONhCaUIS/BhJbNw14w6584HCQWf3frBK8vmWnGZq/sbPHQ==",
           "dev": true
         }
       }
           },
           "dependencies": {
             "type-fest": {
-              "version": "2.18.0",
-              "resolved": "https://registry.npmjs.org/type-fest/-/type-fest-2.18.0.tgz",
-              "integrity": "sha512-pRS+/yrW5TjPPHNOvxhbNZexr2bS63WjrMU8a+VzEBhUi9Tz1pZeD+vQz3ut0svZ46P+SRqMEPnJmk2XnvNzTw==",
+              "version": "2.18.1",
+              "resolved": "https://registry.npmjs.org/type-fest/-/type-fest-2.18.1.tgz",
+              "integrity": "sha512-UKCINsd4qiATXD6OIlnQw9t1ux/n2ld+Nl0kzPbCONhCaUIS/BhJbNw14w6584HCQWf3frBK8vmWnGZq/sbPHQ==",
               "dev": true
             }
           }
index 0e8d3d27c4a344b76b8c46d2cfbfe7626b28d1a9..18ef26acb9523eedd41e5b0944858445f05b5648 100644 (file)
     "@types/express": "^4.17.13",
     "@types/mocha": "^9.1.1",
     "@types/mochawesome": "^6.2.1",
-    "@types/node": "^18.7.6",
+    "@types/node": "^18.7.8",
     "@types/proper-lockfile": "^4.1.2",
     "@types/tar": "^6.1.2",
     "@types/uuid": "^8.3.4",
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,
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;
+  }
 }
index e3e87d4c59d31180e1b0e1a3cbda1c1482c17d68..984cb1b34c09ab53bfe90d518d9c2e86b8c55b3a 100644 (file)
@@ -179,18 +179,7 @@ export default class OCPP16ResponseService extends OCPPResponseService {
         ChargingStationUtils.isRequestCommandSupported(commandName, chargingStation)
       ) {
         try {
-          if (this.jsonSchemas.has(commandName)) {
-            this.validateResponsePayload(
-              chargingStation,
-              commandName,
-              this.jsonSchemas.get(commandName),
-              payload
-            );
-          } else {
-            logger.warn(
-              `${chargingStation.logPrefix()} ${moduleName}.responseHandler: No JSON schema found for command ${commandName} PDU validation`
-            );
-          }
+          this.validatePayload(chargingStation, commandName, payload);
           await this.responseHandlers.get(commandName)(chargingStation, payload, requestPayload);
         } catch (error) {
           logger.error(chargingStation.logPrefix() + ' Handle request response error: %j', error);
@@ -223,6 +212,25 @@ export default class OCPP16ResponseService extends OCPPResponseService {
     }
   }
 
+  private validatePayload(
+    chargingStation: ChargingStation,
+    commandName: OCPP16RequestCommand,
+    payload: JsonType
+  ): boolean {
+    if (this.jsonSchemas.has(commandName)) {
+      return this.validateResponsePayload(
+        chargingStation,
+        commandName,
+        this.jsonSchemas.get(commandName),
+        payload
+      );
+    }
+    logger.warn(
+      `${chargingStation.logPrefix()} ${moduleName}.responseHandler: No JSON schema found for command ${commandName} PDU validation`
+    );
+    return false;
+  }
+
   private handleResponseBootNotification(
     chargingStation: ChargingStation,
     payload: OCPP16BootNotificationResponse