validation package

Zod-backed input validation for @imqueue services, expressed as native (TC39) decorators rather than as a schema kept alongside the class it describes.

Declare the rules on the input class itself — validate() on each field, validatable() on the class to seal them — and then either guard a service method's arguments with validated() or fetch the assembled schema with schemaOf() and parse by hand. It is used by @imqueue/rpc services and by the model code @imqueue/pg-prisma generates.

Remarks

Two things about the decorators are worth knowing before you rely on them.

validatable() is not optional bookkeeping. Field validators are buffered until a class decorator claims them, so a class that uses validate() without it hands its fields to the next class that is sealed — which then rejects valid input over properties it does not declare, while the class with the real mistake validates nothing. Seal every class that carries field validators.

validated() checks arguments without replacing them. The method body receives exactly what the caller passed, so transforming schemas — z.coerce.number(), .trim(), .default(...) — validate as expected and change nothing that reaches the method.

Failures throw Zod's own ZodError, unwrapped — but only in-process. Over RPC it does not arrive as an exception at all: @imqueue/rpc converts whatever a method throws into its own error payload, so the remote caller sees the code IMQ_RPC_CALL_ERROR (a ZodError carries no code of its own) with Zod's issue list as the message string, and instanceof ZodError never holds there. Zod is the single runtime dependency.

Example

import { z } from 'zod';
import { validatable, validate, validated } from '@imqueue/validation';

@validatable()
class Credentials {
    @validate(z.string().email())
    email!: string;

    @validate(z.string().min(8))
    password!: string;
}

class AuthService {
    @validated(Credentials)
    async signIn(creds: Credentials): Promise<string> {
        return `token-for-${creds.email}`;
    }
}

Functions

Function

Description

schemaOf(target)

The assembled Zod object schema for a validatable() class, or null when the class contributes no validated fields.

validatable()

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

validate(validator)

Field decorator that records a validator for one class field, sitting beside @property on an @imqueue/rpc input class.

validated(validators)

Method decorator that checks a method's positional arguments before the method body runs — the usual way to validate an @imqueue/rpc service method's input.

Type Aliases

Type Alias

Description

Ctor

Any class constructor, abstract ones included.

Validator

What validate() and validated() accept for a single value: a Zod schema, a validatable() class whose own field schemas should be used, or null/undefined to skip validation at that position.

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