Skip to content

Outgoing responses can be validated too. Use the response property to declare the expected status code, content type, and body schema:

api/users/index.ts
ts
import type { User } from "~/types/api-payload";
import { defineRoute } from "_/api";

export default defineRoute<"users">(({ GET }) => [
  GET<{
    response: [200, "json", User], 
  }>(async (ctx) => {
    // response must comply to defined schema
  }),
]);

Before sending, KosmoJS checks that the actual status, content type, and body match the schema. If anything is off - a missing field, a type mismatch, a constraint violation - it throws a ValidationError instead of sending malformed data to the client.

Response validation is especially valuable for data sourced from databases or third-party APIs, where the shape can change without warning.

Defining a response schema also enables automatic OpenAPI generation - type safety and documentation in one step. (Details ➜ )

Released under the MIT License.