Table of contents

Extends Validator<TypeOf<Config[keyof Config]> | UnknownValue>.

class UnionValidator<
  Key extends string,
  Config extends UnionValidatorConfig<Key, Config>,
  UnknownValue = never,
> extends Validator<TypeOf<Config[keyof Config]> | UnknownValue> {}

Constructor

Constructs a new instance of the UnionValidator class

Parameters
NameDescription

key

Key

config

Config

unknownValueValidation

(
  value: object,
  variant: string
) => UnknownValue

useNumberKeys

boolean

Properties

validateUsingKnownGoodVersionFn

from Validator
readonly validateUsingKnownGoodVersionFn?:
  | undefined
  | ValidatorUsingKnownGoodVersionFn<T, T>

validationFn

from Validator
readonly validationFn: ValidatorFn<T>

Methods

check()

from Validator

Refine this validation with an additional check that doesn't change the resulting value.

check(name: string, checkFn: (value: T) => void): Validator<T>
Example
const numberLessThan10Validator = T.number.check((value) => {
  if (value >= 10) {
    throw new ValidationError(`Expected number less than 10, got ${value}`)
  }
})
Parameters
NameDescription

name

string

checkFn

(value: T) => void
Returns
Validator<T>

isValid()

from Validator

Checks that the passed value is of the correct type.

isValid(value: unknown): value is T
Parameters
NameDescription

value

unknown
Returns
value is T

nullable()

from Validator

Returns a new validator that also accepts null or undefined. The resulting value will always be null.

nullable(): Validator<null | T>

optional()

from Validator

Returns a new validator that also accepts null or undefined. The resulting value will always be null.

optional(): Validator<T | undefined>

refine()

from Validator

Refine this validation to a new type. The passed-in validation function should throw an error if the value can't be converted to the new type, or return the new type otherwise.

refine<U>(otherValidationFn: (value: T) => U): Validator<U>
Parameters
NameDescription

otherValidationFn

(value: T) => U
Returns
Validator<U>

validate()

from Validator

Asserts that the passed value is of the correct type and returns it. The returned value is guaranteed to be referentially equal to the passed value.

validate(value: unknown): T
Parameters
NameDescription

value

unknown
Returns
T

validateUnknownVariants()

validateUnknownVariants<Unknown>(
  unknownValueValidation: (value: object, variant: string) => Unknown
): UnionValidator<Key, Config, Unknown>
Parameters
NameDescription

unknownValueValidation

(value: object, variant: string) => Unknown
Returns
UnionValidator<Key, Config, Unknown>

validateUsingKnownGoodVersion()

from Validator
validateUsingKnownGoodVersion(knownGoodValue: T, newValue: unknown): T
Parameters
NameDescription

knownGoodValue

T

newValue

unknown
Returns
T

ObjectValidatorValidator