Options
All
  • Public
  • Public/Protected
  • All
Menu

class-validator-extended - v0.0.4

Index

Type

IsBigInt

  • IsBigInt(options?: ValidationOptions): PropertyDecorator

IsDayjs

  • IsDayjs(options?: Partial<IsDayjsOptions> & ValidationOptions): PropertyDecorator
  • Checks if the given value is a Dayjs object.

    Note that dayjs is not a constructor, so you can't simply use @IsInstance(Dayjs).

    Example

    // Ensure the value is a Dayjs object.
    @IsDayjs()
    dateOfBirth: Dayjs

    Parameters

    • options: Partial<IsDayjsOptions> & ValidationOptions = {}

      Accepts the following options (in addition to generic class-validator options):

      • is_valid: boolean = true If true, checks that the Dayjs object is valid.

    Returns PropertyDecorator

IsDuration

  • IsDuration(options?: ValidationOptions): PropertyDecorator
  • Checks if the given value is a Dayjs Duration object.

    This requires the Duration-plugin to be loaded. If this is not the case, an error will be thrown.

    Example

    // Ensure the value is a Dayjs Duration object.
    @IsDuration()
    value: Duration

    Parameters

    • Optional options: ValidationOptions

      Generic class-validator options.

    Returns PropertyDecorator

IsMap

  • IsMap(options?: ValidationOptions): PropertyDecorator
  • Checks if the given value is a Map.

    This is a convenience decorator for @IsInstance(Map).

    Example

    // Ensure the value is a Map.
    @IsMap()
    values: Map<string, string>

    Parameters

    • Optional options: ValidationOptions

      Generic class-validator options.

    Returns PropertyDecorator

IsSet

  • IsSet(options?: ValidationOptions): PropertyDecorator
  • Checks if the given value is a Set.

    This is a convenience decorator for @IsInstance(Set).

    Example

    // Ensure the value is a Set.
    @IsSet()
    values: Set<string>

    Parameters

    • Optional options: ValidationOptions

      Generic class-validator options.

    Returns PropertyDecorator

Array

ArrayMonotonic

  • Checks if the given value is an array sorted in either (strictly) ascending or (strictly) descending order.

    This validator will always pass if the given value is an empty array.

    Example

    // Ensure the array is sorted in reverse order of dates of birth.
    @ArrayMonotonic({
    monotonicity: Monotonicity.WEAKLY_DECREASING,
    projection: (user: User) => user.dateOfBirth.getTime()
    })
    users: User[]
    // Ensure the array is sorted in ascending order, allowing no duplicates.
    @ArrayMonotonic({
    monotonicity: Monotonicity.STRICTLY_INCREASING,
    comparator: (a, b) => a - b
    })
    values: number[]

    Type parameters

    • T = unknown

      The type of the array elements.

    Parameters

    • options: ({ projection: ArrayMonotonicityProjection<T> } & { monotonicity: Monotonicity } & ValidationOptions) & ({ comparator: ArrayMonotonicityComparator<T> } & { monotonicity: Monotonicity } & ValidationOptions)

      Accepts the following options (in addition to generic class-validator options):

      • monotonicity: Monotonicity The required ordering of the array.
      • projection: (element) => number (mutually exclusive with comparator) Project an array element to a number which is then used for comparison.
      • comparator: (a, b) => number (mutually exclusive with projection) Directly compare two elements (as in Array.sort).

    Returns PropertyDecorator

BigInt

MaxBigInt

  • MaxBigInt(maximum: number | BigInt, options?: ValidationOptions): PropertyDecorator
  • Checks if the given value is a BigInt not greater than maximum.

    Example

    // Ensure the value is less than 9000.
    @MaxBigInt(8_999)
    underNineThousand: BigInt

    Parameters

    • maximum: number | BigInt

      The maximum allowed value.

    • Optional options: ValidationOptions

      Generic class-validator options.

    Returns PropertyDecorator

MinBigInt

  • MinBigInt(minimum: number | BigInt, options?: ValidationOptions): PropertyDecorator
  • Checks if the given value is a BigInt not less than minimum.

    Example

    // Ensure the value is greater than 9000.
    @MinBigInt(9_001)
    overNineThousand: BigInt

    Parameters

    • minimum: number | BigInt

      The minimum allowed value.

    • Optional options: ValidationOptions

      Generic class-validator options.

    Returns PropertyDecorator

