0.0.30 • Published 3 years ago

ajv-class-decorator v0.0.30

Weekly downloads
-
License
MIT
Repository
gitlab
Last release
3 years ago

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

0.0.24

4 years ago

0.0.25

4 years ago

0.0.30

3 years ago

0.0.26

4 years ago

0.0.27

4 years ago

0.0.28

4 years ago

0.0.29

4 years ago

0.0.20

4 years ago

0.0.21

4 years ago

0.0.22

4 years ago

0.0.23

4 years ago

0.0.15

4 years ago

0.0.16

4 years ago

0.0.17

4 years ago

0.0.18

4 years ago

0.0.19

4 years ago

0.0.10

4 years ago

0.0.11

4 years ago

0.0.12

4 years ago

0.0.13

4 years ago

0.0.14

4 years ago

0.0.9

4 years ago

0.0.8

4 years ago

0.0.5

4 years ago

0.0.4

4 years ago

0.0.7

4 years ago

0.0.6

4 years ago

0.0.3

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago