0.3.3 • Published 3 months ago

tipets v0.3.3

Weekly downloads
-
License
MIT
Repository
github
Last release
3 months ago

tipets

Typescript-first schema declaration, validation library and contextual transformation. Inspired from io-ts and joi.

tipets is still in alpha. Expect bugs and api changes!

Features

  • Typescript-first library which support type inference and schema type properly.
  • Support class-based or schema-based declaration
  • Powerful schema declaration, can be used to type-guard and validation. Can also be reused for multiple use case such as json, bson, or other
  • Type coercion, allow data transformation from/to different type for input/output.
  • Fast and Extensible, build your own schema/codec/parser

Quick Start

Install

npm install tipets

Using schema:

// create schema
const schema = object({
  id: number(),
  firstName: string(),
  lastName: optional(string()),
  active: boolean(),
})

// type-guard
if (schema.is(value)) {
  // validation
  const violations = schema.validate(value)
}

Using parser:

// create default parser
const parser = Parser.create()

// decode/encode
const decoded = parser.decode(value)
const encoded = parser.encode(decoded)

// decode without exception
const decoded = parser.tryDecode(value)
if (decoded.success) {
  // do whatever with Result
  const result = decoded.value

  // encode without exception
  const encoded = parser.tryEncode(result)
}

Built-in

Schema

TypeTypescriptBuilder
stringstringt.string()
numbernumbert.number()
booleanbooleant.boolean()
literal'A't.literal('A')
unknownunknownt.unknown()
anyanyt.any()
nullnullt.null()
nullableA \| nullt.nullable(t.type(A))
undefinedundefinedt.undefined()
optionalA \| undefinedt.undefined(t.type(A))
arrayarrayt.array(T)
type*At.type(A)
object{a:A}t.object({a: t.type(A)})
unionA \| Bt.union(t.type(A), t.type(B))
intersectA & Bt.intersect(t.type(A), t.type(B))

*: type is still in development because I can't decide if

Parser

NameDescription
ParserParser with some some built in primitive Codec. You mostly use this
0.3.2

3 months ago

0.3.3

3 months ago

0.3.1

9 months ago

0.3.0

9 months ago

0.2.0

10 months ago

0.1.0

11 months ago