# @bpinternal/ptb-schema

> Small wrapper above [protobufjs](https://github.com/protobufjs/protobuf.js) that allows infering a TypeScript type directly from a protobuf schema in a [zod](https://github.com/colinhacks/zod)-like fashion.

Latest version **0.0.7** (published 2025-07-18) · MIT license · 0 weekly downloads

## Install

```sh
npm install @bpinternal/ptb-schema
pnpm add @bpinternal/ptb-schema
yarn add @bpinternal/ptb-schema
bun add @bpinternal/ptb-schema
```

## Health

**Score 35/100 (D)** — status: maintenance-mode.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.7 |
| Published | 2025-07-18 |
| First published | 2022-06-18 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >=16.0.0 |
| Dependencies | 1 |
| Unpacked size | 30 KB |
| Known vulnerabilities | 0 (+11 in 1 direct dependencies) |
| Install scripts | no |
| Author | Botpress, Inc. |
| Maintainers | allardy, davidvitora, michael.masson, franklevasseur, chsir17, slvnperron, botpress-cloud-ops |

## Links

- npm: https://www.npmjs.com/package/@bpinternal/ptb-schema
- npm.io page: https://npm.io/package/@bpinternal/ptb-schema

## Dependencies (1)

- [protobufjs](https://npm.io/package/protobufjs.md) 7.4.0

## Recent versions

- 0.0.7 (latest) — 2025-07-18
- 0.0.6 — 2024-09-26
- 0.0.5 — 2024-09-25
- 0.0.4 — 2024-07-02
- 0.0.3 — 2024-05-31
- 0.0.2 — 2022-06-18
- 0.0.1 — 2022-06-18

## README

# Protobuf Schema

Small wrapper above [protobufjs](https://github.com/protobufjs/protobuf.js) that allows infering a TypeScript type directly from a protobuf schema in a [zod](https://github.com/colinhacks/zod)-like fashion.

Defining a `PTBMessage` allows both to decode/encode data structures and to define types.

## Usage

```ts
import * as ptb from '@bpinternal/ptb-schema'

let userid = 0
const PtbUser = new ptb.PTBMessage('User', {
  email: { id: userid++, rule: 'required', type: 'string' },
  firstName: { id: userid++, rule: 'optional', type: 'string' },
  lastName: { id: userid++, rule: 'optional', type: 'string' },
  birthDate: { id: userid++, rule: 'required', type: 'int32' },
  isStrong: { id: userid++, rule: 'required', type: 'bool' },
  favoriteFood: { id: userid++, rule: 'repeated', type: 'string' }
})

let partyid = 0
const PtbParty = new ptb.PTBMessage('Party', {
  users: { id: partyid++, rule: 'repeated', type: PtbUser }
})

type User = ptb.Infer<typeof PtbUser>
type Party = ptb.Infer<typeof PtbUser>

const eminemBenchPress = 145
const strongThreshold = 225

const eminem: User = {
  email: 'slim.shady@botpress.com',
  firstName: 'eminem',
  lastName: undefined,
  birthDate: Date.now(),
  favoriteFood: ["mom's spaghetthi"],
  isStrong: eminemBenchPress >= strongThreshold
}

const encoded: Uint8Array = PtbParty.encode({
  users: [eminem]
})

console.log('bytes:', encoded.length)

const decoded = PtbParty.decode(encoded)
console.log('decoded:', decoded)
```

## Disclaimer ⚠️

This package is published under the `@bpinternal` organization. All packages of this organization are meant to be used by the [Botpress](https://github.com/botpress/botpress) team internally and are not meant for our community. Since the packages are catered to our own use-cases, they might have less stable APIs, receive breaking changes without much warning, have minimal documentation and lack community-focused support. However, these packages were still left intentionally public for an important reason : We Love Open-Source. Therefore, if you wish to install or fork this package feel absolutly free to do it. We strongly recommend that you tag your versions properly.

The Botpress Engineering team.

---
_Source: https://npm.io/package/@bpinternal/ptb-schema · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
