Validator
Defined in: validator.ts:76, validator.ts:583
Validator for handling synchronous and asynchronous validations
- Supports both sync and async validation handlers
- Tracks validation state (isValidating)
- Looks up errors by key path pattern
- Supports nested validators
AsyncHandler
Section titled “AsyncHandler”AsyncHandler<
T,Expr> = (expr:Expr,builder:ValidationErrorMapBuilder<T>,abortSignal:AbortSignal) =>Promise<void>
Defined in: validator.ts:591
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:609
Handler options
Type Parameters
Section titled “Type Parameters”Expr = unknown
delayMs?
Section titled “delayMs?”
optionaldelayMs?:number
Defined in: validator.ts:621
Throttle reaction. [milliseconds]
Default
Section titled “Default”100equals?
Section titled “equals?”
optionalequals?:IEqualsComparer<Expr>
Defined in: validator.ts:627
The equality comparer for the expression.
Only effective for async handlers.
initialRun?
Section titled “initialRun?”
optionalinitialRun?:boolean
Defined in: validator.ts:615
Whether to run the handler immediately
Default
Section titled “Default”trueInstantHandler
Section titled “InstantHandler”InstantHandler<
T> = (builder:ValidationErrorMapBuilder<T>) =>void
Defined in: validator.ts:607
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:601
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:77
staticget<T>(target):Validator<T>
Defined in: validator.ts:98
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 by the identity of the target, so an object inheriting from a validated one gets its own
- Nothing is written to the target, so frozen, sealed and non-extensible objects are supported
- Instances are 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:109
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:79
asyncState
Section titled “asyncState”Get Signature
Section titled “Get Signature”get asyncState():
number
Defined in: validator.ts:283
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:188
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:139
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:161
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:171
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:149
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:133
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:295
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():
Generator<StandardNestedFetcher.Entry<Validator<any>>,void,unknown>
Defined in: validator.ts:359
Nested validators
Remarks
Section titled “Remarks”A fresh iterator over the @nested entries, in annotation order and then in collection order. Each entry
carries the name of the annotated member in key ("items"), the address of the nested object in keyPath
("items.0") and its validator in data.
Each read starts a new iteration, and an iterator is consumed once, so a second pass needs a second read.
There is no lookup by name: entries are not unique by key path. To reach the validator of one nested object, go
through the property — Validator.get(target.child) is cached per subject, so it is the very instance yielded
here — or iterate and match entry.key, which is KeyPath.Self for a @nested.hoist member
rather than the name it is declared with. Errors are looked up by pattern with findErrors, which
searches nested validators itself.
Returns
Section titled “Returns”Generator<StandardNestedFetcher.Entry<Validator<any>>, void, unknown>
reactionState
Section titled “reactionState”Get Signature
Section titled “Get Signature”get reactionState():
number
Defined in: validator.ts:275
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:455
@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:416
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(...
patterns):Generator<[keyPath:KeyPath,error:ValidationError],void,any>
Defined in: validator.ts:221
Find the errors of the key paths matching any of the patterns
- Yields each error once, with its key path relative to this validator, however many patterns match it
- Searches nested validators, including hoisted ones, for the rest of the patterns
- Reads only the
@nestedmembers the patterns can reach, which is all of them when one starts with a wildcard
Parameters
Section titled “Parameters”patterns
Section titled “patterns”...KeyPathPattern[]
Returns
Section titled “Returns”Generator<[keyPath: KeyPath, error: ValidationError], void, any>
KeyPathPattern for the syntax
getErrorMessages()
Section titled “getErrorMessages()”getErrorMessages(...
patterns):Set<string>
Defined in: validator.ts:196
Get the error messages of the key paths matching any of the patterns
Parameters
Section titled “Parameters”patterns
Section titled “patterns”...KeyPathPattern[]
Returns
Section titled “Returns”Set<string>
hasErrors()
Section titled “hasErrors()”hasErrors(...
patterns):boolean
Defined in: validator.ts:205
Check if any key path matching any of the patterns has errors
Parameters
Section titled “Parameters”patterns
Section titled “patterns”...KeyPathPattern[]
Returns
Section titled “Returns”boolean
reset()
Section titled “reset()”reset():
void
Defined in: validator.ts:371
@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:390
@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:326
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