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