ty

@xieyuheng/ty

Here is a schema checker which can return well typed results. Tell your friends!

Showing:

Popularity

Downloads/wk

84

GitHub Stars

7

Maintenance

Last Commit

2d ago

Contributors

0

Package

Dependencies

10

License

GPL-3.0-or-later

Type Definitions

Tree-Shakeable

No?

Categories

Readme

Ty

Write schema to bring TypeScript's types to runtime.

Which can be used to:

  • Validate untyped data and return well typed result.
  • Generate random data of a given schema, to do property-based testing.
    • We also provide a library of logic theories, to be used as target of models.

Install

npm i @xieyuheng/ty

Examples

Validation untyped data

import ty from "@xieyuheng/ty"

const userSchema = ty.object({
  id: ty.int({ min: 0 }),
  first_name: ty.string(),
  last_name: ty.string(),
})

type User = {
  id: number
  first_name: string
  last_name: string
}

{
  const data: any = {
    id: 1,
    first_name: "Yuheng",
    last_name: "Xie",
  }

  const user: User = userSchema.validate(data)
  const userOmitId: Omit<User, "id"> = ty.omit(userSchema, "id").validate(data)
}

Generate random data of a given schema

{
  const user: User = userSchema.generate()

  userSchema.validate(user)

  console.log(user)
  // Will print something like:
  //   { id: 0, first_name: 'ada4a39ab0', last_name: '73be' }
}

Recursive and generic schema

type List<T> = null | { head: T; tail: List<T> }

function cons<T>(head: T, tail: List<T>): List<T> {
  return { head, tail }
}

function listSchema<T>(itemSchema: Schema<T>): Schema<List<T>> {
  const nullSchema = ty.null()
  const consSchema = ty.object({
    head: itemSchema,
    tail: ty.lazy(() => listSchema(itemSchema)),
  })
  return ty.union(nullSchema, consSchema)
}

{
  const schema = listSchema(ty.string())
  const data0: List<string> = schema.validate(null)
  const data1: List<string> = schema.validate(cons("a", null))
  const data2: List<string> = schema.validate(cons("a", cons("b", null)))
  const data3: List<string> = schema.validate(
    cons("a", cons("b", cons("c", null)))
  )
  schema.assertInvalid(cons(1, null))
  schema.assertInvalid(cons(1, cons(2, null)))
  schema.assertInvalid(cons(1, cons(2, cons(3, null))))
}

{
  const schema = listSchema(ty.number())
  const data0: List<number> = schema.validate(null)
  const data1: List<number> = schema.validate(cons(1, null))
  const data2: List<number> = schema.validate(cons(1, cons(2, null)))
  const data3: List<number> = schema.validate(cons(1, cons(2, cons(3, null))))
  schema.assertInvalid(cons("a", null))
  schema.assertInvalid(cons("a", cons("b", null)))
  schema.assertInvalid(cons("a", cons("b", cons("c", null))))
}

Property-based testing

TODO

Logic theories (interface and typeclass of laws)

To be used as target of models, for property-based testing.

TODO

API Docs

Primitive:

Collection:

Set-Theoretic:

Structural:

Recursion:

Similar projects

Runtime typing:

Property-based testing and data generation:

Contributions

Be polite, do not bring negative emotion to others.

License

Rate & Review

Great Documentation0
Easy to Use0
Performant0
Highly Customizable0
Bleeding Edge0
Responsive Maintainers0
Poor Documentation0
Hard to Use0
Slow0
Buggy0
Abandoned0
Unwelcoming Community0
100