Table of contents

Public class

Signature
class Validator<T> implements Validatable<T> {}
References

Validatable

Source

packages/validate/src/lib/validation.ts


Constructor

Public constructor

Constructs a new instance of the Validator class

Parameters
NameDescription

validationFn

ValidatorFn<T>

validateUsingKnownGoodVersionFn

  | undefined
  | ValidatorUsingKnownGoodVersionFn<T, T>
References

ValidatorFn, ValidatorUsingKnownGoodVersionFn


Properties

validateUsingKnownGoodVersionFn

Public readonly property

Signature
readonly validateUsingKnownGoodVersionFn?:
  | undefined
  | ValidatorUsingKnownGoodVersionFn<T, T>
References

ValidatorUsingKnownGoodVersionFn


validationFn

Public readonly property

Signature
readonly validationFn: ValidatorFn<T>
References

ValidatorFn


Methods

check()

Public method

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

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

name

string

checkFn

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

Validator


check()

Public method

Signature
check(checkFn: (value: T) => void): Validator<T>
Parameters
NameDescription

checkFn

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

Validator


isValid()

Public method

Checks that the passed value is of the correct type.

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

value

unknown
Returns
value is T

nullable()

Public method

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

Signature
nullable(): Validator<null | T>
References

Validator


optional()

Public method

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

Signature
optional(): Validator<T | undefined>
References

Validator


refine()

Public method

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.

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

otherValidationFn

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

Validator


validate()

Public method

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.

Signature
validate(value: unknown): T
Parameters
NameDescription

value

unknown
Returns
T

validateUsingKnownGoodVersion()

Public method

Signature
validateUsingKnownGoodVersion(knownGoodValue: T, newValue: unknown): T
Parameters
NameDescription

knownGoodValue

T

newValue

unknown
Returns
T

UnionValidator