Creating New Resources with DTOs and Validation

Introduction: Creating New Resources in an API

Welcome to your first lesson in building the Reading Tracker API with NestJS! In this lesson, you will learn how to let users add new resources — specifically, new books — to your API. In the world of web APIs, a "resource" is just a piece of data, like a book, a user, or a review. When you want to add a new resource, you usually use a POST request. As a reminder, POST request is part of the HTTP protocol and is typically used when the client wants to send data to the server to create a new resource. In our case, the resource is a book.

For example, if you want to let someone add a new book to your reading tracker, you would create a POST endpoint. This endpoint will accept information about the book (like its title and author) and store it in your system. This is a common pattern in almost every web application.

By the end of this lesson, you will know how to:

  • Define a DTO (Data Transfer Object) to validate input.
  • Create a POST endpoint in a controller.
  • Use the UUID library to generate unique IDs for new resources.

What Are UUIDs and Why Do We Need Them?

Every resource in your system (book, user, session) needs a unique identifier. Instead of using simple numbers like 1, 2, 3, we use UUIDs (Universally Unique Identifiers).

A UUID is a long, random-looking string such as: 550e8400-e29b-41d4-a716-446655440000

They are designed to be globally unique, so you can safely generate IDs without worrying about collisions. UUIDs are commonly used in APIs, databases, and distributed systems.

In Node.js, we use the uuid library. Normally, you install it with:

npm install uuid

But in the CodeSignal environment, everything is already pre-installed — so you’re ready to use it right away.

// src/database/mock-db.ts
import { v4 as uuidv4 } from 'uuid';

export const users = [
  { id: uuidv4(), name: 'Alice' },
  { id: uuidv4(), name: 'Bob' },
];

export const books = [
  { id: uuidv4(), title: 'The Hobbit', author: 'J.R.R. Tolkien' },
  { id: uuidv4(), title: 'Dune', author: 'Frank Herbert' },
];

Every time we create a new user or book, uuidv4() generates a fresh, unique id.

What Is a DTO and Why It Matters

When someone sends data to your API (for example, to add a new book), you want to make sure the data is correct. This is where a Data Transfer Object (DTO) comes in. A DTO is a simple class that describes what data you expect and helps validate it.

DTO stands for Data Transfer Object. It acts as a contract between the client (like a frontend or mobile app) and your API. When someone wants to send data to your server (like creating a book), they must follow the structure you define in the DTO.

DTOs ensure:

  • Your code knows exactly what shape of data to expect.
  • Invalid or missing data is caught early before it breaks your business logic.
  • The frontend and backend can evolve independently while maintaining agreed structure.

To use these validation decorators, you need to install the validation libraries:

npm install class-validator class-transformer

These libraries integrate with NestJS and automatically check incoming data before it reaches your service. If a request doesn’t pass validation (e.g., title is missing), NestJS returns a clear 400 Bad Request response — no manual checks required.

Here’s an example DTO for creating a new book:

// src/books/dto/create-book.dto.ts
import { IsString, IsNotEmpty } from 'class-validator';

export class CreateBookDto {
  @IsString()
  @IsNotEmpty()
  title!: string;

  @IsString()
  @IsNotEmpty()
  author!: string;
}

Explanation:

  • @IsString() and @IsNotEmpty() are decorators from the class-validator library. They make sure that both title and author are non-empty strings.
  • If someone tries to add a book without a title or author, or with numbers instead of text, the API will reject the request.

This helps keep your data clean and your application safe from bad input.

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal