Skip to content

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 tinyval

Try 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 ​

SectionContents
FunctionsRunning validation: validate, validateAsync, parse, parseAsync
SchemasData types: string, number, object, array, optional, union, variant, coerce…
ConditionsLike Laravel: requiredIf, requiredWith, requiredWithout, prohibitedIf
Object helpersNew schemas from existing ones: partial, pick, omit, extend
RulesChecks and transforms via pipe: minLength, email, sameAs, trim, check…

Further reading ​