mobx-sentinel API doc
    Preparing search index...

    Class Validator<T>

    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

    Type Parameters

    • T
    Index
    defaultDelayMs: number = 100
    id: string = ...
    • Get a validator instance for the target object.

      Type Parameters

      • T extends object

      Parameters

      • target: T

      Returns 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.

    • Add an async handler

      Type Parameters

      • Expr

      Parameters

      • expr: () => Expr

        The expression to observe

      • handler: AsyncHandler<T, NoInfer<Expr>>

        The async handler to call when the expression changes

      • Optionalopt: HandlerOptions<NoInfer<Expr>>

        The handler options

      Returns () => void

      A function to remove the handler

      • 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
    • Add a sync handler

      Parameters

      Returns () => void

      A function to remove the handler

      • Handler runs immediately when added for initial validation
      • Handler is called when observable expressions within it change
      • Changes are throttled by default delay
    • 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

      • searchKeyPath: KeyPath
      • prefixMatch: boolean = false

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

    • 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 void