Documentation
API
Input (tsdl.input)

Input

Defining inputs for queries

Inputs are a way to request specific payload from the client. The input callback will validate and transform the incoming payload. The parsed payload is then passed to middleware and lastly the query as input.

Zod (opens in a new tab) example


client.ts
const volvos = await tsdl.cars({
  brand: "volvo", // ✅
});
 
const apples = await tsdl.cars({
  type: "apple", // Error! Property "type" does not exist on type `{ brand: string }`
});
server.ts
import { z } from "zod";
 
const router = tsdl.router({
  cars: tsdl
    .input(z.object({
      brand: z.string().min(2).max(100).trim(),
    }))
    .query(async ({ input }) => {
      return await db.findOne({
        brand: input.brand // string
      });
    });
});

Yup (opens in a new tab) example


server.ts
import * as yup from "yup";
 
const router = tsdl.router({
  cars: tsdl
    .input(yup.object({
      brand: yup.string().required(),
    }))
    .query(async ({ input }) => {
      return await db.findOne({
        brand: input.brand // string
      });
    });
});

Other/custom schema validator


server.ts
const router = tsdl.router({
  cars: tsdl
    .input({
      validate(raw: unknown) {
        if (typeof raw === "object" && "brand" in raw && typeof raw.brand === "string") {
          return raw as {
            brand: string;
          };
        }
        throw "invalid input";
      }
    })
    .query(async ({ input }) => {
      return await db.findOne({
        brand: input.brand
      });
    });
});