NegativeBigInt

  • NegativeBigInt(options?: ValidationOptions): PropertyDecorator

PositiveBigInt

  • PositiveBigInt(options?: ValidationOptions): PropertyDecorator

Date

FutureDate

  • FutureDate(options?: ValidationOptions): PropertyDecorator
  • Checks if the given value is a Date object in the future.

    Beware that the behaviour of this check depends on the current time and can thus be difficult to test. In particular, as time goes by the property can become invalid without ever changing its value.

    Example

    // Ensure the value is in the future.
    @FutureDay()
    scheduledFor: Date

    Parameters

    • Optional options: ValidationOptions

      Generic class-validator options.

    Returns PropertyDecorator

PastDate

  • PastDate(options?: ValidationOptions): PropertyDecorator
  • Checks if the given value is a Date object in the past.

    Beware that the behaviour of this check depends on the current time and can thus be difficult to test.

    Example

    // Ensure the value is in the past.
    @PastDate()
    created: Date

    Parameters

    • Optional options: ValidationOptions

      Generic class-validator options.

    Returns PropertyDecorator

Dayjs

FutureDayjs

  • FutureDayjs(options?: ValidationOptions): PropertyDecorator
  • Checks if the given value is a Dayjs object in the future.

    Beware that the behaviour of this check depends on the current time and can thus be difficult to test. In particular, as time goes by the property can become invalid without ever changing its value.

    Example

    // Ensure the value is in the future.
    @FutureDayjs()
    scheduledFor: Dayjs

    Parameters

    • Optional options: ValidationOptions

      Generic class-validator options.

    Returns PropertyDecorator

MaxDayjs

  • MaxDayjs(maximum: undefined | null | string | number | Date | Dayjs, options?: ValidationOptions): PropertyDecorator
  • Checks if the given value is a Dayjs object not later than maximum.

    Example

    // Ensure the value is before the infamous Y2K.
    @MaxDayjs('2000-01-01T00:00:00.000Z')
    y2kSafeDate: Dayjs

    Parameters

    • maximum: undefined | null | string | number | Date | Dayjs

      The maximum allowed value.

    • Optional options: ValidationOptions

      Generic class-validator options.

    Returns PropertyDecorator

MinDayjs

  • MinDayjs(minimum: undefined | null | string | number | Date | Dayjs, options?: ValidationOptions): PropertyDecorator
  • Checks if the given value is a Dayjs object not earlier than minimum.

    Example

    // Ensure the value is after the infamous Y2K.
    @MinDayjs('2000-01-01T00:00:00.000Z')
    y2kUnsafeDate: Dayjs

    Parameters

    • minimum: undefined | null | string | number | Date | Dayjs

      The minimum allowed value.

    • Optional options: ValidationOptions

      Generic class-validator options.

    Returns PropertyDecorator

PastDayjs

  • PastDayjs(options?: ValidationOptions): PropertyDecorator
  • Checks if the given value is a Dayjs object in the past.

    Beware that the behaviour of this check depends on the current time and can thus be difficult to test.

    Example

    // Ensure the value is in the past.
    @PastDayjs()
    created: Dayjs

    Parameters

    • Optional options: ValidationOptions

      Generic class-validator options.

    Returns PropertyDecorator

Map

MapContains

  • MapContains<Value>(required: Iterable<Value>, options?: ValidationOptions): PropertyDecorator
  • Checks if the given value is a Map and contains all required values.

    Example

    // Ensure the map contains (at least) 'foo' and 'bar'.
    @MapContains(['foo', 'bar'])
    values: Map<number, string>

    Type parameters

    • Value = unknown

      The type of values to check for.

    Parameters

    • required: Iterable<Value>

      The values required in the given map.

    • Optional options: ValidationOptions

      Generic class-validator options.

    Returns PropertyDecorator

