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 |
Shape | A bare object schema |
Leaves
| Schema | Output | Checks |
|---|---|---|
string | string | min, max, length, email, url, uuid, datetime, ip, startsWith, endsWith, includes, regex |
number | number | min, max, gt, lt, int, finite, positive, nonnegative, multipleOf |
boolean | boolean | — |
date | Date | min, max |
unknown | unknown | — |
literal(v) | literal type | exact value |
enum_(values) | value union | member |
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
arraytupleunionrecorddiscriminatedUnionlazystrictstrippartialrequiredpreprocessrefinecoercefallbackdescribestandardimport { 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
import { fromJsonSchema, toJsonSchema } from 'litetype/jsonschema'
The subpath keeps JSON Schema code out of the main bundle. Transform, refine and preprocess cannot be serialized.