Giter VIP home page Giter VIP logo

avrots's Introduction

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.

avrots's People

avrots's Issues

Make optional properties optional, not just nullable

Adding a nullable or defaulted field to a record creates a sane avro schema, but still requires the field to be provided (though it can be null or undefined).

const Person = Record('person', {name: Field({type: String(), default: 'odysseus'})

const nobody: AsTypescript<typeof Person> = {} // type error! {} has no property 'name'

Improve inference on parametrized record fields

When a Field is constructed from a parametrized type, it can't correctly infer the underlying type without an explicit annotation. This is trivial to add, but irritating boilerplate.

const person = Record("person", {
    name: Field(String()),
    age: Field(Long()),
  });
const field = Field(person) // infers RequiredField<unknown>
const betterField = Field<typeof person>(person) //infers RequiredField<{name: string, age: number}>

Ideally fields would automatically do this inference, rather than needing to be "prompted" in this way. This feels like a higher kinded types problem though, so I'm not sure if there is a solution.

The issue exists for any field constructed with an underlying type which has a parameter.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.