ajv-class-decorator v0.0.30
Description
⚠ WARNING: Library is still under regular development ⚠
Installation
$ npm i ajv-class-decorator
Examples
🧩 Create the validation schema. This is usually the Data Transfer Object.
// creating interface is OPTIONAL
export interface ICreateCatDto {
age: number;
legs: number;
email?: string;
password: string;
name: string;
ipv4address: string;
}
enum Emails {
'haha@email.com',
'hehe@email.com'
}
@AllRequired()
@AdditionalProperties(false)
export class CreateCatDTO implements ICreateCatDto {
@Minimum(1)
@IsInteger32()
public age!: number;
@Maximum(4)
@Minimum(1)
public legs!: number;
@DependentRequired(['password'])
@Enum(Emails)
@IsEmail()
public email?: string;
@IsStrongPassword()
public password!: string;
// 2nd param is Object with 'message' key for custom message
@IsAlphabetic({message: 'this is a message'})
@IsLowercase()
public name!: string;
@IsIPv4()
public ipv4address!: string;
}
🧩 You may choose to initialize the Validator with certain params.
Validator.init({
coerceTypes: true,
removeAdditional: true,
ownProperties: true,
generateCode: true,
outDir: `${path.resolve()}/validators`
});
🧩 Get the validation results of IValidationResults
.
const newCat = new CreateCatDTO();
newCat.age = 1;
newCat.legs = 4;
newCat.email = 'haha1@email.com';
newCat.password = 'P@$$w0rd';
newCat.name = 'hello1';
newCat.ipv4address = '192.168.1.1';
// Optionally use Object.assign() for assigning properties to Class for validation.
// But you'll lose runtime checks for additional properties that don't exist in the Class / Object.
Object.assign(newCat, {hehe: '!@#$%^&*()_+'});
Features:
✅ on-the-fly property validation\ ✅ Modulerise each validation schema\ ✅ Add custom formats\ ✅ Add custom keywords\ ✅ Minify to compress generated validation code\ ✅ Allow adding of directory path for storing generated file to\ ✔️ ❗️Add tests❗️\ ✔️ Benchmarks\ ✔️ Allow async validation\ ✔️ Custom error message (WIP)\ ✔️ Continue to add more validation features (WIP)\ ✔️ Moving window validations - e.g. future date, past date
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago