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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@caeljs/tsh

v1.1.3

Published

A flexible typed data validation library that allows you to define schemas with TypeScript and validate data at runtime.

Readme

# Type-Safe Validation Library

A robust, type-safe validation library for TypeScript that provides runtime validation while maintaining full type safety.

## Features

- 🛠️ **Type-safe validators** with TypeScript inference
- 🧩 **Composable API** for building complex validation schemas
- 🔍 **Runtime validation** with detailed error reporting
- 🏗️ **Object shape validation** with nested structures
- 🔄 **Coercion support** for common type conversions
- ✨ **Utility functions** for common validation patterns

## Installation

```bash
npm install @caeljs/tsh
# or
yarn add @caeljs/tsh

Basic Usage

import { t } from '@caeljs/tsh';

// Define a schema
const userSchema = object({
  name: t.string().required(),
  age: t.number().positive().int(),
  email: t.string().email(),
  tags: t.array(t.string()).minLength(1),
});

// Validate data
const result = t.validate({
  name: "Drylian",
  age: 23,
  email: "[email protected]",
  tags: ["developer", "typescript"]
}, userSchema);

if (result.success) {
  console.log("Valid data:", result.data);
} else {
  console.error("Validation error:", result.error);
}

Core Validators

Primitive Validators

  • t.string() - Validates string values
  • t.number() - Validates number values
  • t.boolean() - Validates boolean values
  • t.any() - Accepts any value
  • t.enum() - Validates against enum values

Complex Validators

  • t.object(shape) - Validates object structures
  • t.array(shape) - Validates arrays
  • t.record(keyShape, valueShape) - Validates dictionary/map structures
  • t.union(shapes) - Validates against multiple possible shapes

Modifiers

  • .optional() - Makes a field optional
  • .nullable() - Allows null values
  • .required() - Requires non-null, non-undefined values
  • .partial() - Makes all object properties optional
  • .refine(predicate, message) - Adds custom validation

Utility Functions

Validation

  • t.validate(value, shape) - Validates a value against a shape
  • t.isValid(value, shape) - Checks if a value is valid

Object Manipulation

  • t.pick(shape, keys) - Creates a shape with only specified keys
  • t.omit(shape, keys) - Creates a shape without specified keys
  • t.merge(shape1, shape2) - Merges two object shapes
  • t.extend(shape, extensions) - Extends an object shape

Array Utilities

  • t.nonEmptyArray(shape) - Requires non-empty arrays
  • t.uniqueArray(shape) - Requires unique array elements
  • t.minLength(shape, min) - Sets minimum array length
  • t.maxLength(shape, max) - Sets maximum array length

Common Validators

String Validators

  • t.email() - Validates email format
  • t.uuid() - Validates UUID format
  • t.url() - Validates URL format
  • t.ip() - Validates IP address
  • t.regex(pattern) - Validates against regex

Number Validators

  • t.int() - Validates integers
  • t.positive() - Validates positive numbers
  • t.negative() - Validates negative numbers
  • t.port() - Validates port numbers
  • t.percentage() - Validates percentages

Advanced Features

Logical Operators

  • t.and(shape1, shape2) - Requires both shapes to match (intersection)
  • t.or(shape1, shape2) - Requires either shape to match (union)
  • t.not(shape) - Requires value to NOT match shape

Coercion

import { t } from '@caeljs/tsh';

// Coerce values to specific types
const coercedString = t.coerce.string(); // Converts value to string
const coercedNumber = t.coerce.number(); // Converts value to number
const coercedBoolean = t.coerce.boolean(); // Converts value to boolean

Custom Validators

import { t } from '@caeljs/tsh';

// Create custom validator
const evenNumber = t.custom(
  (value): value is number => typeof value === 'number' && value % 2 === 0,
  'Value must be an even number'
);

Error Handling

All validation errors include detailed information about what failed:

try {
  shape.parse(invalidValue);
} catch (error) {
  if (error instanceof TshShapeError) {
    console.error(error.message);
    console.error(error.path); // Path to invalid field
    console.error(error.value); // Invalid value
  }
}

Type Safety

The library provides full TypeScript type inference:

const userSchema = t.object({
  name: t.string(),
  age: t.number(),
});

type User = t.infer<typeof userSchema>;
// Equivalent to:
// type User = {
//   name: string;
//   age: number;
// }

Random Value Generation

import { random, randomInt, randomUuid } from '@caeljs/tsh';

t.random(); // Random string
t.randomInt(1, 100); // Random integer between 1-100
t.randomUuid(); // Random UUID v4

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

License

MIT