@citrineos/base
Version:
The base module for OCPP v2.0.1 including all interfaces. This module is not intended to be used directly, but rather as a dependency for other modules.
75 lines (74 loc) • 3.33 kB
TypeScript
import { Ajv, type ErrorObject } from 'ajv';
import type { ILogObj } from 'tslog';
import { Logger } from 'tslog';
import type { OcppRequest, OcppResponse } from '../../ocpp/internal-types.js';
import { type CallAction, OcppError, OCPPVersion } from '../../ocpp/rpc/message.js';
export declare class OCPPValidator {
protected _ajv: Ajv;
protected readonly _logger: Logger<ILogObj>;
constructor(logger?: Logger<ILogObj>, ajv?: Ajv);
/**
* Creates an Ajv instance configured for Fastify HTTP schema compilation.
* Enables type coercion since HTTP query/path params arrive as strings,
* and does not include OCPP-specific keywords.
*
* @param ajv - Optional existing Ajv instance to use instead of creating a new one
* @returns Configured Ajv instance for Fastify schema compilation
*/
static createServerAjvInstance(ajv?: Ajv): Ajv;
/**
* Creates an Ajv instance configured for OCPP message validation.
* Does not coerce types since OCPP messages arrive as parsed JSON with correct types.
* Includes OCPP-specific schema keywords and strict number/required validation.
*
* @param ajv - Optional existing Ajv instance to use instead of creating a new one
* @returns Configured Ajv instance for OCPP message validation
*/
static createValidatorAjvInstance(ajv?: Ajv): Ajv;
/**
* Adds custom keywords for OCPP schema metadata to an Ajv instance.
* These keywords are used in OCPP JSON schemas but don't affect validation.
*
* @param ajv - The Ajv instance to add keywords to
*/
static addOcppKeywords(ajv: Ajv): void;
/**
* Adds format validation for date-time and URI formats to an Ajv instance.
*
* @param ajv - The Ajv instance to add formats to
*/
static addFormats(ajv: Ajv): void;
/**
* Validates an OCPP Request object against its schema.
*
* @param {CallAction} action - The original CallAction.
* @param {OcppRequest} payload - The OCPP Request object to validate.
* @param {OCPPVersion} protocol - The OCPP protocol version.
* @return {boolean} - Returns true if the OCPP Request object is valid, false otherwise.
*/
validateOCPPRequest(action: CallAction, payload: OcppRequest, protocol: OCPPVersion): {
isValid: boolean;
errors?: ErrorObject[] | null;
};
/**
* Validates an OCPP Response against its schema.
*
* @param {CallAction} action - The original CallAction.
* @param {OcppResponse} payload - The OCPPResponse object to validate.
* @param {OCPPVersion} protocol - The OCPP protocol version.
* @return {boolean} - Returns true if the OCPPResponse object is valid, false otherwise.
*/
validateOCPPResponse(action: CallAction, payload: OcppResponse | OcppError, protocol: OCPPVersion): {
isValid: boolean;
errors?: ErrorObject[] | null;
};
/**
* Prepares an OCPP Payload for sending by removing any null values, as OCPP does not allow null values in its messages.
*
* @param message OCPP Payload, as an object
* @returns The sanitized OCPP Payload, with null values removed
*/
sanitizeOCPPPayload<T extends OcppRequest | OcppResponse>(message: T): T;
private removeNulls;
private fixRefs;
}