MapContainsKeys

  • MapContainsKeys<Key>(required: Key[], options?: ValidationOptions): PropertyDecorator
  • Checks if the given value is a Map and contains all required keys.

    Example

    // Ensure the map contains (at least) the keys 13 and 42.
    @MapContainsKeys([13, 42])
    values: Map<number, string>

    Type parameters

    • Key = unknown

      The type of keys to check for.

    Parameters

    • required: Key[]

      The keys required in the given map.

    • Optional options: ValidationOptions

      Generic class-validator options.

    Returns PropertyDecorator

MapMaxSize

  • MapMaxSize(maximum: number, options?: ValidationOptions): PropertyDecorator
  • Checks if the given value is a Map with no more than maximum entries.

    Example

    // Ensure the map has at most 5 entries.
    @MapMaxSize(5)
    values: Map<string, string>

    Parameters

    • maximum: number

      The maximum allowed size of the map.

    • Optional options: ValidationOptions

      Generic class-validator options.

    Returns PropertyDecorator

MapMinSize

  • MapMinSize(minimum: number, options?: ValidationOptions): PropertyDecorator
  • Checks if the given value is a Map with no fewer than minimum entries.

    Example

    // Ensure the map has at least 5 entries.
    @MapMinSize(5)
    values: Map<string, string>

    Parameters

    • minimum: number

      The minimum allowed size of the map.

    • Optional options: ValidationOptions

      Generic class-validator options.

    Returns PropertyDecorator

MapNotContains

  • MapNotContains<Value>(forbidden: Iterable<Value>, options?: ValidationOptions): PropertyDecorator
  • Checks if the given value is a Map which does not contain any of the forbidden values.

    Example

    // Ensure the map contains does not include the values 'foo' and 'bar'.
    @MapNotContains(['foo', 'bar'])
    values: Map<number, string>

    Type parameters

    • Value = unknown

      The type of values to check for.

    Parameters

    • forbidden: Iterable<Value>

      The values forbidden in the given map.

    • Optional options: ValidationOptions

      Generic class-validator options.

    Returns PropertyDecorator

MapNotContainsKeys

  • MapNotContainsKeys<Key>(forbidden: Iterable<Key>, options?: ValidationOptions): PropertyDecorator
  • Checks if the given value is a Map which does not contain any of the forbidden keys.

    Example

    // Ensure the map contains does not include the keys 13 and 42.
    @MapNotContainsKeys(13, 42)
    values: Map<number, string>

    Type parameters

    • Key = unknown

      The type of keys to check for.

    Parameters

    • forbidden: Iterable<Key>

      The values forbidden in the given map.

    • Optional options: ValidationOptions

      Generic class-validator options.

    Returns PropertyDecorator

MapNotEmpty

  • MapNotEmpty(options?: ValidationOptions): PropertyDecorator

MapUnique

  • MapUnique<Value, Projection>(projection: MapUniqueProjection<Value, Projection>, options?: ValidationOptions): PropertyDecorator
  • Checks if the given value is a Map without duplicates with regard to the given projection.

    Example

    // Ensure the map does not include duplicate users.
    @MapUnique<User>(user => user.id)
    usersByEmail: Map<string, User>

    Type parameters

    • Value = unknown

      The type of the map's values.

    • Projection = Value

      The type returned by projection.

    Parameters

    • projection: MapUniqueProjection<Value, Projection>

      The function mapping each map entry to the value that is used for the uniqueness check.

    • Optional options: ValidationOptions

      Generic class-validator options.

    Returns PropertyDecorator

MapUniqueKeys

  • MapUniqueKeys<Key, Projection>(projection: MapUniqueKeysProjection<Key, Projection>, options?: ValidationOptions): PropertyDecorator
  • Checks if the given value is a Map whose keys are all unique with regard to the given projection.

    Example

    // Ensure there are no duplicate user ids in the map.
    @MapUniqueKeys<User>(user => user.id)
    postsByUser: Map<User, Post[]>

    Type parameters

    • Key = unknown

      The type of the map's keys.

    • Projection = unknown

      The type returned by projection.

    Parameters

    • projection: MapUniqueKeysProjection<Key, Projection>

      The function mapping each key to the value that is used for the uniqueness check.

    • Optional options: ValidationOptions

      Generic class-validator options.

    Returns PropertyDecorator

Set

