refactor(simulator): cleanup JSON type usage in OCPP payload validation
[e-mobility-charging-stations-simulator.git] / src / charging-station / ocpp / OCPPRequestService.ts
index 18aa5ffdb3cdbeee1b033521128cfc7f161d4aab..b95e9d239e6864701b41166a47b6a0d19e81094d 100644 (file)
@@ -1,4 +1,4 @@
-import Ajv, { type JSONSchemaType } from 'ajv';
+import Ajv, { type JSONSchemaType, type ValidateFunction } from 'ajv';
 import ajvFormats from 'ajv-formats';
 
 import { OCPPConstants } from './OCPPConstants';
@@ -44,7 +44,8 @@ export abstract class OCPPRequestService {
   private readonly version: OCPPVersion;
   private readonly ajv: Ajv;
   private readonly ocppResponseService: OCPPResponseService;
-  protected abstract jsonSchemas: Map<RequestCommand, JSONSchemaType<JsonObject>>;
+  private readonly jsonValidateFunctions: Map<RequestCommand, ValidateFunction<JsonType>>;
+  protected abstract jsonSchemas: Map<RequestCommand, JSONSchemaType<JsonType>>;
 
   protected constructor(version: OCPPVersion, ocppResponseService: OCPPResponseService) {
     this.version = version;
@@ -53,6 +54,7 @@ export abstract class OCPPRequestService {
       multipleOfPrecision: 2,
     });
     ajvFormats(this.ajv);
+    this.jsonValidateFunctions = new Map<RequestCommand, ValidateFunction<JsonType>>();
     this.ocppResponseService = ocppResponseService;
     this.requestHandler = this.requestHandler.bind(this) as <
       // eslint-disable-next-line @typescript-eslint/no-unused-vars
@@ -100,14 +102,14 @@ export abstract class OCPPRequestService {
       responseCallback: ResponseCallback,
       errorCallback: ErrorCallback,
     ) => string;
-    this.validateRequestPayload = this.validateRequestPayload.bind(this) as <T extends JsonObject>(
+    this.validateRequestPayload = this.validateRequestPayload.bind(this) as <T extends JsonType>(
       chargingStation: ChargingStation,
       commandName: RequestCommand | IncomingRequestCommand,
       payload: T,
     ) => boolean;
     this.validateIncomingRequestResponsePayload = this.validateIncomingRequestResponsePayload.bind(
       this,
-    ) as <T extends JsonObject>(
+    ) as <T extends JsonType>(
       chargingStation: ChargingStation,
       commandName: RequestCommand | IncomingRequestCommand,
       payload: T,
@@ -196,7 +198,7 @@ export abstract class OCPPRequestService {
     }
   }
 
-  private validateRequestPayload<T extends JsonObject>(
+  private validateRequestPayload<T extends JsonType>(
     chargingStation: ChargingStation,
     commandName: RequestCommand | IncomingRequestCommand,
     payload: T,
@@ -210,7 +212,13 @@ export abstract class OCPPRequestService {
       );
       return true;
     }
-    const validate = this.ajv.compile(this.jsonSchemas.get(commandName as RequestCommand)!);
+    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)!;
     payload = cloneObject<T>(payload);
     OCPPServiceUtils.convertDateToISOString<T>(payload);
     if (validate(payload)) {
@@ -229,7 +237,7 @@ export abstract class OCPPRequestService {
     );
   }
 
-  private validateIncomingRequestResponsePayload<T extends JsonObject>(
+  private validateIncomingRequestResponsePayload<T extends JsonType>(
     chargingStation: ChargingStation,
     commandName: RequestCommand | IncomingRequestCommand,
     payload: T,
@@ -247,11 +255,25 @@ export abstract class OCPPRequestService {
       );
       return true;
     }
-    const validate = this.ajv.compile(
-      this.ocppResponseService.jsonIncomingRequestResponseSchemas.get(
+    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(
+      commandName as IncomingRequestCommand,
+    )!;
     payload = cloneObject<T>(payload);
     OCPPServiceUtils.convertDateToISOString<T>(payload);
     if (validate(payload)) {
@@ -453,7 +475,7 @@ export abstract class OCPPRequestService {
       // Request
       case MessageType.CALL_MESSAGE:
         // Build request
-        this.validateRequestPayload(chargingStation, commandName, messagePayload as JsonObject);
+        this.validateRequestPayload(chargingStation, commandName, messagePayload as JsonType);
         chargingStation.requests.set(messageId, [
           responseCallback,
           errorCallback,
@@ -473,7 +495,7 @@ export abstract class OCPPRequestService {
         this.validateIncomingRequestResponsePayload(
           chargingStation,
           commandName,
-          messagePayload as JsonObject,
+          messagePayload as JsonType,
         );
         messageToSend = JSON.stringify([messageType, messageId, messagePayload] as Response);
         break;