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

@shar_hao/guardian

v0.1.0-bate.1

Published

A library for data rule validation.

Readme

guardian-x

A library for data rule validation.

Serve only

guardian-x is exclusively designed for server-side validation of the received data.

Installation and Usage

Server-side usage

Install the library with npm install guardian-x

Example code

Entity

import { IsEmail, IsNumber, IsOptional, IsString, Length, Size } from 'guardian-x';

class User {
  @Length({ min: 5, max: 8 })
  @IsString()
  name: string;
  @Size({ min: 18, max: 120 })
  @IsNumber()
  age: number;
  @IsOptional()
  @IsEmail()
  @IsString()
  email: string | null;

  constructor(name: string, age: number, email: string | null) {
    this.name = name;
    this.age = age;
    this.email = email;
  }
}

Service

import { GMethod, Guardian} from 'guardian-x';

class UserService {
  @GMethod()
  async create(@Guardian() user: User) {
    return user;
  }
}

const userService = new UserService();

userService.create(new User('guardian', 18, '[email protected]')).then((data) => {
  console.log(data); // { name: 'guardian', age: 18, email: '[email protected]' }
});
userService.create(new User('guardian', 18, 'example.com')).then((data) => {
  console.log(data); // [ 'email: Value is not comply with the email address rules.' ]
});
userService.create(new User('gua', 180, null)).then((data) => {
  console.log(data);
  /*
  [
    'name: Length must more than 5 and less than 8',
    'age: Size must more than 18 and less than 120'
  ]
  */
});

Rules

Standard

Here are some type restriction rules.

| Rule | Type | | --------- | ------- | | IsNumber | number | | IsString | string | | IsBoolean | boolean | | IsArray | array | | IsObject | object |

Here are some basic rules.

Note: Basic rules are additional restrictions on type restriction rules and cannot be used alone.

| Rule | Type | Explain | | ---------- | ------ | ------------------------------------------------------------ | | IsOptional | any | Allow the value to be null or undefined. | | Size | number | Restrict the size of the number type. { min?: number, max?: number } \| number | | IsIn | number | Validate whether the value exists in the specified set. number[] | | IsAge | number | Validate whether the value matches the standard age. | | Length | string | Restrict the length of the string type. { min?: number, max?: number } \| number | | IsEmail | string | Validate whether the value conforms to an email address. | | IsPhone | string | Validate whether the value conforms to an phone number. | | IsUrl | string | Validate whether the value conforms to an url. |

Custom

Example Code

import { DataType, GMethod, Guardian, IsEmail, IsNumber, IsOptional, IsString, Length, Size, createDecMethod } from 'guardian-x';

// Custom DecMethod
const IsInteger = createDecMethod(DataType.NUMBER, (value: number) => {
  return Number.isInteger(value) ? null : 'Invalid integer';
})

// Entity
class User {
  @Length({ min: 5, max: 8 })
  @IsString()
  name: string;
  @IsInteger()
  @Size({ min: 18, max: 120 })
  @IsNumber()
  age: number;
  @IsOptional()
  @IsEmail()
  @IsString()
  email: string | null;

  constructor(name: string, age: number, email: string | null) {
    this.name = name;
    this.age = age;
    this.email = email;
  }
}

// Service
class UserService {
  @GMethod()
  async create(@Guardian() user: User) {
    return user;
  }
}

const userService = new UserService();

userService.create(new User('guardian', 18.5, '[email protected]')).then((data) => {
  console.log(data); // [ 'age: Invalid integer' ]
});
userService.create(new User('guardian', 19, '[email protected]')).then((data) => {
  console.log(data); // { name: 'guardian', age: 19, email: '[email protected]' }
});

Options

| Attribute | Type | Explain | Default | | --------------------- | ------------------------ | -----------------------------------------------| ------- | | skipMissingProperties | boolean | Allow the value to be undefined. | false | | buildMethod | (error: string[]) => any | Method for building a validated return object. | - |

Example Code

const guardianOptions: IGuardianOptions = {
  skipMissingProperties: true,
  buildMethod: (error: string[]) => {
    return { msg: error };
  }
};
const GuardianMethod: MethodDecorator = GMethod(guardianOptions);

Epilogue

If you have any valuable suggestions, please contact email [email protected]