0.6.0 • Published 7 months ago

pertype v0.6.0

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

pertype

pertype - /type - is a typescript-first schema declaration, validation library and contextual transformation. Inspired from io-ts and joi.

pertype 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

Installation

npm install pertype       # npm
yarn add pertype          # yarn
bun add pertype           # bun
pnpm add pertype          # pnpm

Basic Usage

import { t } from 'pertype'

// create string
const schema = t.string()

// type guard
if (schema.is(value)) {
  // ... value is inferred as `string`
}

// decode value (coercion) to `string`
schema.decode(value)

// encode value to Json compatible type
schema.encode(value)

Schema

pertype come with built-in schema you can immediately use

NameTypescriptFactory
stringstringt.string()
numbernumbert.number()
booleanbooleant.boolean()
literal'A't.literal('A')
unknownunknownt.unknown()
anyanyt.any()
nullnullt.null()
nullableA \| nullt.nullable(t.type(A)) or t.type(A).nullable()
undefinedundefinedt.undefined()
optionalA \| undefinedt.optional(t.type(A)) or t.type(A).optional()
arrayarrayt.array(T) or t.
typeAt.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))
0.6.0

7 months ago

0.5.1

9 months ago

0.3.8

1 year ago

0.5.0

12 months ago

0.4.0

1 year ago

0.3.0

1 year ago

0.3.6

1 year ago

0.3.5

1 year ago

0.3.7

1 year ago

0.3.2

1 year ago

0.3.1

1 year ago

0.3.4

1 year ago

0.3.3

1 year ago

0.2.1

1 year ago

0.2.0

1 year ago

0.2.2

1 year ago

0.1.1

1 year ago

0.1.0

1 year ago