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 🙏

© 2024 – Pkg Stats / Ryan Hefner

type-sentry

v0.0.9

Published

A powerful TypeScript library for runtime type checking and validation using decorators

Downloads

9

Readme

TypeSentry

TypeSentry Logo

TypeSentry is a lightweight yet powerful TypeScript library for runtime type checking and validation using decorators. It offers a simple and intuitive API for validating method parameters and complex objects, enhancing the robustness and reliability of your TypeScript applications without adding unnecessary bulk to your project. With TypeSentry, you can easily create your own custom decorators, allowing for maximum flexibility and extensibility.

Key benefits:

  • 🪶 Lightweight: Minimal impact on your project's size
  • 🚀 Fast: Efficient runtime type checking and validation
  • 🛠 Easy to use: Intuitive decorator-based API
  • 🔧 Flexible: Works with simple types and complex objects
  • 🎨 Customizable: Create your own decorators with ease

npm version License: MIT

Features

  • 🛡️ Runtime type checking for method parameters
  • 🎨 Built-in decorators for common validations
  • 🔧 Customizable validation messages
  • 🚀 Seamless integration with existing TypeScript projects
  • 🛠️ Easy creation of custom decorators for unique validation needs
  • 🏗️ Complex object validation using class-validator
  • 🔍 Lightweight core with optional integration of powerful validation libraries

Installation

npm install type-sentry

For complex object validation with class-validator:

npm install type-sentry class-validator class-transformer

Configuration

Enable experimental decorators in your tsconfig.json:

{
  "compilerOptions": {
    "experimentalDecorators": true
  }
}

Usage

Basic Usage

import { Validate, IsNumber, IsString } from 'type-sentry';

class UserService {
  @Validate()
  createUser(@IsNumber() age: number, @IsString() name: string) {
    console.log(`Creating user: ${name}, age: ${age}`);
  }
}

const userService = new UserService();
userService.createUser(30, "John Doe"); // Works fine
userService.createUser("30", "John Doe"); // Throws a validation error

Custom Validators

One of TypeSentry's most powerful features is the ability to create custom validators:

import { createParamValidator, Validate } from 'type-sentry';

const IsPositive = createParamValidator(
  (value) => typeof value === 'number' && value > 0,
  "Must be a positive number"
);

class MathService {
  @Validate()
  calculateSquareRoot(@IsPositive() num: number) {
    return Math.sqrt(num);
  }
}

const mathService = new MathService();
mathService.calculateSquareRoot(16); // Works fine
mathService.calculateSquareRoot(-4); // Throws a validation error

Complex Object Validation

For more advanced scenarios, TypeSentry integrates with class-validator:

import { Validate, Validator } from 'type-sentry';
import { IsEmail, Length, IsDate, MaxDate } from 'class-validator';

class UserDto {
  @IsEmail()
  email: string;

  @Length(8, 20)
  password: string;

  @IsDate()
  @MaxDate(new Date())
  birthDate: Date;
}

class UserService {
  @Validate()
  registerUser(@Validator(UserDto) user: UserDto) {
    console.log(`Registering user: ${user.email}`);
  }
}

const userService = new UserService();
userService.registerUser({
  email: "[email protected]",
  password: "securepass",
  birthDate: new Date("1990-01-01")
}); // Works fine

userService.registerUser({
  email: "invalid-email",
  password: "short",
  birthDate: new Date("2025-01-01")
}); // Throws a validation error

Flexible Validator Definition

TypeSentry supports both direct class references and factory functions for defining validators. This is particularly useful for avoiding circular dependencies or when dealing with classes that aren't fully defined at decoration time.

import { Validate, Validator } from 'type-sentry';
import { IsEmail, Length } from 'class-validator';

class UserDto {
  @IsEmail()
  email: string;

  @Length(8, 20)
  password: string;
}

class UserService {
  // Using direct class reference
  @Validate()
  registerUser(@Validator(UserDto) user: UserDto) {
    console.log(`Registering user: ${user.email}`);
  }

  // Using factory function
  @Validate()
  updateUser(@Validator(() => UserDto) user: UserDto) {
    console.log(`Updating user: ${user.email}`);
  }
}

API Reference

Decorators

  • @Validate(): Method decorator to enable validation for a method.
  • @Validator(classOrFactory): Parameter decorator to validate complex objects using class-validator. Accepts either a class constructor or a factory function that returns a class constructor.
  • @IsNumber(): Parameter decorator to validate that a parameter is a number.
  • @IsString(): Parameter decorator to validate that a parameter is a string.

Functions

  • createParamValidator(validator: (value: any) => boolean, defaultMessage: string): Creates a custom parameter validator.

Contributing

We welcome contributions to TypeSentry! Please see our Contributing Guide for more details.

License

TypeSentry is MIT licensed.

Support

If you encounter any issues or have questions, please file an issue on the GitHub repository.


Made with ❤️ by Najam Shehzad