0.1.0-bate.1 • Published 8 months ago

@shar_hao/guardian v0.1.0-bate.1

Weekly downloads
-
License
ISC
Repository
-
Last release
8 months ago

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, 'guardian@example.com')).then((data) => {
  console.log(data); // { name: 'guardian', age: 18, email: 'guardian@example.com' }
});
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.

RuleType
IsNumbernumber
IsStringstring
IsBooleanboolean
IsArrayarray
IsObjectobject

Here are some basic rules.

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

RuleTypeExplain
IsOptionalanyAllow the value to be null or undefined.
SizenumberRestrict the size of the number type. { min?: number, max?: number } \| number
IsInnumberValidate whether the value exists in the specified set. number[]
IsAgenumberValidate whether the value matches the standard age.
LengthstringRestrict the length of the string type. { min?: number, max?: number } \| number
IsEmailstringValidate whether the value conforms to an email address.
IsPhonestringValidate whether the value conforms to an phone number.
IsUrlstringValidate 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, 'guardian@example.com')).then((data) => {
  console.log(data); // [ 'age: Invalid integer' ]
});
userService.create(new User('guardian', 19, 'guardian@example.com')).then((data) => {
  console.log(data); // { name: 'guardian', age: 19, email: 'guardian@example.com' }
});

Options

AttributeTypeExplainDefault
skipMissingPropertiesbooleanAllow the value to be undefined.false
buildMethod(error: string[]) => anyMethod 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 sharhao2002@gmail.com

0.1.0-bate.1

8 months ago