validatable() function

Class decorator that seals the validate() fields declared in this class, making them retrievable through schemaOf().

Signature:

export declare function validatable(): (target: Ctor, context: ClassDecoratorContext) => void;

Returns:

(target: Ctor, context: ClassDecoratorContext) => void

The TC39 class decorator that seals the buffered field validators.

Exceptions

Error if applied to anything other than a class.

Remarks

Required on every class whose fields carry validate(), for two independent reasons: without it schemaOf() returns null, and the unclaimed fields go on to contaminate the next class that is sealed.

It seals the fields declared in this class body and nothing else. Fields inherited from a sealed parent are not included, so schemaOf(Child) describes the child's own fields alone and an inherited field is validated only if the subclass re-declares it. schemaOf() does not walk the prototype chain either: an undecorated subclass of a sealed parent resolves to null rather than to the parent's schema.

Example

@validatable()
class Address {
    @validate(z.string().min(1))
    city!: string;
}

// A sealed class is itself usable as a validator for a nested object.
@validatable()
class Customer {
    @validate(z.string().min(1))
    name!: string;

    @validate(Address)
    address!: Address;
}

Read this page as plain markdown — no HTML, no navigation. For pasting into an LLM, or for an agent to fetch.