1.0.0-rc1 • Published 5 years ago

@rtcad/core v1.0.0-rc1

Weekly downloads
4
License
MIT
Repository
github
Last release
5 years ago

WORK IN PROGRESS

Current status: proof of concept

rtcad

runtime type checking and documentation

Motivation

  1. Validate arguments and result of functions call for good guaranties of code correctness.
  2. Automatically generate documentation.
  3. Infer static types.
  4. SSoT of validators, static types and documentation.
  5. static Refinement data-types
  6. Leave mocks for tests (generate it automatically).
  7. Property-base testing out of the box.

target Example

import * as t from 'rtcad'
import * as network from './network' // fetcher

// declare runtype
const Password = t.String()

// declare runtype with description and extended validation
const Email = t`
  simple e-mail validation
  ${t.String(value => value.contains('@'))}
`

// declare structure
const User = t`user data ${t.Record({
  id: t.Number(),
  name: t.String(),
  avatar: t.OR(t.Null, t.String(url => url.startsWith('http'))),
})}`

// declare ~generic by custom runtype
// (TypeScript syntax)
const Instanceof = <T extends t.Runtype>(Gen: T) =>
  t.custom<T>(value => value instanceof Gen)

// use generic
const ErrorApi = Instanceof(network.Error)

// declare contract for function with description (Markdown)
export const authUser = t`
  # Return detailed user data from auth service
  > set auth token to cookies
  - ${[Email, Password]}
  - ${t.Promise(User, ErrorApi)}
`((email, password) => network.get(`/user`, { email, password }))

/* EQUAL */

// declare contract for function without description
export const authUser = t([Email, Password], t.Promise(User, ErrorApi))(
  (email, password) => network.get(`/user`, { email, password }),
)

/**
 * Now we can:
 * 1. Infer static types.
 * 2. Validate arguments and result of `authUser` call.
 * 3. Automatically generate documentation.
 * 4. Leave mocks for tests (generate it automatically).
 * 5. Property-base testing out of the box.
 */

Background

1.0.0-rc1

5 years ago

1.0.0-rc0

5 years ago