Dictionary

litetype API reference

Narrative lives in the guide. This page is the compact reference.

Verbs

parse(s, data)Return Infer<S> or throw SchemaError
safeParse(s, data)Return a success/error union
check(s, data)Type predicate
compile(s)Cached predicate for hot loops

Types

Infer<S>Output type
InferInput<S>Input type before transforms and defaults
Schema<O, I>A schema node
ShapeA bare object schema

Leaves

SchemaOutputChecks
stringstringmin, max, length, email, url, uuid, datetime, ip, startsWith, endsWith, includes, regex
numbernumbermin, max, gt, lt, int, finite, positive, nonnegative, multipleOf
booleanboolean
dateDatemin, max
unknownunknown
literal(v)literal typeexact value
enum_(values)value unionmember

Every schema node

.transform(fn)Map after successful validation
.refine(pred, message)Add a predicate
.default(value)Fill when input is undefined
.undefinable()Allow undefined
.nullable()Allow null
.nullish()Allow both

Composition

arraytupleunionrecorddiscriminatedUnionlazystrictstrippartialrequiredpreprocessrefinecoercefallbackdescribestandard
TypeScript
import { array, discriminatedUnion, lazy, literal, number, string } from 'litetype'

const Shape = discriminatedUnion('type',
  { type: literal('circle'), radius: number },
  { type: literal('square'), side: number },
)

const Tree = { value: string, 'children?': array(lazy(() => Tree)) }

Errors

Every issue is { path, message, code }. Use flatten for forms, treeify for nested display, or prettify for text.

JSON Schema

TypeScript
import { fromJsonSchema, toJsonSchema } from 'litetype/jsonschema'

The subpath keeps JSON Schema code out of the main bundle. Transform, refine and preprocess cannot be serialized.