npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

typebox-utils

v0.2.2

Published

TypeBox utilities with MongoDB ObjectId support and common validation types

Downloads

9

Readme

TypeBox Utils

A robust validation library built on top of @sinclair/typebox that provides enhanced ObjectId support, common schema types, and simplified validation workflows.

Features

  • 🔄 Automatic MongoDB ObjectId conversion and validation
  • 📝 Pre-compiled schema validation for better performance
  • ✨ Common reusable schema types (Email, Mobile, UUID, Timestamp)
  • 🎯 Type-safe validation with TypeScript
  • 🛠 Custom format validators for ObjectId, email, mobile, and UUID
  • 📦 Array validation support
  • 🔍 Detailed error messages with path information
  • 🔀 Dual ESM and CommonJS support

Installation

npm install typebox-utils @sinclair/typebox mongodb

Note: This package is a peer dependency of @sinclair/typebox and mongodb. so you need to install them separately.

Usage

ESM Example

import { Type, Utils, createSchema, validate } from 'typebox-utils';

// Schema creation
const userSchema = createSchema(
  Type.Object({
    _id: Utils.ObjectId(),
    email: Utils.Email({ random: true }),
    createdAt: Utils.Timestamp({ random: true }),
    contacts: Type.Array(Utils.ObjectId())
  })
);

// Data validation
const [error, validated] = validate({
  email: '[email protected]',
  contacts: ['507f1f77bcf86cd799439011']
}, userSchema, true);

if (error) throw new Error(error);
console.log(validated);

CommonJS Example

const { Type, Utils, createSchema, validate } = require('typebox-utils');

// Schema creation
const productSchema = createSchema(
  Type.Object({
    sku: Utils.UUID({ random: true }),
    price: Type.Number({ minimum: 0 }),
    created: Utils.Timestamp()
  })
);

// Data validation
const [err, result] = validate({
  price: 29.99,
  created: Date.now()
}, productSchema);

if (err) console.error(err);
else console.log(result);

API Reference

validate(value, schema, skipOperations?)

Validates data against a TypeBox schema with automatic type conversion.

Parameters:

  • value: Data to validate
  • schema: Compiled TypeBox schema (use createSchema)
  • skipOperations: Optional array of operations to skip:
    • Clean: Responsible for removing excess properties from a value
    • Default: Responsible for generating missing properties on a value using default schema annotations if available
    • Convert: Responsible for converting a value into its target type if a reasonable conversion is possible
    • ConvertOID: Responsible for converting ObjectId strings to ObjectId instances

Returns: [error: string | null, validatedData: T]

Example:

const [error, data] = validate(rawInput, schema, ['Clean']);

validateArray(values, schema)

Validates an array of values against a schema.

Parameters:

  • values: Array of data to validate
  • schema: Compiled TypeBox schema

Returns: Array of [error, validatedData] tuples


createSchema(schema)

Pre-compiles schemas for better validation performance.

Parameters:

  • schema: TypeBox schema object

Returns: Compiled schema with type information


Utility Types

Utils.Timestamp(config?)

Unix timestamp (milliseconds since epoch) Options:

  • default: Default timestamp value
  • minimum: Minimum allowed value (default: 0)
  • maximum: Maximum allowed value
  • random: Generate current timestamp

Utils.UUID(config?)

UUID v4 format validation Options:

  • default: Default UUID string
  • random: Generate random UUID

Utils.Email(config?)

Email format validation Options:

  • default: Default email address
  • random: Generate random email

Utils.Mobile(config?)

10-digit mobile number validation Options:

  • default: Default mobile number
  • random: Generate random number

Utils.ObjectId(config?)

MongoDB ObjectId validation/transformation Options:

  • default: Default ObjectId string
  • random: Generate new ObjectId

Custom Formats

Pre-registered validation formats:

  • objectid: MongoDB ObjectId validation
  • email: Simple email format
  • mobile: 10-digit number
  • uuid: UUID v4 format

See TypeBox Formats for more information.

Best Practices

  1. Pre-compile Schemas:
// Recommended
const compiledSchema = createSchema(Type.Object({ ... }));
const [error] = validate(data, compiledSchema);

// Not recommended
const [error] = validate(data, Type.Object({ ... }));
  1. Handle ObjectId Conversion:
// Returns ObjectId instances for string values
const [error, data] = validate({
  _id: '507f1f77bcf86cd799439011'
}, schema);

console.log(data._id instanceof ObjectId); // true
  1. Use Random Defaults:
const schema = createSchema(
  Type.Object({
    sessionId: Utils.UUID({ random: true }),
    createdAt: Utils.Timestamp({ random: true })
  })
);

Peer Dependencies

License

MIT © Anuj Kumar Jha

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.