From: Jérôme Benoit Date: Sat, 20 Aug 2022 13:11:11 +0000 (+0200) Subject: Add and use helper to convert Ajv JSON schema validation errors to OCPP X-Git-Tag: v1.1.66~37 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=06ad945f66c591d91a4c0062e9f39e007b05ba83;p=e-mobility-charging-stations-simulator.git Add and use helper to convert Ajv JSON schema validation errors to OCPP error type Signed-off-by: Jérôme Benoit --- 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,