Skip to content

tiny predator at
the app's edge

A JavaScript runtime schema validator written in TypeScript, with type safety and inference.

dot

Hi, my name is Dot.

I am small and fast. I do one thing well: I validate stuff.

At the app's edge, a shape comes in, Dot checks it, and you move on to business logic.

ts
import {dot} from '@sajajs/dot'

const UserSchema = dot.object({
    id: dot.string()
        .min(4)
        .as(s => dot.int().gte(1000).validate(Number(s))), // cheap, no hoisting required
    name: dot.string()
        .min(1)
        .prepare(s => s.trim())
        .refine(s => s.toLowerCase() !== 'root', 'reserved name'),
    role: dot.enum(['admin', 'predator']).optional(),
    tags: dot.array(dot.string().min(2)).min(1).nullable()
}).strict()

type User = dot.infer<typeof UserSchema>
// {id: number, name: string, role?: 'admin' | 'predator', tags: string[] | null}

const user: User = UserSchema.validate({
    id: '12354',
    name: ' Dot ',
    role: 'predator',
    tags: ['small', 'fast']
})
// {id: 12354, name: 'Dot', role: 'predator', tags: ['small', 'fast']}