Skip to content

Lightweight TypeScript library for robust runtime type checking and validation. Utilizes decorators for easy integration, supports complex object validation, and allows custom validators. Enhance your TypeScript projects with minimal overhead.

License

Notifications You must be signed in to change notification settings

NajamShehzad/type-sentry

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

21 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

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

About

Lightweight TypeScript library for robust runtime type checking and validation. Utilizes decorators for easy integration, supports complex object validation, and allows custom validators. Enhance your TypeScript projects with minimal overhead.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published