Table of contents

Properties

any

Validation that accepts any value. Generally this should be avoided, but you can use it as an escape hatch if you want to work without validations for e.g. a prototype.

any: Validator<any>

array

Validates that a value is an array. To check the contents of the array, use T.arrayOf.

array: Validator<unknown[]>

bigint

Validates that a value is a bigint.

bigint: Validator<bigint>

boolean

Validates that a value is boolean.

boolean: Validator<boolean>

httpUrl

Validates an http(s) url

httpUrl: Validator<string>

indexKey

Validates that a value is an IndexKey.

indexKey: Validator<IndexKey>

integer

Fails if number is not an integer

integer: Validator<number>

jsonValue

Validate that a value is valid JSON.

jsonValue: Validator<JsonValue>

linkUrl

Validates that a value is a url safe to use as a link.

linkUrl: Validator<string>

nonZeroInteger

Fails if value <= 0 and is not an integer

nonZeroInteger: Validator<number>

nonZeroNumber

Fails if value <= 0

nonZeroNumber: Validator<number>

number

Validates that a value is a finite non-NaN number.

number: Validator<number>

positiveInteger

Fails if value < 0 and is not an integer

positiveInteger: Validator<number>

positiveNumber

Fails if value < 0

positiveNumber: Validator<number>

srcUrl

Validates that a valid is a url safe to load as an asset.

srcUrl: Validator<string>

string

Validates that a value is a string.

string: Validator<string>

unknown

Validation that accepts any value. Useful as a starting point for building your own custom validations.

unknown: Validator<unknown>

unknownObject

unknownObject: Validator<Record<string, unknown>>

Methods

arrayOf()

Validates that a value is an array whose contents matches the passed-in validator.

function arrayOf<T>(itemValidator: Validatable<T>): ArrayOfValidator<T>
Parameters
NameDescription

itemValidator

Validatable<T>
Returns
ArrayOfValidator<T>

dict()

Validation that an option is a dict with particular keys and values.

function dict<Key extends string, Value>(
  keyValidator: Validatable<Key>,
  valueValidator: Validatable<Value>
): DictValidator<Key, Value>
Parameters
NameDescription

keyValidator

Validatable<Key>

valueValidator

Validatable<Value>
Returns
DictValidator<Key, Value>

jsonDict()

Validate an object has a particular shape.

function jsonDict(): DictValidator<string, JsonValue>

literal()

Validates that a value matches another that was passed in.

function literal<T extends boolean | number | string>(
  expectedValue: T
): Validator<T>
Example
const trueValidator = T.literal(true)
Parameters
NameDescription

expectedValue

T
Returns
Validator<T>

literalEnum()

function literalEnum<const Values extends readonly unknown[]>(
  ...values: Values
): Validator<Values[number]>
Parameters
NameDescription

values

Values
Returns
Validator<Values[number]>

model()

A named object with an ID. Errors will be reported as being part of the object with the given name.

function model<
  T extends {
    readonly id: string
  },
>(name: string, validator: Validatable<T>): Validator<T>
Parameters
NameDescription

name

string

validator

Validatable<T>
Returns
Validator<T>

nullable()

function nullable<T>(validator: Validatable<T>): Validator<null | T>
Parameters
NameDescription

validator

Validatable<T>
Returns
Validator<null | T>

object()

Validate an object has a particular shape.

function object<Shape extends object>(config: {
  readonly [K in keyof Shape]: Validatable<Shape[K]>
}): ObjectValidator<
  Expand<
    {
      [P in ExtractRequiredKeys<Shape>]: Shape[P]
    } & {
      [P in ExtractOptionalKeys<Shape>]?: Shape[P]
    }
  >
>
Parameters
NameDescription

config

{
  readonly [K in keyof Shape]: Validatable<Shape[K]>
}
Returns
ObjectValidator<
  Expand<
    {
      [P in ExtractRequiredKeys<Shape>]: Shape[P]
    } & {
      [P in ExtractOptionalKeys<Shape>]?: Shape[P]
    }
  >
>

optional()

function optional<T>(validator: Validatable<T>): Validator<T | undefined>
Parameters
NameDescription

validator

Validatable<T>
Returns
Validator<T | undefined>

setEnum()

function setEnum<T>(values: ReadonlySet<T>): Validator<T>
Parameters
NameDescription

values

ReadonlySet<T>
Returns
Validator<T>

union()

Validate a union of several object types. Each object must have a property matching key which should be a unique string.

function union<
  Key extends string,
  Config extends UnionValidatorConfig<Key, Config>,
>(key: Key, config: Config): UnionValidator<Key, Config>
Example
const catValidator = T.object({ kind: T.literal('cat'), meow: T.boolean })
const dogValidator = T.object({ kind: T.literal('dog'), bark: T.boolean })
const animalValidator = T.union('kind', {
  cat: catValidator,
  dog: dogValidator,
})
Parameters
NameDescription

key

Key

config

Config
Returns
UnionValidator<Key, Config>