From 06ad945f66c591d91a4c0062e9f39e007b05ba83 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sat, 20 Aug 2022 15:11:11 +0200 Subject: [PATCH] Add and use helper to convert Ajv JSON schema validation errors to OCPP error type MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- .../ocpp/OCPPIncomingRequestService.ts | 4 ++-- .../ocpp/OCPPRequestService.ts | 3 ++- .../ocpp/OCPPResponseService.ts | 4 ++-- src/charging-station/ocpp/OCPPServiceUtils.ts | 20 +++++++++++++++++++ 4 files changed, 26 insertions(+), 5 deletions(-) diff --git a/src/charging-station/ocpp/OCPPIncomingRequestService.ts b/src/charging-station/ocpp/OCPPIncomingRequestService.ts index d55a7453..ef05c3af 100644 --- a/src/charging-station/ocpp/OCPPIncomingRequestService.ts +++ b/src/charging-station/ocpp/OCPPIncomingRequestService.ts @@ -5,10 +5,10 @@ import ajvFormats from 'ajv-formats'; import OCPPError from '../../exception/OCPPError'; import { HandleErrorParams } from '../../types/Error'; import { JsonType } from '../../types/JsonType'; -import { ErrorType } from '../../types/ocpp/ErrorType'; import { IncomingRequestCommand } from '../../types/ocpp/Requests'; import logger from '../../utils/Logger'; import type ChargingStation from '../ChargingStation'; +import { OCPP16ServiceUtils } from './1.6/OCPP16ServiceUtils'; const moduleName = 'OCPPIncomingRequestService'; @@ -68,7 +68,7 @@ export default abstract class OCPPIncomingRequestService { validate.errors ); throw new OCPPError( - ErrorType.FORMATION_VIOLATION, + OCPP16ServiceUtils.AjvErrorsToErrorType(validate.errors), 'Incoming request PDU is invalid', commandName, JSON.stringify(validate.errors, null, 2) diff --git a/src/charging-station/ocpp/OCPPRequestService.ts b/src/charging-station/ocpp/OCPPRequestService.ts index 74ea7a8d..e4ac7868 100644 --- a/src/charging-station/ocpp/OCPPRequestService.ts +++ b/src/charging-station/ocpp/OCPPRequestService.ts @@ -21,6 +21,7 @@ import Constants from '../../utils/Constants'; import logger from '../../utils/Logger'; import Utils from '../../utils/Utils'; import type ChargingStation from '../ChargingStation'; +import { OCPP16ServiceUtils } from './1.6/OCPP16ServiceUtils'; import type OCPPResponseService from './OCPPResponseService'; const moduleName = 'OCPPRequestService'; @@ -132,7 +133,7 @@ export default abstract class OCPPRequestService { validate.errors ); throw new OCPPError( - ErrorType.FORMATION_VIOLATION, + OCPP16ServiceUtils.AjvErrorsToErrorType(validate.errors), 'Request PDU is invalid', commandName, JSON.stringify(validate.errors, null, 2) diff --git a/src/charging-station/ocpp/OCPPResponseService.ts b/src/charging-station/ocpp/OCPPResponseService.ts index afef76b6..8c9cd984 100644 --- a/src/charging-station/ocpp/OCPPResponseService.ts +++ b/src/charging-station/ocpp/OCPPResponseService.ts @@ -4,10 +4,10 @@ import ajvFormats from 'ajv-formats'; import OCPPError from '../../exception/OCPPError'; import { JsonType } from '../../types/JsonType'; -import { ErrorType } from '../../types/ocpp/ErrorType'; import { RequestCommand } from '../../types/ocpp/Requests'; import logger from '../../utils/Logger'; import type ChargingStation from '../ChargingStation'; +import { OCPP16ServiceUtils } from './1.6/OCPP16ServiceUtils'; const moduleName = 'OCPPResponseService'; @@ -45,7 +45,7 @@ export default abstract class OCPPResponseService { validate.errors ); throw new OCPPError( - ErrorType.FORMATION_VIOLATION, + OCPP16ServiceUtils.AjvErrorsToErrorType(validate.errors), 'Response PDU is invalid', commandName, JSON.stringify(validate.errors, null, 2) diff --git a/src/charging-station/ocpp/OCPPServiceUtils.ts b/src/charging-station/ocpp/OCPPServiceUtils.ts index c9bfdd56..25d6b1e7 100644 --- a/src/charging-station/ocpp/OCPPServiceUtils.ts +++ b/src/charging-station/ocpp/OCPPServiceUtils.ts @@ -1,8 +1,28 @@ +import { DefinedError, ErrorObject } from 'ajv'; + +import { ErrorType } from '../../types/ocpp/ErrorType'; + export class OCPPServiceUtils { protected constructor() { // This is intentional } + public static AjvErrorsToErrorType(errors: ErrorObject[]): ErrorType { + for (const error of errors as DefinedError[]) { + switch (error.keyword) { + case 'type': + return ErrorType.TYPE_CONSTRAINT_VIOLATION; + case 'dependencies': + case 'required': + return ErrorType.OCCURRENCE_CONSTRAINT_VIOLATION; + case 'pattern': + case 'format': + return ErrorType.PROPERTY_CONSTRAINT_VIOLATION; + } + } + return ErrorType.FORMAT_VIOLATION; + } + protected static getLimitFromSampledValueTemplateCustomValue( value: string, limit: number, -- 2.34.1