validator-experimental-decorators
Version:
A LIGHTWEIGHT TYPESCRIPT/JAVASCRIPT VALIDATOR
148 lines (116 loc) • 3 kB
Markdown
# 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`
##
```javascript
class Person {
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");
```
##
```javascript
class Person {
public password(password: string): string {
return password;
}
}
const person = new Person();
person.password("iamvalidpassword1337");
```
```javascript
class Person {
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");
```
##
-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 {
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");
```
##
```javascript
class Person {
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)
```
##
```javascript
class Person {
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
) {}
public changeEmail(newEmail: string): string {
return (this.email = newEmail);
}
public changeUsername(newUsername: string): string {
return (this.name = newUsername);
}
public changeUserPassword(newPassword: string): string {
return (this.password = newPassword);
}
get age() {
return this.userAge;
}
set _age(newAge: number) {
this.userAge = newAge;
}
}
```