refactor(simulator): cleanup JSON type usage in OCPP payload validation
authorJérôme Benoit <jerome.benoit@sap.com>
Tue, 7 Nov 2023 19:10:37 +0000 (20:10 +0100)
committerJérôme Benoit <jerome.benoit@sap.com>
Tue, 7 Nov 2023 19:10:37 +0000 (20:10 +0100)
     code

Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts
src/charging-station/ocpp/1.6/OCPP16RequestService.ts
src/charging-station/ocpp/1.6/OCPP16ResponseService.ts
src/charging-station/ocpp/2.0/OCPP20IncomingRequestService.ts
src/charging-station/ocpp/2.0/OCPP20RequestService.ts
src/charging-station/ocpp/2.0/OCPP20ResponseService.ts
src/charging-station/ocpp/OCPPIncomingRequestService.ts
src/charging-station/ocpp/OCPPRequestService.ts
src/charging-station/ocpp/OCPPResponseService.ts

index 4b565eb937d27728d7f98b02ab5f88ae3138d906..1f1366fa8a4e7416084687062fa6301ee8fba6d4 100644 (file)
@@ -35,7 +35,6 @@ import {
   type GetDiagnosticsRequest,
   type GetDiagnosticsResponse,
   type IncomingRequestHandler,
-  type JsonObject,
   type JsonType,
   OCPP16AuthorizationStatus,
   OCPP16AvailabilityType,
@@ -109,7 +108,7 @@ import { OCPPIncomingRequestService } from '../OCPPIncomingRequestService';
 const moduleName = 'OCPP16IncomingRequestService';
 
 export class OCPP16IncomingRequestService extends OCPPIncomingRequestService {
-  protected jsonSchemas: Map<OCPP16IncomingRequestCommand, JSONSchemaType<JsonObject>>;
+  protected jsonSchemas: Map<OCPP16IncomingRequestCommand, JSONSchemaType<JsonType>>;
   private incomingRequestHandlers: Map<OCPP16IncomingRequestCommand, IncomingRequestHandler>;
 
   public constructor() {
@@ -187,7 +186,7 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService {
         this.handleRequestCancelReservation.bind(this) as unknown as IncomingRequestHandler,
       ],
     ]);
-    this.jsonSchemas = new Map<OCPP16IncomingRequestCommand, JSONSchemaType<JsonObject>>([
+    this.jsonSchemas = new Map<OCPP16IncomingRequestCommand, JSONSchemaType<JsonType>>([
       [
         OCPP16IncomingRequestCommand.RESET,
         OCPP16ServiceUtils.parseJsonSchemaFile<ResetRequest>(
index 5c1c129f6b671a96d22207f8aa4c6620a88ab7d1..d731071df7fb7c1ce7db2b730c527dd6e34b5e8f 100644 (file)
@@ -32,14 +32,14 @@ import type { OCPPResponseService } from '../OCPPResponseService';
 const moduleName = 'OCPP16RequestService';
 
 export class OCPP16RequestService extends OCPPRequestService {
-  protected jsonSchemas: Map<OCPP16RequestCommand, JSONSchemaType<JsonObject>>;
+  protected jsonSchemas: Map<OCPP16RequestCommand, JSONSchemaType<JsonType>>;
 
   public constructor(ocppResponseService: OCPPResponseService) {
     // if (new.target?.name === moduleName) {
     //   throw new TypeError(`Cannot construct ${new.target?.name} instances directly`);
     // }
     super(OCPPVersion.VERSION_16, ocppResponseService);
-    this.jsonSchemas = new Map<OCPP16RequestCommand, JSONSchemaType<JsonObject>>([
+    this.jsonSchemas = new Map<OCPP16RequestCommand, JSONSchemaType<JsonType>>([
       [
         OCPP16RequestCommand.AUTHORIZE,
         OCPP16ServiceUtils.parseJsonSchemaFile<OCPP16AuthorizeRequest>(
index b634562b7da188abecf0d4daa354bd6524a743dc..d2e1937590db905d4107be2a81d7da0860f0e325 100644 (file)
@@ -21,7 +21,6 @@ import {
   type GenericResponse,
   type GetConfigurationResponse,
   type GetDiagnosticsResponse,
-  type JsonObject,
   type JsonType,
   OCPP16AuthorizationStatus,
   type OCPP16AuthorizeRequest,
@@ -68,11 +67,11 @@ const moduleName = 'OCPP16ResponseService';
 export class OCPP16ResponseService extends OCPPResponseService {
   public jsonIncomingRequestResponseSchemas: Map<
     OCPP16IncomingRequestCommand,
-    JSONSchemaType<JsonObject>
+    JSONSchemaType<JsonType>
   >;
 
   private responseHandlers: Map<OCPP16RequestCommand, ResponseHandler>;
-  private jsonSchemas: Map<OCPP16RequestCommand, JSONSchemaType<JsonObject>>;
+  private jsonSchemas: Map<OCPP16RequestCommand, JSONSchemaType<JsonType>>;
 
   public constructor() {
     // if (new.target?.name === moduleName) {
@@ -109,7 +108,7 @@ export class OCPP16ResponseService extends OCPPResponseService {
         this.emptyResponseHandler.bind(this) as ResponseHandler,
       ],
     ]);
-    this.jsonSchemas = new Map<OCPP16RequestCommand, JSONSchemaType<JsonObject>>([
+    this.jsonSchemas = new Map<OCPP16RequestCommand, JSONSchemaType<JsonType>>([
       [
         OCPP16RequestCommand.BOOT_NOTIFICATION,
         OCPP16ServiceUtils.parseJsonSchemaFile<OCPP16BootNotificationResponse>(
index b5790b5697506c4ac3911894e052751d03659ad4..6344d36d01aa607e7aed7f3523c11a1da96f440e 100644 (file)
@@ -8,7 +8,6 @@ import { OCPPError } from '../../../exception';
 import {
   ErrorType,
   type IncomingRequestHandler,
-  type JsonObject,
   type JsonType,
   type OCPP20ClearCacheRequest,
   OCPP20IncomingRequestCommand,
@@ -20,7 +19,7 @@ import { OCPPIncomingRequestService } from '../OCPPIncomingRequestService';
 const moduleName = 'OCPP20IncomingRequestService';
 
 export class OCPP20IncomingRequestService extends OCPPIncomingRequestService {
-  protected jsonSchemas: Map<OCPP20IncomingRequestCommand, JSONSchemaType<JsonObject>>;
+  protected jsonSchemas: Map<OCPP20IncomingRequestCommand, JSONSchemaType<JsonType>>;
   private incomingRequestHandlers: Map<OCPP20IncomingRequestCommand, IncomingRequestHandler>;
 
   public constructor() {
@@ -31,7 +30,7 @@ export class OCPP20IncomingRequestService extends OCPPIncomingRequestService {
     this.incomingRequestHandlers = new Map<OCPP20IncomingRequestCommand, IncomingRequestHandler>([
       [OCPP20IncomingRequestCommand.CLEAR_CACHE, this.handleRequestClearCache.bind(this)],
     ]);
-    this.jsonSchemas = new Map<OCPP20IncomingRequestCommand, JSONSchemaType<JsonObject>>([
+    this.jsonSchemas = new Map<OCPP20IncomingRequestCommand, JSONSchemaType<JsonType>>([
       [
         OCPP20IncomingRequestCommand.CLEAR_CACHE,
         OCPP20ServiceUtils.parseJsonSchemaFile<OCPP20ClearCacheRequest>(
index e75842365345c7b2327bb84a0479e4044e269592..f9f5c2686e3b3115301ed906c1b6f7075f5060c4 100644 (file)
@@ -24,14 +24,14 @@ import type { OCPPResponseService } from '../OCPPResponseService';
 const moduleName = 'OCPP20RequestService';
 
 export class OCPP20RequestService extends OCPPRequestService {
-  protected jsonSchemas: Map<OCPP20RequestCommand, JSONSchemaType<JsonObject>>;
+  protected jsonSchemas: Map<OCPP20RequestCommand, JSONSchemaType<JsonType>>;
 
   public constructor(ocppResponseService: OCPPResponseService) {
     // if (new.target?.name === moduleName) {
     //   throw new TypeError(`Cannot construct ${new.target?.name} instances directly`);
     // }
     super(OCPPVersion.VERSION_20, ocppResponseService);
-    this.jsonSchemas = new Map<OCPP20RequestCommand, JSONSchemaType<JsonObject>>([
+    this.jsonSchemas = new Map<OCPP20RequestCommand, JSONSchemaType<JsonType>>([
       [
         OCPP20RequestCommand.BOOT_NOTIFICATION,
         OCPP20ServiceUtils.parseJsonSchemaFile<OCPP20BootNotificationRequest>(
index cc4370e8ac5965ecdfa960aa26b1908819fdf3db..111b114a34048113c4fdf2a60a77133570fe4405 100644 (file)
@@ -7,7 +7,6 @@ import { type ChargingStation, addConfigurationKey } from '../../../charging-sta
 import { OCPPError } from '../../../exception';
 import {
   ErrorType,
-  type JsonObject,
   type JsonType,
   type OCPP20BootNotificationResponse,
   type OCPP20ClearCacheResponse,
@@ -28,11 +27,11 @@ const moduleName = 'OCPP20ResponseService';
 export class OCPP20ResponseService extends OCPPResponseService {
   public jsonIncomingRequestResponseSchemas: Map<
     OCPP20IncomingRequestCommand,
-    JSONSchemaType<JsonObject>
+    JSONSchemaType<JsonType>
   >;
 
   private responseHandlers: Map<OCPP20RequestCommand, ResponseHandler>;
-  private jsonSchemas: Map<OCPP20RequestCommand, JSONSchemaType<JsonObject>>;
+  private jsonSchemas: Map<OCPP20RequestCommand, JSONSchemaType<JsonType>>;
 
   public constructor() {
     // if (new.target?.name === moduleName) {
@@ -50,7 +49,7 @@ export class OCPP20ResponseService extends OCPPResponseService {
         this.emptyResponseHandler.bind(this) as ResponseHandler,
       ],
     ]);
-    this.jsonSchemas = new Map<OCPP20RequestCommand, JSONSchemaType<JsonObject>>([
+    this.jsonSchemas = new Map<OCPP20RequestCommand, JSONSchemaType<JsonType>>([
       [
         OCPP20RequestCommand.BOOT_NOTIFICATION,
         OCPP20ServiceUtils.parseJsonSchemaFile<OCPP20BootNotificationResponse>(
index 0c5b5c61b05651d7859cd8225edabe7d2c5ef92f..0ccf92fe78990be826da78d6da556df98c00dfc8 100644 (file)
@@ -11,7 +11,6 @@ import type {
   ClearCacheResponse,
   HandleErrorParams,
   IncomingRequestCommand,
-  JsonObject,
   JsonType,
   OCPPVersion,
 } from '../../types';
@@ -23,8 +22,8 @@ export abstract class OCPPIncomingRequestService extends AsyncResource {
   private static instance: OCPPIncomingRequestService | null = null;
   private readonly version: OCPPVersion;
   private readonly ajv: Ajv;
-  private jsonValidateFunctions: Map<IncomingRequestCommand, ValidateFunction<JsonObject>>;
-  protected abstract jsonSchemas: Map<IncomingRequestCommand, JSONSchemaType<JsonObject>>;
+  private jsonValidateFunctions: Map<IncomingRequestCommand, ValidateFunction<JsonType>>;
+  protected abstract jsonSchemas: Map<IncomingRequestCommand, JSONSchemaType<JsonType>>;
 
   protected constructor(version: OCPPVersion) {
     super(moduleName);
@@ -34,7 +33,7 @@ export abstract class OCPPIncomingRequestService extends AsyncResource {
       multipleOfPrecision: 2,
     });
     ajvFormats(this.ajv);
-    this.jsonValidateFunctions = new Map<IncomingRequestCommand, ValidateFunction<JsonObject>>();
+    this.jsonValidateFunctions = new Map<IncomingRequestCommand, ValidateFunction<JsonType>>();
     this.incomingRequestHandler = this.incomingRequestHandler.bind(this) as <
       ReqType extends JsonType,
       // eslint-disable-next-line @typescript-eslint/no-unused-vars
@@ -94,7 +93,7 @@ export abstract class OCPPIncomingRequestService extends AsyncResource {
       return true;
     }
     if (this.jsonValidateFunctions.has(commandName) === false) {
-      this.jsonValidateFunctions.set(commandName, this.ajv.compile<JsonObject>(schema).bind(this));
+      this.jsonValidateFunctions.set(commandName, this.ajv.compile<T>(schema).bind(this));
     }
     const validate = this.jsonValidateFunctions.get(commandName)!;
     if (validate(payload)) {
index 52a8a6e5246eb5f06df450f4ed320589129bf57b..b95e9d239e6864701b41166a47b6a0d19e81094d 100644 (file)
@@ -44,8 +44,8 @@ export abstract class OCPPRequestService {
   private readonly version: OCPPVersion;
   private readonly ajv: Ajv;
   private readonly ocppResponseService: OCPPResponseService;
-  private readonly jsonValidateFunctions: Map<RequestCommand, ValidateFunction<JsonObject>>;
-  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;
@@ -54,7 +54,7 @@ export abstract class OCPPRequestService {
       multipleOfPrecision: 2,
     });
     ajvFormats(this.ajv);
-    this.jsonValidateFunctions = new Map<RequestCommand, ValidateFunction<JsonObject>>();
+    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
@@ -102,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,
@@ -198,7 +198,7 @@ export abstract class OCPPRequestService {
     }
   }
 
-  private validateRequestPayload<T extends JsonObject>(
+  private validateRequestPayload<T extends JsonType>(
     chargingStation: ChargingStation,
     commandName: RequestCommand | IncomingRequestCommand,
     payload: T,
@@ -215,7 +215,7 @@ export abstract class OCPPRequestService {
     if (this.jsonValidateFunctions.has(commandName as RequestCommand) === false) {
       this.jsonValidateFunctions.set(
         commandName as RequestCommand,
-        this.ajv.compile(this.jsonSchemas.get(commandName as RequestCommand)!).bind(this),
+        this.ajv.compile<T>(this.jsonSchemas.get(commandName as RequestCommand)!).bind(this),
       );
     }
     const validate = this.jsonValidateFunctions.get(commandName as RequestCommand)!;
@@ -237,7 +237,7 @@ export abstract class OCPPRequestService {
     );
   }
 
-  private validateIncomingRequestResponsePayload<T extends JsonObject>(
+  private validateIncomingRequestResponsePayload<T extends JsonType>(
     chargingStation: ChargingStation,
     commandName: RequestCommand | IncomingRequestCommand,
     payload: T,
@@ -263,7 +263,7 @@ export abstract class OCPPRequestService {
       this.ocppResponseService.jsonIncomingRequestResponseValidateFunctions.set(
         commandName as IncomingRequestCommand,
         this.ajv
-          .compile(
+          .compile<T>(
             this.ocppResponseService.jsonIncomingRequestResponseSchemas.get(
               commandName as IncomingRequestCommand,
             )!,
@@ -475,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,
@@ -495,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;
index 46d12fb048aa7374972316451972a4090aef59a2..90f92169cd4c76f8a49f37fc3635a4778689cd86 100644 (file)
@@ -4,13 +4,7 @@ import ajvFormats from 'ajv-formats';
 import { OCPPServiceUtils } from './OCPPServiceUtils';
 import type { ChargingStation } from '../../charging-station';
 import { OCPPError } from '../../exception';
-import type {
-  IncomingRequestCommand,
-  JsonObject,
-  JsonType,
-  OCPPVersion,
-  RequestCommand,
-} from '../../types';
+import type { IncomingRequestCommand, JsonType, OCPPVersion, RequestCommand } from '../../types';
 import { logger } from '../../utils';
 
 const moduleName = 'OCPPResponseService';
@@ -20,16 +14,16 @@ export abstract class OCPPResponseService {
 
   public jsonIncomingRequestResponseValidateFunctions: Map<
     IncomingRequestCommand,
-    ValidateFunction<JsonObject>
+    ValidateFunction<JsonType>
   >;
 
   private readonly version: OCPPVersion;
   private readonly ajv: Ajv;
-  private jsonRequestValidateFunctions: Map<RequestCommand, ValidateFunction<JsonObject>>;
+  private jsonRequestValidateFunctions: Map<RequestCommand, ValidateFunction<JsonType>>;
 
   public abstract jsonIncomingRequestResponseSchemas: Map<
     IncomingRequestCommand,
-    JSONSchemaType<JsonObject>
+    JSONSchemaType<JsonType>
   >;
 
   protected constructor(version: OCPPVersion) {
@@ -39,10 +33,10 @@ export abstract class OCPPResponseService {
       multipleOfPrecision: 2,
     });
     ajvFormats(this.ajv);
-    this.jsonRequestValidateFunctions = new Map<RequestCommand, ValidateFunction<JsonObject>>();
+    this.jsonRequestValidateFunctions = new Map<RequestCommand, ValidateFunction<JsonType>>();
     this.jsonIncomingRequestResponseValidateFunctions = new Map<
       IncomingRequestCommand,
-      ValidateFunction<JsonObject>
+      ValidateFunction<JsonType>
     >();
     this.responseHandler = this.responseHandler.bind(this) as <
       ReqType extends JsonType,
@@ -80,7 +74,7 @@ export abstract class OCPPResponseService {
     if (this.jsonRequestValidateFunctions.has(commandName) === false) {
       this.jsonRequestValidateFunctions.set(
         commandName,
-        this.ajv.compile<JsonObject>(schema).bind(this),
+        this.ajv.compile<JsonType>(schema).bind(this),
       );
     }
     const validate = this.jsonRequestValidateFunctions.get(commandName)!;