0.4.1 • Published 9 months ago

@apizr-io/class-utils v0.4.1

Weekly downloads
-
License
ISC
Repository
gitlab
Last release
9 months ago

class-utils

This package contains all class-validator and class-tranformer functions with custom class decorators that can be used to validate a class.

Usage

import { IsString } from '@apizr/class-utils/validators';
import { Transform } from '@apizr/class-utils/transformers';

class MyClass {
  @IsString()
  myString: string;

  @Transform((value) => value === 'true')
  myBoolean: boolean;
}

Dependencies

This package depends on jsonpath-plus to be able to use some decorators (e.g. ValidateSubKeyExistIf). See documentation for more information.

class-validator decorators and class-transformer

See documentation of class-validator for the list of default exports. See documentation of class-transformer for the list of default export.

Custom transformers

Custom decorators

SplitString

SplitString(separator: string = ',')

Will transform a string received to an array of string split by separator.

Usage

import { SplitString } from '@apizr/class-utils/tranformers';

class MyStringClass {
  @SplitString()
  myString: string[];
}

IsBeforeDate

IsBeforeDate(property: string, options: ValidationOptions)

Checks if the date is before the date present in the same objet at the specified key.

Usage

import { IsBeforeDate } from '@apizr/class-utils/validators';

class Dates {
  @IsBeforeDate('end')
  start: Date;

  end: Date;
}

If the other date is optional, you can specify a flag to prevent an error if no date is present

import { IsBeforeDate } from '@apizr/class-utils/validators';

class Dates {
  @IsBeforeDate('end', { context: { otherDateIsOptional: true } })
  start: Date;

  end?: Date;
}

IsAfterDate

IsAfterDate(property: string, options: ValidationOptions)

Checks if the date is after the date present in the same objet at the specified key.

Usage

import { IsAfterDate } from '@apizr/class-utils/validators';

class Dates {
  start: Date;

  @IsAfterDate('start')
  end: Date;
}

If the other date is optional, you can specify a flag to prevent an error if no date is present

import { IsAfterDate } from '@apizr/class-utils/validators';

class Dates {
  start?: Date;

  @IsAfterDate('start', { context: { otherDateIsOptional: true } })
  end: Date;
}

IsFutureDate

IsFutureDate(void)

Checks if the date is in the future.

Usage

import { IsFutureDate } from '@apizr/class-utils/validators';

class Date {
  @IsFutureDate()
  end: Date;
}

StartsWith

StartsWith(startValue: string | string[])

Checks if the string starts with the specified value

Usage

import { StartsWith } from '@apizr/class-utils/validators';

class Order {
  @StartsWith('PROD-')
  id: string;
}

OneOfKeysIsDefined

OneOfKeysIsDefined(keysToValidate: string[])

Checks if at least one of the key has value (not null or undefined).

Usage

import { OneOfKeysIsDefined } from '@apizr/class-utils/validators';

class Product {
  id: string;
  description?: string;
  name?: string;
}

class Order {
  @ValidateNested({ each: true })
  @Type(() => Product)
  @OneOfKeysIsDefined(['description', 'name'], { each: true })
  products: Product[];
}

ValidateSort

ValidateSort(allowedSort: string | string[])

Checks that the string array contains valid keys with valid sorting directions (asc or desc).

usage

import { ValidateSort } from '@apizr/class-utils/validators';

class Filters {
  @ValidateSort(['date.creation', 'date.modification'])
  sort: string[];
}

This will allow date.creation:asc or date.modification:desc for example.

ValidateAtLeastOneKeyGroupExists

ValidateAtLeastOneKeyGroupExists(groups: string[][])

Checks if one of the keys groups exist in class.

Usage

@ValidateAtLeastOneKeyGroupExists([
  ['key1', 'key2'],
  ['key2', 'key3']
])

Will validate that (key1 and key2) or (key2 and key3) exists in the class.

Usage

import { ValidateAtLeastOneKeyGroupExists } from '@apizr/class-utils/validators';

@ValidateAtLeastOneKeyGroupExists([
  ['email', 'birthday'],
  ['firstname', 'lastname'],
])
class Customer {
  email: string;

  firstname: string;

  lastname: string;

  birthday: string;
}

ValidateOnlyOneKeyGroupExists

ValidateOnlyOneKeyGroupExists(groups: string[][])

Checks if keys of a key groups exist in class.

Usage

@ValidateOneKeyGroupExists([
  ['key1', 'key2'],
  ['key2', 'key3']
])

Will validate that (key1 and key2) xor (exclusive or) (key2 and key3) exists in the class.

Usage

import { ValidateOnlyOneKeyGroupExists } from '@apizr/class-utils/validators';

@ValidateOnlyOneKeyGroupExists([
  ['email', 'birthday'],
  ['firstname', 'lastname'],
])
class Customer {
  email: string;

  firstname: string;

  lastname: string;

  birthday: string;
}

ValidateSubKeyExistIf

ValidateSubKeyExistIf(path: string, condition: (value: any) => boolean)

Allows validating the presence of a specified key in a child object based on a given condition. The child value described by the path option must have the @IsOptional() decorator (see example below).

Usage

import { ValidateSubKeyExistIf } from '@apizr/class-utils/validators';

class ChildClass {
    @IsOptional() // Mandatory when this decorator is used
    targetedValue?: any;
}

@ValidateSubKeyExistIf('child.targetedValue',(o) => o.conditionalValue !== false)
class ParentClass {
    @IsString()
    conditionalValue: boolean;

    @ValidateNested({ each: true })
    @Type(() => ChildClass)
    child: ChildClass;
}

You can also use jsonpath-plus to validate a key in an array of objects:

import { ValidateSubKeyExistIf } from '@apizr/class-utils/validators';

class LittleChildClass {
    @IsOptional() // Mandatory when this decorator is used
    targetedValue?: string;
}

class ChildClass {
    @IsOptional()
    @ValidateNested({ each: true })
    @Type(() => LittleChildClass)
    myOtherValue?: LittleChildClass[];
}

@ValidateSubKeyExistIf('child.myOtherValue[*].targetedValue',(o) => o.conditionalValue !== false)
class ParentClass {
    conditionalValue: boolean;
    @ValidateNested({ each: true })
    @Type(() => ChildClass)
    child: ChildClass;
}
0.3.0

10 months ago

0.4.1

9 months ago

0.4.0

10 months ago

0.2.0

2 years ago

0.1.4

2 years ago

0.1.3

2 years ago