import _Ajv, { type ValidateFunction } from 'ajv'
import _ajvFormats from 'ajv-formats'
-import { OCPPServiceUtils } from './OCPPServiceUtils.js'
import type { ChargingStation } from '../../charging-station/index.js'
import { OCPPError } from '../../exception/index.js'
import type {
RequestCommand
} from '../../types/index.js'
import { Constants, logger } from '../../utils/index.js'
+import { ajvErrorsToErrorType } from './OCPPServiceUtils.js'
type Ajv = _Ajv.default
// eslint-disable-next-line @typescript-eslint/no-redeclare
const Ajv = _Ajv.default
private static instance: OCPPResponseService | null = null
private readonly version: OCPPVersion
protected readonly ajv: Ajv
- protected abstract jsonSchemasValidateFunction: Map<RequestCommand, ValidateFunction<JsonType>>
- public abstract jsonSchemasIncomingRequestResponseValidateFunction: Map<
+ protected readonly ajvIncomingRequest: Ajv
+ protected abstract payloadValidateFunctions: Map<RequestCommand, ValidateFunction<JsonType>>
+ public abstract incomingRequestResponsePayloadValidateFunctions: Map<
IncomingRequestCommand,
ValidateFunction<JsonType>
>
multipleOfPrecision: 2
})
ajvFormats(this.ajv)
+ this.ajvIncomingRequest = new Ajv({
+ keywords: ['javaType'],
+ multipleOfPrecision: 2
+ })
+ ajvFormats(this.ajvIncomingRequest)
this.responseHandler = this.responseHandler.bind(this)
this.validateResponsePayload = this.validateResponsePayload.bind(this)
}
if (chargingStation.stationInfo?.ocppStrictCompliance === false) {
return true
}
- const validate = this.jsonSchemasValidateFunction.get(commandName)
+ const validate = this.payloadValidateFunctions.get(commandName)
if (validate?.(payload) === true) {
return true
}
validate?.errors
)
throw new OCPPError(
- OCPPServiceUtils.ajvErrorsToErrorType(validate?.errors),
+ ajvErrorsToErrorType(validate?.errors),
'Response PDU is invalid',
commandName,
JSON.stringify(validate?.errors, undefined, 2)