tinyval
Tiny, fast, zero-dependency validation for browser and server.
- ~2.5 kB gzip for a typical form, ~5.5 kB for everything — only what you import ends up in the bundle
- TypeScript types inferred from the schema
- Nested objects and arrays, unknown keys handling, coercion, transforms, async checks
Install
bash
npm install tinyvalTry it
Quick start
ts
import { validate, object, string, optional, pipe, trim, minLength, email, min, coerce, type Infer } from 'tinyval';
const User = object({
name: pipe(string(), trim(), minLength(2)),
email: pipe(string(), trim(), email()),
age: optional(pipe(coerce.number(), min(0))),
});
type User = Infer<typeof User>; // { name: string; email: string; age?: number }
const result = validate(User, input);
if (result.valid) {
result.data; // cleaned, typed data
} else {
result.errors; // { name: '...', email: '...' }
}On the server parse is handier — it returns the data or throws ValidationError.
What's inside
| Section | Contents |
|---|---|
| Functions | Running validation: validate, validateAsync, parse, parseAsync |
| Schemas | Data types: string, number, object, array, optional, union, variant, coerce… |
| Conditions | Like Laravel: requiredIf, requiredWith, requiredWithout, prohibitedIf |
| Object helpers | New schemas from existing ones: partial, pick, omit, extend |
| Rules | Checks and transforms via pipe: minLength, email, sameAs, trim, check… |
Further reading
- Validation result — what's in
data,errorsandissues - Empty values — why
''is treated as a missing value - Messages & i18n — custom error texts and locales
- TypeScript — types from schemas
- Custom rules & schemas — when built-ins are not enough
- tinyval-vue — Vue forms:
useForm, errors, submission, server errors - Standard Schema — tinyval schemas in tRPC, Hono, TanStack Form, React Hook Form