Registry / testing / tiny-schema-validator

tiny-schema-validator

JSON →
library5.0.3jsnpmunverified

JSON schema validator with excellent type inference for JavaScript and TypeScript. Current stable version is 5.0.3. It provides a declarative API using an underscore (_) namespace for defining schemas with built-in validators like string, number, boolean, union, record, list, etc. Differentiators include automatic TypeScript type inference via the produce method, detailed error objects that mirror the schema shape, and a lightweight bundle (minzipped size shown in badge). The library is actively maintained, supports Node.js >=10, and is ESM and CJS compatible.

npm install tiny-schema-validator
INSTALL
IMPORT
SIG · TINY-SCHEMA-VALIDA
T
tiny-schema-validator
testingjavascriptv5.0.3
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

createSchema
✓ import { createSchema } from 'tiny-schema-validator'
✗ const { createSchema } = require('tiny-schema-validator')
ESM import is recommended; CommonJS require also works for Node.js environments.
_
✓ import { _ as validators } from 'tiny-schema-validator'
✗ import _ from 'tiny-schema-validator'
The underscore must be a named import, not default. Aliasing is optional but recommended for clarity.
ReturnType
✓ import { createSchema, _ } from 'tiny-schema-validator'; export type UserType = ReturnType<typeof User.produce>
✗ import { createSchema } from 'tiny-schema-validator'; export type UserType = InferType<typeof User>
Type inference is obtained via ReturnType on the produce method, not a separate type utility.

Creates a Person schema, then demonstrates is(), validate(), and produce() methods.

import { createSchema, _ } from 'tiny-schema-validator'; const Person = createSchema({ name: _.string(), age: _.number(), email: _.string() }); const data = { name: 'Alice', age: 30, email: 'alice@example.com' }; console.log(Person.is(data)); // true console.log(Person.validate(data)); // null try { Person.produce(data); console.log('valid'); } catch (e) { console.error(e.message); }
Debug
Known issues
gotchaAll validators are required by default. To make a field optional, use the embed() method with { optional: true }.
fix
For optional fields, define a separate schema and embed it: const OptionalName = createSchema({ name: _.string() }).embed({ optional: true }).
affects: *
breakingIn version 5.0.0, the `validate` method changed from throwing on invalid data to returning an errors object or null.
fix
Update code to check the return value of validate() instead of using try/catch.
affects: >=5.0.0 <6.0.0
gotchaThe `_.listof` validator returns an array type, not a tuple. For fixed-length tuples, use `_.list([...])`.
fix
Use `_.list([_.string(), _.number()])` for a tuple [string, number].
affects: *
gotchaThe `_.constant` validator requires a primitive value. Passing objects or arrays will not produce the expected type inference.
fix
Use `_.record` or `_.list` for object/array constants.
affects: *
Errors
Common errors & fixes
TypeError: createSchema is not a function
Importing createSchema as default export instead of named export.
fix
Change import to: import { createSchema } from 'tiny-schema-validator'
TypeError: _.string is not a function
Importing _ as default export instead of named export.
fix
Change import to: import { _ } from 'tiny-schema-validator'
TypeScript error: 'Person' refers to a value, but is being used as a type here.
Attempting to use the schema directly as a type without extracting the inferred type.
fix
Use: type PersonType = ReturnType<typeof Person.produce>
Upgrade
Version history
5.0.3latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
10 hits · last 30 days
node
10
Resources
tiny-schema-validator — npm install tiny-schema-validator · libregistry