zod-ocpp
TypeScript icon, indicating that this package has built-in type declarations

1.0.10 • Public • Published

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

Configuration

Types

Package Sidebar

Install

npm i zod-ocpp

Weekly Downloads

26

Version

1.0.10

License

MIT

Unpacked Size

204 kB

Total Files

288

Last publish

Collaborators

  • chilgoe