Tiny CBOR Schema

A runtime schema builder for CBOR that provides encoding and decoding between CBOR and TypeScript types. It pairs with Tiny CBOR under the hood and exposes a concise API via cs. Type inference with valueOf<T> keeps your application strongly typed.

Install

npm i @levischuck/tiny-cbor-schema
pnpm add @levischuck/tiny-cbor-schema
deno add jsr:@levischuck/tiny-cbor-schema
bunx jsr add @levischuck/tiny-cbor-schema

Quick start

quick-start.tsts

Primitives

primitives.tsts

Arrays and tuples

arrays-tuples.tsts

Maps and fields

maps.tsts

Optional and literal values

optional-literal.tsts

Unions

unions.tsts

Tagged values

tagged.tsts

Nested CBOR

nested.tsts

Lazy and recursive schemas

lazy.tsts

API overview

cs (schema builder)

Main schema builder class containing all schema constructors and primitive types. Prefer the shorthand alias cs. Primitives: string, integer, bigint, float, boolean, bytes. Constructors: array, tuple, union, tagged, optional, map, field, numberField, nested, literal, lazy.

fromCBOR

Decodes a CBOR byte array using the provided schema. Returns the decoded, typed value.

toCBOR

Encodes a value to CBOR using the provided schema. Returns a Uint8Array of encoded bytes.

valueOf<T>

Infers the TypeScript type from a tiny CBOR schema object, allowing you to annonate the type of decoded value.Please note that any exported type using valueOf will be considered a slow type.

COSE and CWT examples

cose-ec2.tsts
cwt.tsts

Source codeBack to Docs