zod-ocpp
v1.0.17
Published
Typescript Open Charge Point Protocol v1.6 (ocpp1.6j) validate schema with zod.
Downloads
492
Maintainers
Readme
zod-ocpp
zod-ocpp is a TypeScript library that provides validation schemas for the Open Charge Point Protocol (OCPP) 1.6J specification using the Zod library. It allows you to validate OCPP 1.6J request and response payloads against the official specification.
Table of contents
Installation
You can install zod-ocpp using npm:
npm install zod-ocpp
Example
Call Validate message
import { CallSchema } from 'zod-ocpp';
const message = [2, '4ae886ab-407d-45a7-839c-3632cf108f53', "Heartbeat", {}];
const result = CallSchema.safeParse(requestPayload);
if (result.success) {
console.log('Valid payload');
console.log(result.data);
} else {
console.error('Invalid payload:', result.error);
}
CallResult Validate message
import { CallResultSchema } from 'zod-ocpp';
const message = [3, '4ae886ab-407d-45a7-839c-3632cf108f53', {currentTime: '2024-03-31T15:01:21.325Z'}];
const result = CallResultSchema.safeParse(message);
if (result.success) {
console.log('Valid payload');
console.log(result.data);
} else {
console.error('Invalid payload:', result.error);
}
CallError Validate message
import { CallErrorSchema } from 'zod-ocpp';
const message = [4, '4ae886ab-407d-45a7-839c-3632cf108f53', 'NotSupported', 'Not support this protocol', {}];
const result = CallErrorSchema.safeParse(message);
if (result.success) {
console.log('Valid payload');
console.log(result.data);
} else {
console.error('Invalid payload:', result.error);
}
BootNotification Validate payload
Request
import { BootNotificationRequestSchema } from 'zod-ocpp';
const requestPayload = {
chargePointVendor: 'VendorX',
chargePointModel: 'ModelA',
chargePointSerialNumber: '123456789',
chargeBoxSerialNumber: '987654321',
firmwareVersion: '1.2.3',
iccid: '89012345678901234567',
imsi: '123456789012345',
meterType: 'Metertype1',
meterSerialNumber: 'ABC123',
};
const result = BootNotificationRequestSchema.safeParse(requestPayload);
if (result.success) {
console.log('Valid payload');
} else {
console.error('Invalid payload:', result.error);
}
Response
import { BootNotificationResponseSchema } from 'zod-ocpp';
const responsePayload = {
status: 'Accepted',
currentTime: '2024-03-31T15:01:21.325Z',
interval: 300,
};
const result = BootNotificationResponseSchema.safeParse(responsePayload);
if (result.success) {
console.log('Valid payload');
console.log(result.data);
} else {
console.error('Invalid payload:', result.error);
}
Schema and Enum
Message
- Authorize
- BootNotification
- CancelReservation
- ChangeAvailability
- ChangeConfiguration
- ClearCache
- ClearChargingProfile
- DataTransfer
- DiagnosticsStatusNotification
- FirmwareStatusNotification
- GetCompositeSchedule
- GetConfiguration
- GetDiagnostics
- GetLocalListVersion
- Heartbeat
- MeterValues
- RemoteStartTransaction
- RemoteStopTransaction
- ReserveNow
- Reset
- SendLocalList
- SetChargingProfile
- StartTransaction
- StatusNotification
- StopTransaction
- TriggerMessage
- UnlockConnector
- UpdateFirmware
Configuration
Types
- Action
- AuthorizationData
- AuthorizationStatus
- AvailabilityStatus
- AvailabilityType
- Call
- CallError
- CallResult
- CancelReservationStatus
- ChargePointErrorCode
- ChargePointStatus
- ChargingProfile
- ChargingProfileKindType
- ChargingProfilePurposeType
- ChargingProfileStatus
- ChargingRateUnitType
- ChargingSchedule
- ChargingSchedulePeriod
- CiString20Type
- CiString25Type
- CiString50Type
- CiString255Type
- CiString500Type
- ClearCacheStatus
- ClearChargingProfileStatus
- ConfigurationStatus
- DataTransferStatus
- DiagnosticsStatus
- ErrorCode
- FirmwareStatus
- GetCompositeScheduleStatus
- IdTagInfo
- IdToken
- KeyValue
- Location
- Measurand
- MessageTrigger
- MeterValue
- Phase
- ReadingContext
- Reason
- RecurrencyKindType
- RegistrationStatus
- RemoteStartStopStatus
- ReservationStatus
- ResetStatus
- ResetType
- SampledValue
- TriggerMessageStatus
- UnitOfMeasure
- UnlockStatus
- UpdateStatus
- UpdateType
- ValueFormat