Skip to content

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

web-template (TypeScript)

Quick TypeScript setup for this repository.

Commands:

  • Install deps:
npm install
  • Development (auto-restart):
npm run dev
  • Build:
npm run build
  • Run built app:
npm start

Request Validation with Zod

This project uses Zod for request validation, providing strong type safety and improved error handling.

How It Works

  1. Validation Schemas: Defined in src/models/user.schema.ts using Zod
  2. Validation Middleware: Located in src/utils/validate.ts
  3. Route Integration: Applied in src/routes/user.routes.ts

Example Usage

Creating a User (POST /api/users)

Valid request:

{
  "name": "John",
  "surname": "Doe",
  "email": "john.doe@example.com",
  "age": 25
}

Invalid request (missing required fields):

{
  "name": "John"
}

Error response:

{
  "message": "Validation failed",
  "errors": [
    { "field": "surname", "message": "Surname is required" },
    {
      "field": "email",
      "message": "Invalid input: expected string, received undefined"
    }
  ]
}

Adding New Validation Schemas

  1. Define your schema in the appropriate model file:
import { z } from 'zod';

export const mySchema = z.object({
  field: z.string().min(1),
  // ... other fields
});
  1. Apply the validation middleware to your routes:
import { validate } from '../utils/validate';
import { mySchema } from '../models/my.schema';

router.post('/endpoint', validate(mySchema, 'body'), controller);

Benefits

  • Type Safety: Automatic TypeScript type inference from schemas
  • Consistent Error Handling: Standardized validation error responses
  • Better DX: Clear error messages help developers understand validation issues
  • Runtime Safety: Ensures data structure at runtime matches TypeScript types

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages