Skip to content

Validator

Defined in: validator.ts:86, validator.ts:588

Validator for handling synchronous and asynchronous validations

  • Supports both sync and async validation handlers
  • Tracks validation state (isValidating)
  • Provides error access by key path
  • Supports nested validators

AsyncHandler<T, Expr> = (expr: Expr, builder: ValidationErrorMapBuilder<T>, abortSignal: AbortSignal) => Promise<void>

Defined in: validator.ts:596

Async handler

Expr

The expression observed

ValidationErrorMapBuilder<T>

The builder to build the errors

AbortSignal

The abort signal

Promise<void>


HandlerOptions<Expr> = object

Defined in: validator.ts:614

Handler options

Expr = unknown

optional delayMs?: number

Defined in: validator.ts:626

Throttle reaction. [milliseconds]

100

optional equals?: IEqualsComparer<Expr>

Defined in: validator.ts:632

The equality comparer for the expression.

Only effective for async handlers.

optional initialRun?: boolean

Defined in: validator.ts:620

Whether to run the handler immediately

true

InstantHandler<T> = (builder: ValidationErrorMapBuilder<T>) => void

Defined in: validator.ts:612

Instant handler

ValidationErrorMapBuilder<T>

The builder to build the errors

void


SyncHandler<T> = (builder: ValidationErrorMapBuilder<T>) => void

Defined in: validator.ts:606

Sync handler

ValidationErrorMapBuilder<T>

The builder to build the errors

void

static defaultDelayMs: number = 100

Defined in: validator.ts:87


static get<T>(target): Validator<T>

Defined in: validator.ts:108

Get a validator instance for the target object.

T extends object

T

Validator<T>

  • Returns existing instance if one exists for the target
  • Creates new instance if none exists
  • Instances are cached, and garbage collected with the target only if everything their handlers observe is too

TypeError if the target is not an object.


static getSafe<T>(target): Validator<T> | null

Defined in: validator.ts:119

Get a validator instance for the target object.

Same as Validator.get but returns null instead of throwing an error.

T

Validator<T> | null

readonly id: string

Defined in: validator.ts:89


get asyncState(): number

Defined in: validator.ts:309

The number of pending/running async jobs.

number

@computed


get firstErrorMessage(): string | null

Defined in: validator.ts:198

Get the first error message (including nested objects)

string | null

@computed


get invalidKeyCount(): number

Defined in: validator.ts:149

The number of invalid keys

number

@computed


get invalidKeyPathCount(): number

Defined in: validator.ts:171

The number of invalid key paths

number

@computed


get invalidKeyPaths(): ReadonlySet<KeyPath>

Defined in: validator.ts:181

The key paths that have errors

Keys of nested objects are included.

ReadonlySet<KeyPath>

@computed


get invalidKeys(): ReadonlySet<KeyPath>

Defined in: validator.ts:159

The keys that have errors

Keys of nested objects are NOT included.

ReadonlySet<KeyPath>

@computed


get isValid(): boolean

Defined in: validator.ts:143

Whether no errors are found

boolean

@computed


get isValidating(): boolean

Defined in: validator.ts:321

Whether the validator is computing errors (including nested validators)

boolean

@computed


get nested(): ReadonlyMap<KeyPath, Validator<any>>

Defined in: validator.ts:372

Nested validators

ReadonlyMap<KeyPath, Validator<any>>


get reactionState(): number

Defined in: validator.ts:301

The number of pending/running reactions.

number

@computed


addAsyncHandler<Expr>(expr, handler, opt?): () => void

Defined in: validator.ts:468

@action

Add an async handler

() => Expr

The expression to observe

Validator.AsyncHandler<T, NoInfer<Expr>>

The async handler to call when the expression changes

Validator.HandlerOptions<NoInfer<Expr>>

The handler options

A function to remove the handler

() => void

  • Handler runs immediately when added for initial validation
  • Handler is called when the watched expression changes
  • Changes are throttled by default delay
  • Changes made while the handler is running are queued, not aborted: the latest value is validated after the running handler settles
  • Provides abort signal, which is aborted when the validator is reset or the handler is removed; the result of an aborted run is discarded, so the errors it collected are not applied

addSyncHandler(handler, opt?): () => void

Defined in: validator.ts:429

Add a sync handler

Validator.SyncHandler<T>

The sync handler containing observable expressions

Validator.HandlerOptions<unknown>

A function to remove the handler

() => void

  • Handler runs immediately when added for initial validation
  • Handler is called when observable expressions within it change
  • Changes are throttled by default delay

findErrors(searchKeyPath, prefixMatch?): Generator<[keyPath: KeyPath, error: ValidationError], void, any>

Defined in: validator.ts:229

Find errors for the key path

  • Can do exact or prefix matching
  • Returns all errors that match the key path
  • Includes errors from nested validators when using prefix match

KeyPath

boolean = false

Generator<[keyPath: KeyPath, error: ValidationError], void, any>


getErrorMessages(keyPath, prefixMatch?): Set<string>

Defined in: validator.ts:206

Get error messages for the key path

KeyPath

boolean = false

Set<string>


hasErrors(keyPath, prefixMatch?): boolean

Defined in: validator.ts:215

Check if the validator has errors for the key path

KeyPath

boolean = false

boolean


reset(): void

Defined in: validator.ts:384

@action

Reset the validator

Use with caution.
Since validation is reactive, errors won't reappear until you make some changes.
Running async validations are aborted, and their results are discarded.

void


updateErrors(key, handler): () => void

Defined in: validator.ts:403

@action

Update the errors immediately

symbol

Validator.InstantHandler<T>

A function to remove the errors

() => void


waitForValidation(opt?): Promise<void>

Defined in: validator.ts:352

Wait for the validation to complete

A shorthand for:

await when(() => !validator.isValidating);

AbortSignal

Abort signal to stop waiting

Promise<void>

A promise that resolves once isValidating is false, or rejects with the reason of the signal if it is aborted first

  • Resolves right away if nothing is being validated
  • Waits for nested validators as well, as isValidating includes them
  • Deadlocks when awaited in an async handler of this validator or of a nested one: the handler is part of the validation it waits for