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
Section titled “AsyncHandler”AsyncHandler<
T,Expr> = (expr:Expr,builder:ValidationErrorMapBuilder<T>,abortSignal:AbortSignal) =>Promise<void>
Defined in: validator.ts:596
Async handler
Parameters
Section titled “Parameters”Expr
The expression observed
builder
Section titled “builder”The builder to build the errors
abortSignal
Section titled “abortSignal”AbortSignal
The abort signal
Returns
Section titled “Returns”Promise<void>
HandlerOptions
Section titled “HandlerOptions”HandlerOptions<
Expr> =object
Defined in: validator.ts:614
Handler options
Type Parameters
Section titled “Type Parameters”Expr = unknown
delayMs?
Section titled “delayMs?”
optionaldelayMs?:number
Defined in: validator.ts:626
Throttle reaction. [milliseconds]
Default
Section titled “Default”100equals?
Section titled “equals?”
optionalequals?:IEqualsComparer<Expr>
Defined in: validator.ts:632
The equality comparer for the expression.
Only effective for async handlers.
initialRun?
Section titled “initialRun?”
optionalinitialRun?:boolean
Defined in: validator.ts:620
Whether to run the handler immediately
Default
Section titled “Default”trueInstantHandler
Section titled “InstantHandler”InstantHandler<
T> = (builder:ValidationErrorMapBuilder<T>) =>void
Defined in: validator.ts:612
Instant handler
Parameters
Section titled “Parameters”builder
Section titled “builder”The builder to build the errors
Returns
Section titled “Returns”void
SyncHandler
Section titled “SyncHandler”SyncHandler<
T> = (builder:ValidationErrorMapBuilder<T>) =>void
Defined in: validator.ts:606
Sync handler
Parameters
Section titled “Parameters”builder
Section titled “builder”The builder to build the errors
Returns
Section titled “Returns”void
Static Members
Section titled “Static Members”defaultDelayMs
Section titled “defaultDelayMs”
staticdefaultDelayMs:number=100
Defined in: validator.ts:87
staticget<T>(target):Validator<T>
Defined in: validator.ts:108
Get a validator instance for the target object.
Type Parameters
Section titled “Type Parameters”T extends object
Parameters
Section titled “Parameters”target
Section titled “target”T
Returns
Section titled “Returns”Validator<T>
Remarks
Section titled “Remarks”- 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
Throws
Section titled “Throws”TypeError if the target is not an object.
getSafe()
Section titled “getSafe()”
staticgetSafe<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.
Parameters
Section titled “Parameters”target
Section titled “target”T
Returns
Section titled “Returns”Validator<T> | null
Members
Section titled “Members”
readonlyid:string
Defined in: validator.ts:89
asyncState
Section titled “asyncState”Get Signature
Section titled “Get Signature”get asyncState():
number
Defined in: validator.ts:309
The number of pending/running async jobs.
Returns
Section titled “Returns”number
@computed
firstErrorMessage
Section titled “firstErrorMessage”Get Signature
Section titled “Get Signature”get firstErrorMessage():
string|null
Defined in: validator.ts:198
Get the first error message (including nested objects)
Returns
Section titled “Returns”string | null
@computed
invalidKeyCount
Section titled “invalidKeyCount”Get Signature
Section titled “Get Signature”get invalidKeyCount():
number
Defined in: validator.ts:149
The number of invalid keys
Returns
Section titled “Returns”number
@computed
invalidKeyPathCount
Section titled “invalidKeyPathCount”Get Signature
Section titled “Get Signature”get invalidKeyPathCount():
number
Defined in: validator.ts:171
The number of invalid key paths
Returns
Section titled “Returns”number
@computed
invalidKeyPaths
Section titled “invalidKeyPaths”Get Signature
Section titled “Get Signature”get invalidKeyPaths():
ReadonlySet<KeyPath>
Defined in: validator.ts:181
The key paths that have errors
Keys of nested objects are included.
Returns
Section titled “Returns”ReadonlySet<KeyPath>
@computed
invalidKeys
Section titled “invalidKeys”Get Signature
Section titled “Get Signature”get invalidKeys():
ReadonlySet<KeyPath>
Defined in: validator.ts:159
The keys that have errors
Keys of nested objects are NOT included.
Returns
Section titled “Returns”ReadonlySet<KeyPath>
@computed
isValid
Section titled “isValid”Get Signature
Section titled “Get Signature”get isValid():
boolean
Defined in: validator.ts:143
Whether no errors are found
Returns
Section titled “Returns”boolean
@computed
isValidating
Section titled “isValidating”Get Signature
Section titled “Get Signature”get isValidating():
boolean
Defined in: validator.ts:321
Whether the validator is computing errors (including nested validators)
Returns
Section titled “Returns”boolean
@computed
nested
Section titled “nested”Get Signature
Section titled “Get Signature”get nested():
ReadonlyMap<KeyPath,Validator<any>>
Defined in: validator.ts:372
Nested validators
Returns
Section titled “Returns”ReadonlyMap<KeyPath, Validator<any>>
reactionState
Section titled “reactionState”Get Signature
Section titled “Get Signature”get reactionState():
number
Defined in: validator.ts:301
The number of pending/running reactions.
Returns
Section titled “Returns”number
@computed
addAsyncHandler()
Section titled “addAsyncHandler()”addAsyncHandler<
Expr>(expr,handler,opt?): () =>void
Defined in: validator.ts:468
@action
Add an async handler
Parameters
Section titled “Parameters”() => Expr
The expression to observe
handler
Section titled “handler”Validator.AsyncHandler<T, NoInfer<Expr>>
The async handler to call when the expression changes
Validator.HandlerOptions<NoInfer<Expr>>
The handler options
Returns
Section titled “Returns”A function to remove the handler
() => void
Remarks
Section titled “Remarks”- 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()
Section titled “addSyncHandler()”addSyncHandler(
handler,opt?): () =>void
Defined in: validator.ts:429
Add a sync handler
Parameters
Section titled “Parameters”handler
Section titled “handler”The sync handler containing observable expressions
Validator.HandlerOptions<unknown>
Returns
Section titled “Returns”A function to remove the handler
() => void
Remarks
Section titled “Remarks”- Handler runs immediately when added for initial validation
- Handler is called when observable expressions within it change
- Changes are throttled by default delay
findErrors()
Section titled “findErrors()”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
Parameters
Section titled “Parameters”searchKeyPath
Section titled “searchKeyPath”prefixMatch?
Section titled “prefixMatch?”boolean = false
Returns
Section titled “Returns”Generator<[keyPath: KeyPath, error: ValidationError], void, any>
getErrorMessages()
Section titled “getErrorMessages()”getErrorMessages(
keyPath,prefixMatch?):Set<string>
Defined in: validator.ts:206
Get error messages for the key path
Parameters
Section titled “Parameters”keyPath
Section titled “keyPath”prefixMatch?
Section titled “prefixMatch?”boolean = false
Returns
Section titled “Returns”Set<string>
hasErrors()
Section titled “hasErrors()”hasErrors(
keyPath,prefixMatch?):boolean
Defined in: validator.ts:215
Check if the validator has errors for the key path
Parameters
Section titled “Parameters”keyPath
Section titled “keyPath”prefixMatch?
Section titled “prefixMatch?”boolean = false
Returns
Section titled “Returns”boolean
reset()
Section titled “reset()”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.
Returns
Section titled “Returns”void
updateErrors()
Section titled “updateErrors()”updateErrors(
key,handler): () =>void
Defined in: validator.ts:403
@action
Update the errors immediately
Parameters
Section titled “Parameters”symbol
handler
Section titled “handler”Returns
Section titled “Returns”A function to remove the errors
() => void
waitForValidation()
Section titled “waitForValidation()”waitForValidation(
opt?):Promise<void>
Defined in: validator.ts:352
Wait for the validation to complete
A shorthand for:
await when(() => !validator.isValidating);Parameters
Section titled “Parameters”signal?
Section titled “signal?”AbortSignal
Abort signal to stop waiting
Returns
Section titled “Returns”Promise<void>
A promise that resolves once isValidating is false,
or rejects with the reason of the signal if it is aborted first
Remarks
Section titled “Remarks”- 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