refactor: factor out JSON schema validation function getter
authorJérôme Benoit <jerome.benoit@sap.com>
Wed, 15 Nov 2023 20:12:53 +0000 (21:12 +0100)
committerJérôme Benoit <jerome.benoit@sap.com>
Wed, 15 Nov 2023 20:12:53 +0000 (21:12 +0100)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/ocpp/OCPPIncomingRequestService.ts
src/charging-station/ocpp/OCPPRequestService.ts
src/charging-station/ocpp/OCPPResponseService.ts
ui/web/pnpm-lock.yaml

index 28de507774a281aeeb0d32b4d17b1348b45d50c5..4b27f9d82f8de91b38bd1a19c4a826ebe6800d6b 100644 (file)
@@ -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<T>(schema).bind(this));
-    }
-    const validate = this.jsonValidateFunctions.get(commandName)!;
+    const validate = this.getJsonIncomingRequestValidateFunction<T>(commandName, schema);
     if (validate(payload)) {
       return true;
     }
@@ -118,6 +115,16 @@ export abstract class OCPPIncomingRequestService extends AsyncResource {
     return OCPPConstants.OCPP_RESPONSE_REJECTED;
   }
 
+  private getJsonIncomingRequestValidateFunction<T extends JsonType>(
+    commandName: IncomingRequestCommand,
+    schema: JSONSchemaType<T>,
+  ) {
+    if (this.jsonValidateFunctions.has(commandName) === false) {
+      this.jsonValidateFunctions.set(commandName, this.ajv.compile<T>(schema).bind(this));
+    }
+    return this.jsonValidateFunctions.get(commandName)!;
+  }
+
   // eslint-disable-next-line @typescript-eslint/no-unused-vars
   public abstract incomingRequestHandler<ReqType extends JsonType, ResType extends JsonType>(
     chargingStation: ChargingStation,
index 98c3081155bd2f350fe3a686c7909cd524166b52..69fb503eaf303beee1378157fce587c25393bc9b 100644 (file)
@@ -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<T>(this.jsonSchemas.get(commandName as RequestCommand)!).bind(this),
-      );
-    }
-    const validate = this.jsonValidateFunctions.get(commandName as RequestCommand)!;
+    const validate = this.getJsonRequestValidateFunction<T>(commandName as RequestCommand);
     payload = cloneObject<T>(payload);
     OCPPServiceUtils.convertDateToISOString<T>(payload);
     if (validate(payload)) {
@@ -237,6 +231,16 @@ export abstract class OCPPRequestService {
     );
   }
 
+  private getJsonRequestValidateFunction<T extends JsonType>(commandName: RequestCommand) {
+    if (this.jsonValidateFunctions.has(commandName) === false) {
+      this.jsonValidateFunctions.set(
+        commandName,
+        this.ajv.compile<T>(this.jsonSchemas.get(commandName)!).bind(this),
+      );
+    }
+    return this.jsonValidateFunctions.get(commandName)!;
+  }
+
   private validateIncomingRequestResponsePayload<T extends JsonType>(
     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<T>(
-            this.ocppResponseService.jsonIncomingRequestResponseSchemas.get(
-              commandName as IncomingRequestCommand,
-            )!,
-          )
-          .bind(this),
-      );
-    }
-    const validate = this.ocppResponseService.jsonIncomingRequestResponseValidateFunctions.get(
+    const validate = this.getJsonRequestResponseValidateFunction<T>(
       commandName as IncomingRequestCommand,
-    )!;
+    );
     payload = cloneObject<T>(payload);
     OCPPServiceUtils.convertDateToISOString<T>(payload);
     if (validate(payload)) {
@@ -292,6 +280,23 @@ export abstract class OCPPRequestService {
     );
   }
 
+  private getJsonRequestResponseValidateFunction<T extends JsonType>(
+    commandName: IncomingRequestCommand,
+  ) {
+    if (
+      this.ocppResponseService.jsonIncomingRequestResponseValidateFunctions.has(commandName) ===
+      false
+    ) {
+      this.ocppResponseService.jsonIncomingRequestResponseValidateFunctions.set(
+        commandName,
+        this.ajv
+          .compile<T>(this.ocppResponseService.jsonIncomingRequestResponseSchemas.get(commandName)!)
+          .bind(this),
+      );
+    }
+    return this.ocppResponseService.jsonIncomingRequestResponseValidateFunctions.get(commandName)!;
+  }
+
   private async internalSendMessage(
     chargingStation: ChargingStation,
     messageId: string,
index 0aedbb1eebb5b6a17a2abe94b2b85641c68c07e9..b73fee584b4ab5f2045d1d81d723655e7e449e39 100644 (file)
@@ -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<T>(schema).bind(this));
-    }
-    const validate = this.jsonRequestValidateFunctions.get(commandName)!;
+    const validate = this.getJsonRequestValidateFunction<T>(commandName, schema);
     if (validate(payload)) {
       return true;
     }
@@ -94,6 +91,16 @@ export abstract class OCPPResponseService {
     /* This is intentional */
   }
 
+  private getJsonRequestValidateFunction<T extends JsonType>(
+    commandName: RequestCommand,
+    schema: JSONSchemaType<T>,
+  ) {
+    if (this.jsonRequestValidateFunctions.has(commandName) === false) {
+      this.jsonRequestValidateFunctions.set(commandName, this.ajv.compile<T>(schema).bind(this));
+    }
+    return this.jsonRequestValidateFunctions.get(commandName)!;
+  }
+
   public abstract responseHandler<ReqType extends JsonType, ResType extends JsonType>(
     chargingStation: ChargingStation,
     commandName: RequestCommand,
index 64373176579354ec2c15020ecd15c3a26910ac27..8db2db1c743b63205d5cfd933d0fc21a7a2d40e9 100644 (file)
@@ -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: