refactor: factor out JSON schema validation function getter
[e-mobility-charging-stations-simulator.git] / src / charging-station / ocpp / OCPPRequestService.ts
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,