SetContains

  • SetContains<Value>(required: Iterable<Value>, options?: ValidationOptions): PropertyDecorator
  • Checks if the given value is a Set and contains all required values.

    Example

    // Ensure the set contains (at least) 'foo' and 'bar'.
    @SetContains(['foo', 'bar'])
    values: Set<string>

    Type parameters

    • Value = unknown

      The type of values to check for.

    Parameters

    • required: Iterable<Value>

      The values required in the given set.

    • Optional options: ValidationOptions

      Generic class-validator options.

    Returns PropertyDecorator

SetMaxSize

  • SetMaxSize(maximum: number, options?: ValidationOptions): PropertyDecorator
  • Checks if the given value is a Set with no more than maximum values.

    Example

    // Ensure the set has at most 5 entries.
    @SetMaxSize(5)
    values: Set<string>

    Parameters

    • maximum: number

      The maximum allowed size of the set.

    • Optional options: ValidationOptions

      Generic class-validator options.

    Returns PropertyDecorator

SetMinSize

  • SetMinSize(minimum: number, options?: ValidationOptions): PropertyDecorator
  • Checks if the given value is a Set with no fewer than minimum values.

    Example

    // Ensure the set has at least 5 entries.
    @SetMinSize(5)
    values: Set<string>

    Parameters

    • minimum: number

      The minimum allowed size of the set.

    • Optional options: ValidationOptions

      Generic class-validator options.

    Returns PropertyDecorator

SetNotContains

  • SetNotContains<Value>(forbidden: Iterable<Value>, options?: ValidationOptions): PropertyDecorator
  • Checks if the given value is a Set which does not contain any of the forbidden values.

    Example

    // Ensure the set contains does not include the values 'foo' and 'bar'.
    @SetNotContains(['foo', 'bar'])
    values: Set<string>

    Type parameters

    • Value = unknown

      The type of values to check for.

    Parameters

    • forbidden: Iterable<Value>

      The values forbidden in the given set.

    • Optional options: ValidationOptions

      Generic class-validator options.

    Returns PropertyDecorator

SetNotEmpty

  • SetNotEmpty(options?: ValidationOptions): PropertyDecorator
  • Checks if the given value is a Set with at least one value.

    Example

    // Ensure the set is not empty.
    @SetNotEmpty()
    values: Set<string>

    Parameters

    • Optional options: ValidationOptions

      Generic class-validator options.

    Returns PropertyDecorator

SetUnique

  • SetUnique<Value, Projection>(projection: SetUniqueProjection<Value, Projection>, options?: ValidationOptions): PropertyDecorator
  • Checks if the given value is a Set without duplicate values with regard to the given projection.

    Example

    // Ensure the set does not include duplicate users.
    @SetUnique<User>(user => user.id)
    administrators: Set<User>

    Type parameters

    • Value = unknown

      The type of the set's values.

    • Projection = unknown

      The type returned by projection.

    Parameters

    • projection: SetUniqueProjection<Value, Projection>

      The function mapping each value to the value that is used for the uniqueness check.

    • Optional options: ValidationOptions

      Generic class-validator options.

    Returns PropertyDecorator

String

IsAwsARN

  • IsAwsARN(options?: ValidationOptions): PropertyDecorator
  • Checks if the given value is an AWS ARN string (as specified in the AWS Documentation).

    Beware that this decorator only checks for syntactic validity, it does not check if the partition, service or region really exists. The region and account id are always optional, even if required for the specific service.

    For example, the following strings are considered valid if though they are technically not:

    • arn:aws:foo:eu-central-1:bar (there is no foo AWS service)
    • arn:aws:ec2::123456789012:instance/i-1234567890abcdef0 (EC2 requires a region which is omitted)
    • arn:aws:ec2:us-east-42:123456789012:instance/i-1234567890abcdef0 (th region us-east-42 does not exist)

    Example

    // Ensure the value is a valid looking AWS ARN string
    @IsAwsARN()
    arn: string = 'arn:aws:clouddirectory:us-west-2:123456789012:schema/development/cognito'

    Parameters

    • Optional options: ValidationOptions

      Generic class-validator options.

    Returns PropertyDecorator

