Welcome back 👋 In the previous lesson, you established the project’s shared API contract (ApiSuccess, ApiError, ApiResponse) and the response helpers (success, error, parseJson) that make every route return consistent JSON envelopes. That foundation matters here because validation failures should also come back in the exact same structured error shape—just with a VALIDATION_ERROR code.
In this lesson, you’ll make the backend’s data shapes more explicit by introducing domain types and a small DTO for product listing inputs. Then you’ll build a tiny validation toolkit for query params and wire it into GET /api/products, so invalid inputs are rejected early with clean, consistent error responses.
Domain Types as the Backend’s Source of Truth
A backend needs a canonical “truth” for what its entities look like internally—especially once the database is involved. In this project, those canonical shapes live in src/lib/types/domain.ts. These types are designed to match the columns we’ll read from PostgreSQL later, so we don’t end up inventing new shapes at every layer.
This file defines a Currency type (kept intentionally narrow) and a Product interface that includes pricing, inventory, and status fields you’ll see in real queries and API responses.
TypeScript
export type Currency = 'USD';
Currency is a narrow type instead of a plain string, which makes the system more explicit and prevents “random” currency codes from sneaking in.
For this course, we only support 'USD', which keeps the domain model simple while still demonstrating the pattern of constrained values.
Even if we expand currency support later, starting narrow is helpful because it forces consistency across the codebase.
This interface is intentionally “database-shaped.” Fields like price_cents, inventory_count, and timestamps are exactly the kinds of columns we’ll read from PostgreSQL rows.
price_cents is an integer rather than a floating dollar amount. This avoids rounding problems that can happen when using floating point math for money.
description is string | null, which mirrors how SQL databases commonly represent “no value” for optional text fields.
status is a union of 'active' | 'archived'. This is important because status values show up in real filtering and business logic, and the union prevents invalid states like 'deleted' or 'inactive' from compiling.
Keeping these fields in the domain model makes it much easier to map DB rows to strongly-typed objects later, and it keeps your API responses aligned with the real data source.
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal
DTOs for Inputs We Accept From Clients
Domain types describe what the backend knows internally. DTOs (Data Transfer Objects) describe what the client is allowed to send—or, in this case, what query params the client is allowed to provide.
In this unit, we’re focusing on GET /api/products, so our “input” is the query string. That input shape lives in src/lib/types/dto.ts as ListProductsInput.
This interface represents the validated shape of the query params after parsing. Notice the types are string and number, not “raw strings” like you get from the URL.
All fields are optional because query params are optional. A request like /api/products should be valid even when no params are present.
page and pageSize are numbers here because pagination logic becomes much safer once you’ve converted and validated them early.
Even though the route in Unit 2 still returns an empty list, establishing this DTO shape now gives you a clean target: parse raw params → validate → produce a typed input.
Validation Utilities for Query Params
Query parameters always start as strings (or missing entirely). If you let those raw values flow into your route logic, you’ll quickly end up with messy checks scattered everywhere. Instead, this project centralizes query parsing and validation in src/lib/http/validation.ts.
In this unit, the focus is on three responsibilities:
Type guards (isString, isInt) that check types and constraints.
Parsers (parseOptionalStringParam, parseOptionalIntParam) that read from URLSearchParams, trim/convert, and return a consistent result type.
First, the shared result shape used by the parsers:
export type ValidationResult<T> = { ok: true; value: T } | { ok: false; message: string };
This is a simple “result object” pattern: either { ok: true, value } or { ok: false, message }.
It avoids throwing errors for validation failures, which keeps route handlers clean and predictable.
The route can decide how to convert a validation failure into an API error envelope (and what HTTP status to use).
Now the isString type guard:
export function isString( v: unknown, { min = 0, max }: { min?: number; max?: number } = {}): v is string { return typeof v === 'string' && v.length >= min && (max === undefined || v.length <= max);}
isString checks both type and length constraints. It’s not enough to know something is a string—you often also need to know it isn’t empty, or that it isn’t absurdly long.
The min default of 0 makes it flexible: you can validate “any string” or require at least one character depending on the context.
The optional max prevents cases like a 10,000-character query string from reaching your application logic.
Because it’s a TypeScript type guard (v is string), a successful check can narrow types in code that uses it.
Now the isInt type guard:
export function isInt( v: unknown, { min, max }: { min?: number; max?: number } = {}): v is number { return Number.isInteger(v) && (min === undefined || (v as number) >= min) && (max === undefined || (v as number) <= max);}
isInt enforces “integer-ness,” which is crucial for pagination values like page and pageSize.
The optional min and max bounds prevent invalid pagination like page=0 or pageSize=100000.
This guard assumes it is checking a numeric value, so it pairs naturally with a parser that converts the string into a number first.
Keeping this as a reusable guard means every route can enforce the same pagination rules in the same way.
Parsing Optional String Params
Parsing Optional Integer Params
Validating GET /api/products with Shared Parsers
Now we apply the utilities in a real route. The products route lives at src/app/api/products/route.ts, and it already imports parseOptionalStringParam and parseOptionalIntParam. Your job is to use them consistently so the route validates inputs and returns standard error envelopes on invalid values.
Here’s the full GET handler as it exists in the project.
import { NextRequest } from 'next/server';import { error, success } from '@/lib/http/response';import { parseOptionalIntParam, parseOptionalStringParam } from '@/lib/http/validation';export async function GET(req: NextRequest) { try { const sp = req.nextUrl.searchParams; const query = parseOptionalStringParam(sp, 'query', { min: 1, max: 64 }); if (!query.ok) return error('VALIDATION_ERROR', query.message, undefined, 400); const page = parseOptionalIntParam(sp, 'page', { min: 1 }); if (!page.ok) return error('VALIDATION_ERROR', page.message, undefined, 400); const pageSize = parseOptionalIntParam(sp, 'pageSize', { min: 1, max: 100 }); if (!pageSize.ok) return error('VALIDATION_ERROR', pageSize.message, undefined, 400); // Still no persistence in Unit 2 — we just validate and accept the inputs. void query; void page; void pageSize; return success([]); } catch (e: unknown) { return error('INTERNAL_ERROR', (e as Error).message, undefined, 500); }}
The route reads req.nextUrl.searchParams and immediately validates every supported param. This is intentional: we want to reject bad inputs before doing any expensive work.
query is treated as an optional string with constraints { min: 1, max: 64 }. If the param is missing, it becomes undefined. If it’s present but empty/too long after trimming, it fails validation.
page is an optional integer with a minimum of 1. This prevents pagination like page=0 or page=-3 from slipping through.
pageSize is an optional integer constrained to 1–100, which prevents clients from requesting enormous pages and accidentally overloading your backend later.
Validation failures are converted into consistent API errors via error('VALIDATION_ERROR', message, undefined, 400). This ties directly back to the previous lesson: the client always receives the same error envelope shape.
The try/catch is a safety net for unexpected failures. Validation failures are not exceptions—they return early—so the catch block is reserved for true “something went wrong” scenarios and responds with INTERNAL_ERROR.
The route still returns success([]) because Unit 2 is about validation, not persistence. The important part is that the route now accepts or rejects input correctly and does so consistently.
Testing Validation Quickly in the Playground
The Playground at src/app/playground/page.tsx is a simple client-side tool to hit your API routes and display the JSON response. In Unit 2, it builds a /api/products URL from the current input fields so you can quickly try valid and invalid combinations.
This section is important because it mirrors a real-world workflow: you change an input, hit a route, and confirm that validation produces predictable errors.
const productsPath = useMemo(() => { const sp = new URLSearchParams(); if (query.length > 0) sp.set('query', query); if (page.length > 0) sp.set('page', page); if (pageSize.length > 0) sp.set('pageSize', pageSize); return `/api/products?${sp.toString()}`;}, [query, page, pageSize]);
The UI treats query, page, and pageSize as strings because that’s how HTML inputs work—and that matches how query params arrive at the backend.
It only sets params when the input length is greater than 0. This lets you simulate “param missing” vs “param present” by clearing the field entirely.
URLSearchParams ensures proper encoding and avoids manual string concatenation bugs.
Because the backend trims and validates, this UI makes it easy to test cases like " " for query or "abc" for page.
Recap
In this lesson, you made the backend more explicit and safer by separating “what the backend knows” from “what the client can send,” and by validating input at the boundary:
You defined domain types in src/lib/types/domain.ts, including a database-aligned Product shape with price_cents, inventory_count, and a strict 'active' | 'archived' status union.
You defined a DTO input shape in src/lib/types/dto.ts for the query params accepted by GET /api/products.
You implemented reusable validation building blocks in src/lib/http/validation.ts, including type guards and parsers that trim, convert, and return clear validation results.
You wired those parsers into src/app/api/products/route.ts so invalid query params return consistent VALIDATION_ERROR responses with a 400 status.
Next, these same patterns will carry forward naturally: validate at the edges, convert to typed inputs early, and keep your route logic focused on the actual business behavior.
export type ValidationResult<T> = { ok: true; value: T } | { ok: false; message: string };
TypeScript
export function isString( v: unknown, { min = 0, max }: { min?: number; max?: number } = {}): v is string { return typeof v === 'string' && v.length >= min && (max === undefined || v.length <= max);}
TypeScript
export function isInt( v: unknown, { min, max }: { min?: number; max?: number } = {}): v is number { return Number.isInteger(v) && (min === undefined || (v as number) >= min) && (max === undefined || (v as number) <= max);}
Type guards are useful, but route handlers shouldn’t be responsible for trimming strings, dealing with missing params, and crafting error messages over and over. That’s where parsers come in.
parseOptionalStringParam reads a key from URLSearchParams, handles the “missing” case, trims the value, validates it, and returns a ValidationResult.
TypeScript
export function parseOptionalStringParam( searchParams: URLSearchParams, key: string, { min = 0, max }: { min?: number; max?: number } = {}): ValidationResult<string | undefined> { const raw = searchParams.get(key); if (raw === null) return { ok: true, value: undefined }; const trimmed = raw.trim(); if (trimmed.length < min) return { ok: false, message: `${key} must be at least ${min} characters` }; if (max !== undefined && trimmed.length > max) return { ok: false, message: `${key} must be at most ${max} characters` }; return { ok: true, value: trimmed };}
Missing params are not errors. If the key isn’t present, the function returns { ok: true, value: undefined }, which keeps optional params truly optional.
Present params are trimmed. This avoids treating " " as a meaningful value and helps prevent accidental whitespace from affecting behavior.
The length checks enforce constraints with clear, consistent error messages that mention the exact key name.
The return type is string | undefined, which matches how we want to treat optional query params in route logic: “either a valid string or not provided at all.”
This is especially important for query: a missing query should become undefined, not an empty string.
Pagination params need slightly different handling: we must reject empty strings, reject non-integers, and enforce numeric bounds.
parseOptionalIntParam follows the same “optional param” pattern as the string parser, but includes conversion and more numeric-specific validation.
TypeScript
export function parseOptionalIntParam( searchParams: URLSearchParams, key: string, { min, max }: { min?: number; max?: number } = {}): ValidationResult<number | undefined> { const raw = searchParams.get(key); if (raw === null) return { ok: true, value: undefined }; const trimmed = raw.trim(); if (trimmed.length === 0) return { ok: false, message: `${key} must be an integer` }; const parsed = Number(trimmed); if (!Number.isFinite(parsed) || !Number.isInteger(parsed)) return { ok: false, message: `${key} must be an integer` }; if (min !== undefined && parsed < min) return { ok: false, message: `${key} must be >= ${min}` }; if (max !== undefined && parsed > max) return { ok: false, message: `${key} must be <= ${max}` }; return { ok: true, value: parsed };}
Like the string parser, missing params return { ok: true, value: undefined }. This keeps pagination optional unless the API requires it.
An empty-but-present param (like ?page=) is treated as an error. That’s why we explicitly check trimmed.length === 0 and return a helpful message.
The conversion uses Number(trimmed) and then validates both finiteness and integer-ness. This ensures we reject values like "1.5", "abc", or "Infinity".
Bounds checks (min/max) produce clear messages that explain exactly what constraint was violated.
The final result is a typed number, which means downstream code can trust it without re-checking.
TypeScript
import { NextRequest } from 'next/server';import { error, success } from '@/lib/http/response';import { parseOptionalIntParam, parseOptionalStringParam } from '@/lib/http/validation';export async function GET(req: NextRequest) { try { const sp = req.nextUrl.searchParams; const query = parseOptionalStringParam(sp, 'query', { min: 1, max: 64 }); if (!query.ok) return error('VALIDATION_ERROR', query.message, undefined, 400); const page = parseOptionalIntParam(sp, 'page', { min: 1 }); if (!page.ok) return error('VALIDATION_ERROR', page.message, undefined, 400); const pageSize = parseOptionalIntParam(sp, 'pageSize', { min: 1, max: 100 }); if (!pageSize.ok) return error('VALIDATION_ERROR', pageSize.message, undefined, 400); // Still no persistence in Unit 2 — we just validate and accept the inputs. void query; void page; void pageSize; return success([]); } catch (e: unknown) { return error('INTERNAL_ERROR', (e as Error).message, undefined, 500); }}
TypeScript
const productsPath = useMemo(() => { const sp = new URLSearchParams(); if (query.length > 0) sp.set('query', query); if (page.length > 0) sp.set('page', page); if (pageSize.length > 0) sp.set('pageSize', pageSize); return `/api/products?${sp.toString()}`;}, [query, page, pageSize]);