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

0.0.1 • Public • Published

avrots

A DSL for simultaneously constructing an avro schema and the corresponding typescript type. Inspired by pelotom/runtypes and joewood/avro-typescript.

Usage

Construct an avro schema using the DSL.

import {Record, Field, Array, String, Named, AsTypescript} from 'avrots'

const Toy = Record('toy', {
  name: Field(String())
})

const ToyArray = Array(Toy)

const Person = Record('person', {
  name: Field({default: null, type: String()}),
  ownToys: Field<typeof ToyArray>(ToyArray), // occasional disambiguation is still needed, but should be obvious
  covetedToys: Field<typeof ToyArray>(Array(Named(Toy))) // reference named types from earlier in the schema, since avro doesn't like redundant names
})

The generated object is already a valid avro schema, so we can use it directly in that way, but we can also use it to get the typescript type of an object that meets the schema.

type Person = AsTypescript<typeof Person>

const timmy: Person = {
 ownToys: [],
 covetedToys: ['guitar'] //type error! string is not a Toy.
}

This allows us to register a schema with some service and use that schema to get compile time validation that our code will produce records that are valid to that service.

Dependencies (0)

    Dev Dependencies (9)

    Package Sidebar

    Install

    npm i avrots

    Weekly Downloads

    0

    Version

    0.0.1

    License

    MIT

    Unpacked Size

    51.1 kB

    Total Files

    46

    Last publish

    Collaborators

    • schicksw