0.0.30 • Published 2 years ago

ajv-class-decorator v0.0.30

Weekly downloads
-
License
MIT
Repository
gitlab
Last release
2 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

2 years ago

0.0.25

2 years ago

0.0.30

2 years ago

0.0.26

2 years ago

0.0.27

2 years ago

0.0.28

2 years ago

0.0.29

2 years ago

0.0.20

2 years ago

0.0.21

2 years ago

0.0.22

2 years ago

0.0.23

2 years ago

0.0.15

2 years ago

0.0.16

2 years ago

0.0.17

2 years ago

0.0.18

2 years ago

0.0.19

2 years ago

0.0.10

2 years ago

0.0.11

2 years ago

0.0.12

2 years ago

0.0.13

2 years ago

0.0.14

2 years ago

0.0.9

2 years ago

0.0.8

3 years ago

0.0.5

3 years ago

0.0.4

3 years ago

0.0.7

3 years ago

0.0.6

3 years ago

0.0.3

3 years ago

0.0.2

3 years ago

0.0.1

3 years ago