UNPKG

validator-experimental-decorators

Version:
148 lines (116 loc) 3 kB
# Quick start To install this package in your project, run the following command in your terminal. ```bash npm i validator-experimental-decorators ``` # The package provides the following tools -`Length` -`validateNegativeNumber` -`validatePositiveNumber` -`isEmail` -`isValidPassword` ## @isEmail ```javascript 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("example@gmail.com"); ``` ## @isValidPassword ```javascript class Person { @isValidPassword public password(password: string): string { return password; } } const person = new Person(); person.password("iamvalidpassword1337"); ``` ```javascript 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. ```javascript 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 ```javascript 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 ```javascript 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 ```javascript 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; } } ```