AjvErrorsToErrorType -> ajvErrorsToErrorType
[e-mobility-charging-stations-simulator.git] / src / charging-station / ocpp / OCPPRequestService.ts
index d03e64ec067759a7ed0fefe8f0ce7fc40bdb48eb..1ae25a65329107bc31b0ade928d3507b54375822 100644 (file)
@@ -1,4 +1,14 @@
-import { ErrorResponse, Response } from '../../types/ocpp/Responses';
+import { JSONSchemaType } from 'ajv';
+import Ajv from 'ajv-draft-04';
+import ajvFormats from 'ajv-formats';
+
+import OCPPError from '../../exception/OCPPError';
+import PerformanceStatistics from '../../performance/PerformanceStatistics';
+import { EmptyObject } from '../../types/EmptyObject';
+import { HandleErrorParams } from '../../types/Error';
+import { JsonObject, JsonType } from '../../types/JsonType';
+import { ErrorType } from '../../types/ocpp/ErrorType';
+import { MessageType } from '../../types/ocpp/MessageType';
 import {
   IncomingRequestCommand,
   OutgoingRequest,
@@ -6,22 +16,19 @@ import {
   RequestParams,
   ResponseType,
 } from '../../types/ocpp/Requests';
-import { JsonObject, JsonType } from '../../types/JsonType';
-
-import type ChargingStation from '../ChargingStation';
+import { ErrorResponse, Response } from '../../types/ocpp/Responses';
 import Constants from '../../utils/Constants';
-import { EmptyObject } from '../../types/EmptyObject';
-import { ErrorType } from '../../types/ocpp/ErrorType';
-import { HandleErrorParams } from '../../types/Error';
-import { MessageType } from '../../types/ocpp/MessageType';
-import OCPPError from '../../exception/OCPPError';
-import type OCPPResponseService from './OCPPResponseService';
-import PerformanceStatistics from '../../performance/PerformanceStatistics';
-import Utils from '../../utils/Utils';
 import logger from '../../utils/Logger';
+import Utils from '../../utils/Utils';
+import type ChargingStation from '../ChargingStation';
+import type OCPPResponseService from './OCPPResponseService';
+import { OCPPServiceUtils } from './OCPPServiceUtils';
+
+const moduleName = 'OCPPRequestService';
 
 export default abstract class OCPPRequestService {
   private static instance: OCPPRequestService | null = null;
+  private ajv: Ajv;
 
   private readonly ocppResponseService: OCPPResponseService;
 
@@ -30,6 +37,8 @@ export default abstract class OCPPRequestService {
     this.requestHandler.bind(this);
     this.sendResponse.bind(this);
     this.sendError.bind(this);
+    this.ajv = new Ajv();
+    ajvFormats(this.ajv);
   }
 
   public static getInstance<T extends OCPPRequestService>(
@@ -106,6 +115,31 @@ export default abstract class OCPPRequestService {
     }
   }
 
+  protected validateRequestPayload<T extends JsonType>(
+    chargingStation: ChargingStation,
+    commandName: RequestCommand,
+    schema: JSONSchemaType<T>,
+    payload: T
+  ): boolean {
+    if (!chargingStation.getPayloadSchemaValidation()) {
+      return true;
+    }
+    const validate = this.ajv.compile(schema);
+    if (validate(payload)) {
+      return true;
+    }
+    logger.error(
+      `${chargingStation.logPrefix()} ${moduleName}.validateRequestPayload: Request PDU is invalid: %j`,
+      validate.errors
+    );
+    throw new OCPPError(
+      OCPPServiceUtils.ajvErrorsToErrorType(validate.errors),
+      'Request PDU is invalid',
+      commandName,
+      JSON.stringify(validate.errors, null, 2)
+    );
+  }
+
   private async internalSendMessage(
     chargingStation: ChargingStation,
     messageId: string,
@@ -254,7 +288,7 @@ export default abstract class OCPPRequestService {
     }
     throw new OCPPError(
       ErrorType.SECURITY_ERROR,
-      `Cannot send command ${commandName} payload when the charging station is in ${chargingStation.getRegistrationStatus()} state on the central server`,
+      `Cannot send command ${commandName} PDU when the charging station is in ${chargingStation.getRegistrationStatus()} state on the central server`,
       commandName
     );
   }