npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@apizr-io/class-utils

v0.6.0

Published

Package containing all class-validator function with all custom apizr class validation functions

Downloads

2,211

Readme

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

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[];
}

Custom decorators

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;
}

IsRecord

IsRecord(allowedRecordType: RecordAllowedType | RecordAllowedType[], options: IsRecordValidateOptions)

Check if the record is compliant to specified definition.

By default, the key type will be checked to be a string.

Usage

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

class ChildClass {
  @IsString()
  key: string;
}

class ExampleClass {
  @IsRecord('boolean')
  record: Record<string, boolean>;

  @IsRecord(['string', 'undefined'])
  record: Record<string, string | undefined>;

  @IsRecord(ChildClass)
  record: Record<string, ChildClass>;

  @IsRecord('string', { key: 'number' })
  record: Record<number, string>;

  @IsRecord(ChildClass, { each: true })
  records: Record<string, ChildClass>[];

  @IsRecord([ChildClass, ChildClass2, "undefined"])
  record: Record<string, ChildClass | ChildClass2 | undefined>;

  @IsRecord([{ type: ChildClass, array: true }, ChildClass2])
  record: Record<string, ChildClass[] | ChildClass2>;

  @IsRecord([{ type: [ChildClass, ChildClass2], array: true }, ChildClass3])
  record: Record<string, (Class1 | ChildClass2)[] | ChildClass3>;
}