IsAwsRegion

  • IsAwsRegion(options?: ValidationOptions): PropertyDecorator
  • Checks if the given value is an AWS region string.

    Beware that this decorator only checks for syntactic validity, it does not check if the region really exists. For instance, it will accept "eu-central-9" which - at the time of writing - does not exist.

    Example

    // Ensure the value is a valid looking AWS region
    @IsAwsRegion()
    region: string = 'eu-central-1'

    Parameters

    • Optional options: ValidationOptions

      Generic class-validator options.

    Returns PropertyDecorator

IsTimezone

  • IsTimezone(options?: ValidationOptions): PropertyDecorator
  • Checks if the given value is a valid timezone string.

    Uses Intl internally for validation.

    Example

    // Ensure the value is a valid timezone.
    @IsTimezone()
    timezone: string = 'Pacific/Guam'

    Parameters

    • Optional options: ValidationOptions

      Generic class-validator options.

    Returns PropertyDecorator

Predicates

arrayMonotonic

futureDate

  • futureDate(value: unknown): value is Date

futureDayjs

  • futureDayjs(value: unknown): value is Dayjs

isAwsARN

  • isAwsARN(value: unknown): value is string

isAwsRegion

  • isAwsRegion(value: unknown): value is string

isBigInt

  • isBigInt(value: unknown): value is BigInt

isDayjs

isDuration

  • isDuration(value: unknown): value is Duration

isMap

  • isMap(value: unknown): value is Map<unknown, unknown>

isSet

  • isSet(value: unknown): value is Set<unknown>

isTimezone

  • isTimezone(value: unknown): value is string

mapContains

  • mapContains<Value>(value: unknown, required: Iterable<Value>): value is Map<unknown, unknown>
  • Type parameters

    • Value = unknown

      The type of values to check for.

    Parameters

    • value: unknown

      The value to validate.

    • required: Iterable<Value>

      List of mandatory values for this map.

    Returns value is Map<unknown, unknown>

mapContainsKeys

  • mapContainsKeys<Key>(value: unknown, required: Iterable<Key>): value is Map<unknown, unknown>
  • Type parameters

    • Key = unknown

      The type of keys to check for.

    Parameters

    • value: unknown

      The value to validate.

    • required: Iterable<Key>

      List of mandatory keys for this set.

    Returns value is Map<unknown, unknown>

mapMaxSize

  • mapMaxSize(value: unknown, maximum: number): value is Map<unknown, unknown>

mapMinSize

  • mapMinSize(value: unknown, minimum: number): value is Map<unknown, unknown>

mapNotContains

  • mapNotContains<Value>(value: unknown, forbidden: Iterable<Value>): value is Map<unknown, unknown>
  • Type parameters

    • Value = unknown

      The type of values to check for.

    Parameters

    • value: unknown

      The value to validate.

    • forbidden: Iterable<Value>

      List of forbidden values for this map.

    Returns value is Map<unknown, unknown>

mapNotContainsKeys

  • mapNotContainsKeys<Key>(value: unknown, forbidden: Iterable<Key>): value is Map<unknown, unknown>

mapNotEmpty

  • mapNotEmpty(value: unknown): value is Map<unknown, unknown>

mapUnique

  • mapUnique<Value, Projection>(value: unknown, projection: MapUniqueProjection<Value, Projection>): value is Map<unknown, unknown>
  • Type parameters

    • Value

      The type of the map's values.

    • Projection

      The type returned by projection.

    Parameters

    • value: unknown

      The value to validate.

    • projection: MapUniqueProjection<Value, Projection>

      The function mapping each value to the value that is used for the uniqueness check.

    Returns value is Map<unknown, unknown>

mapUniqueKeys

  • mapUniqueKeys<Key, Projection>(value: unknown, projection: MapUniqueKeysProjection<Key, Projection>): value is Map<unknown, unknown>
  • Type parameters

    • Key

      The type of the map's keys.

    • Projection

      The type returned by projection.

    Parameters

    • value: unknown

      The value to validate.

    • projection: MapUniqueKeysProjection<Key, Projection>

      The function mapping each key to the value that is used for the uniqueness check.

    Returns value is Map<unknown, unknown>

maxBigInt

  • maxBigInt(value: unknown, maximum: number | BigInt): value is BigInt

