@mocanvas/editor · class

Validator

declare class Validator<T> implements Validatable<T>

A checked value: validate narrows or throws, everything else is built on top of it.

Validators are immutable — check, refine, optional and nullable all return a new validator and leave the receiver alone, so one shared T.string can be refined per shape without surprising anyone else.


Methods · 8

check(check: (value: T) => void): Validator<T>

Add a further constraint that does not change the type. The callback throws (or returns nothing) — anything it throws is reported under the same path as the value itself.

``ts const even = T.integer.check("even", (n) => { if (n % 2) throw new Error("not even") }) ``

check(name: string, check: (value: T) => void): Validator<T>
constructor(
isValid(value: unknown): boolean
nullable(): Validator<T | null>

T | null.

optional(): Validator<T | undefined>

T | undefined. A missing object property is legal exactly when its validator is optional.

refine<U>(refine: (value: T) => U): Validator<U>

Validate, then map to another type (parsing, normalizing, branding, ...).

validate(value: unknown): T

Properties · 4

readonly validateUsingKnownGoodVersion?: ((knownGood: T, value: unknown) => T) | undefined

Optional fast path used when a previously validated value is known: an expensive validator can skip the parts that cannot have changed.

validateUsingKnownGoodVersion?: ((knownGood: T, value: unknown) => T) | undefined)

Optional fast path used when a previously validated value is known: an expensive validator can skip the parts that cannot have changed.

readonly validateValue: (value: unknown) => T

Narrow value to T, or throw a ValidationError.

validateValue: (value: unknown) => T,

Narrow value to T, or throw a ValidationError.

mocanvas is built by Symbio. Free for non-commercial, non-profit and development use; commercial use needs an agreement. Not affiliated with or endorsed by tldraw.