@akibrk/validator
v2.3.1
Published
Lightweight validation library for typescript
Downloads
50
Maintainers
Readme
Validator
Validate typescript based on schema, class models or decorators!
Release Notes
Usage
There are three main strategies for validation currently available in this library,
- Schema Based
- Class Decorator Based
- Class Model Based
1. Schema Based
import { ValidatorSchema, Type, ValidationError, validate } from '@akibrk/validator';
const registerForm: ValidatorSchema = {
username: {
type: Type.string,
minLength: 4,
maxLength: 50,
},
password: {
type: Type.string,
minLength: 6,
},
email: {
type: Type.email,
maxLength: 100,
},
};
const formData: any = {
username: 'akibrk',
password: '123456',
email: 'me@[email protected]',
};
validate(formData, registerForm);
2. Decorator Based
Before using decorators please check your ts.config file to see if the decorator is enabled under compiler options "experimentalDecorators": true
As of May,22 it's still in experimental flag.
Limitations: You cannot apply multiple decorator to same property at the moment, I will try to get that working next.
import { between, isEmail, required } from '@akibrk/validator';
class CaseClass {
@between(1, 122.33)
public check: number;
@isEmail()
public email: string;
@required()
public acceptTerms: boolean;
constructor() {
this.check = AMOUNT;
this.email = '[email protected]';
this.acceptTerms = true;
}
}
Available decorators
- required
- isEmail
- between
- minLength
- maxLength
- length
- isMobile
3. Class Model/ Interface
import { Validator } from '@akibrk/validator';
class Login {
public email: string = '';
public password: string = '';
}
// Or
interface ILogin {
email: string;
password: string;
}
const loginValidator = new Validator<Login>({
email: {
type: Type.string,
required: true,
},
password: {
type: Type.string,
},
});
loginValidator.validate({
email: 'null',
});