A library for data rule validation.
guardian-x is exclusively designed for server-side validation of the received data.
Install the library with npm install guardian-x
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'
]
*/
});
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. |
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' }
});
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);
If you have any valuable suggestions, please contact email sharhao2002@gmail.com