validator-experimental-decorators
v1.0.8-1
Published
A LIGHTWEIGHT TYPESCRIPT/JAVASCRIPT VALIDATOR
Downloads
6
Maintainers
Readme
Quick start
To install this package in your project, run the following command in your terminal.
npm i validator-experimental-decorators
The package provides the following tools
-Length
-validateNegativeNumber
-validatePositiveNumber
-isEmail
-isValidPassword
@isEmail
class Person {
@isEmail
email(input: string) {
return input;
}
}
const person = new Person();
//If you do not provide a valid email address here, the validator library will throw an error.
person.email("[email protected]");
@isValidPassword
class Person {
@isValidPassword
public password(password: string): string {
return password;
}
}
const person = new Person();
person.password("iamvalidpassword1337");
class Person {
@isValidPassword
public password(password: string): string {
return password;
}
}
const person = new Person();
//If you do not provide a valid password here, the validator library will throw an error.
person.password("iamvalidpassword1337");
@Length
-For the "Length(min, max)" function, you need to specify the range by providing the minimum value first, followed by the maximum value.
class Person {
@Length(0,10)
public password(password: string): string {
return password;
}
}
const person = new Person();
//If you do not provide a valid password here, the validator library will throw an error.
person.password("iamvalidpassword1337");
@validateNegativeNumber
class Person {
@validateNegativeNumber
public number(number: string): number {
return number;
}
}
const person = new Person();
// If you enter a positive number, the validator will throw an error.
person.number(-1337)
@validatePositiveNumber
class Person {
@validatePositiveNumber
public number(number: string): number {
return number;
}
}
const person = new Person();
// If you enter a negative number, the validator will throw an error.
person.number(1337)
Example Usage
const {
Length,
isEmail,
isValidPassword,
validateNegativeNumber,
validatePositiveNumber,
} = new Validator();
class Person {
constructor(
public name: string,
public email: string,
public password: string,
public userAge: number
) {}
@isEmail
public changeEmail(newEmail: string): string {
return (this.email = newEmail);
}
@Length(0, 10)
public changeUsername(newUsername: string): string {
return (this.name = newUsername);
}
@isValidPassword
public changeUserPassword(newPassword: string): string {
return (this.password = newPassword);
}
@validatePositiveNumber
get age() {
return this.userAge;
}
@validatePositiveNumber
set _age(newAge: number) {
this.userAge = newAge;
}
}