maxDayjs

  • maxDayjs(value: unknown, maximum: undefined | null | string | number | Date | Dayjs): value is Dayjs
  • Parameters

    • value: unknown

      The value to validate.

    • maximum: undefined | null | string | number | Date | Dayjs

      The maximum allowed value.

    Returns value is Dayjs

minBigInt

  • minBigInt(value: unknown, minimum: number | BigInt): value is BigInt

minDayjs

  • minDayjs(value: unknown, minimum: undefined | null | string | number | Date | Dayjs): value is Dayjs
  • Parameters

    • value: unknown

      The value to validate.

    • minimum: undefined | null | string | number | Date | Dayjs

      The minimum allowed value.

    Returns value is Dayjs

negativeBigInt

  • negativeBigInt(value: unknown): value is BigInt

pastDate

  • pastDate(value: unknown): value is Date

pastDayjs

  • pastDayjs(value: unknown): value is Dayjs

positiveBigInt

  • positiveBigInt(value: unknown): value is BigInt

setContains

  • setContains<Value>(value: unknown, required: Iterable<Value>): value is Set<unknown>
  • Type parameters

    • Value

      The type of values to check for.

    Parameters

    • value: unknown

      The value to validate.

    • required: Iterable<Value>

      List of mandatory values for this set.

    Returns value is Set<unknown>

setMaxSize

  • setMaxSize(value: unknown, maximum: number): value is Set<unknown>

setMinSize

  • setMinSize(value: unknown, minimum: number): value is Set<unknown>

setNotContains

  • setNotContains<Value>(value: unknown, forbidden: Iterable<Value>): value is Set<unknown>
  • Type parameters

    • Value = unknown

      The type of values to check for.

    Parameters

    • value: unknown

      The value to validate.

    • forbidden: Iterable<Value>

      List of forbidden values for this set.

    Returns value is Set<unknown>

setNotEmpty

  • setNotEmpty(value: unknown): value is Set<unknown>

setUnique

  • setUnique<Value, Projection>(value: unknown, projection: SetUniqueProjection<Value, Projection>): value is Set<Value>
  • Type parameters

    • Value

      The type of the set's values.

    • Projection

      The type returned by projection.

    Parameters

    • value: unknown

      The value to validate.

    • projection: SetUniqueProjection<Value, Projection>

      The function mapping each value to the value that is used for the uniqueness check.

    Returns value is Set<Value>

Types

ArrayMonotonicOptions

ArrayMonotonicOptions<T>: ArrayMonotonicityProjectionOrComparator<T> & { monotonicity: Monotonicity }

Type parameters

  • T

ArrayMonotonicityComparator

ArrayMonotonicityComparator<T>: (a: T, b: T) => number

Type parameters

  • T

Type declaration

    • (a: T, b: T): number
    • Parameters

      • a: T
      • b: T

      Returns number

ArrayMonotonicityProjection

ArrayMonotonicityProjection<T>: (item: T) => number

Type parameters

  • T

Type declaration

    • (item: T): number
    • Parameters

      • item: T

      Returns number

ArrayMonotonicityProjectionOrComparator

ArrayMonotonicityProjectionOrComparator<T>: { projection: ArrayMonotonicityProjection<T> } | { comparator: ArrayMonotonicityComparator<T> }

Type parameters

  • T

IsDayjsOptions

IsDayjsOptions: { is_valid: boolean }

Type declaration

  • is_valid: boolean

MapUniqueKeysProjection

MapUniqueKeysProjection<Key, Projection>: (item: Key) => Projection

Type parameters

  • Key

    The type of the map's keys.

  • Projection

    The type returned by projection.

Type declaration

    • (item: Key): Projection
    • Parameters

      • item: Key

      Returns Projection

MapUniqueProjection

MapUniqueProjection<Value, Projection>: (item: Value) => Projection

Type parameters

  • Value

  • Projection

Type declaration

    • (item: Value): Projection
    • Parameters

      • item: Value

      Returns Projection

SetUniqueProjection

SetUniqueProjection<Value, Projection>: (item: Value) => Projection

Type parameters

  • Value

  • Projection

Type declaration

    • (item: Value): Projection
    • Parameters

      • item: Value

      Returns Projection

Generated using TypeDoc