Validations & Transformations β€” Lecture Notes


1. Where Validations Fit in the Backend Architecture

A typical backend has three layers:

HTTP Request
     ↓
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Controller Layer                   β”‚  ← HTTP concerns: status codes,
β”‚  [Validation & Transformation here] β”‚    request/response format
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
              ↓
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Service Layer                      β”‚  ← Business logic: emails, notifications,
β”‚                                     β”‚    webhooks, orchestration
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
              ↓
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Repository Layer                   β”‚  ← DB queries: inserts, updates,
β”‚                                     β”‚    deletes, reads
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Validations and transformations happen at the entry point of the Controller layer β€” after route matching, before any business logic or DB calls are made.

Why so early? Garbage in β†’ garbage out. Catching bad data at the boundary prevents propagation deep into the system and avoids cryptic errors (like a 500 Internal Server Error from a DB type constraint violation instead of a clean 400 Bad Request).


2. What is Validation?

Ensuring all incoming data β€” JSON body, query params, path params, headers β€” is in the exact format the API expects before any logic runs.

If validation fails β†’ return 400 Bad Request immediately with a descriptive error. No service call, no DB call.

What validation covers: presence of required fields, correct data types, value constraints, structural patterns, logical consistency.


3. Types of Validation

3.1 Type Validation

The most basic: does the field have the correct data type?

FieldExpectedReceivedResult
agenumber"25" (string)❌ Error
tagsstring[][1, 2] (number[])❌ Error
activeboolean"false" (string)❌ Error

Works recursively β€” nested objects and array elements can each have their own type constraints.


3.2 Syntactic Validation

Does the value satisfy a required structural pattern, regardless of semantic meaning?

FieldValidationExample Failure
emailMust match local@domain.tld pattern"notanemail"
phoneCountry code + digit count pattern"123"
dateMust be valid YYYY-MM-DD format"2025-13-45"
nameLength between 5–100 characters"ab"

These are purely structural β€” a syntactically valid email doesn’t mean the inbox exists.


3.3 Semantic Validation

Does the value make sense in the real world β€” given its context and business rules?

FieldRuleExample Failure
dateOfBirthCannot be in the future2026-01-01
ageMust be between 1–120430
endDateMust be after startDateendDate < startDate

Semantic validation catches logically impossible or unrealistic values that pass syntactic and type checks.


3.4 Complex / Cross-Field Validation

Validation rules that span multiple fields or depend on the value of another field.

Examples:

  • passwordConfirmation must exactly equal password
  • password must be at least 8 characters
  • If married = true, then partnerName is required (otherwise optional)
{
  "password": "secret1",          // must be β‰₯ 8 chars
  "passwordConfirmation": "secret1", // must equal password
  "married": true,
  "partnerName": "Alice"          // required only when married = true
}

These dependencies can’t be expressed field-by-field; the validation logic needs access to the full payload.


4. What is Transformation?

Converting incoming data into the format expected by the service layer β€” before or after validation, as part of the same pipeline.

Why it’s necessary

Query parameters are always strings when they arrive at the server, regardless of what the client intended. A value like ?page=2&limit=20 arrives as { page: "2", limit: "20" }. Validating these as numbers without first casting them will fail β€” the type check sees strings.

Transformation β†’ cast string "2" to number 2 β†’ then validate > 0 && < 500.

Common transformation operations

OperationExample
Type casting"20" β†’ 20 (query param β†’ number)
Normalization"Test@GMAIL.COM" β†’ "test@gmail.com" (email to lowercase)
SanitizationStrip leading/trailing whitespace from strings
Formatting"9876543210" β†’ "+9876543210" (prepend + to phone)
Date normalization"2025-11-05" β†’ ISO 8601 format

5. The Validation & Transformation Pipeline

Validations and transformations are combined into a single reusable pipeline at the controller entry point β€” typically a middleware or utility function configured with a schema.

Incoming Request (JSON / query params / path params)
         ↓
  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
  β”‚  Validation & Transformation β”‚
  β”‚  Pipeline (schema-defined)   β”‚
  β”‚                              β”‚
  β”‚  1. Type casting / transform β”‚
  β”‚  2. Presence checks          β”‚
  β”‚  3. Type validation          β”‚
  β”‚  4. Syntactic validation     β”‚
  β”‚  5. Semantic validation      β”‚
  β”‚  6. Cross-field validation   β”‚
  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
             β”‚
     Pass ───┼─── Fail β†’ 400 Bad Request + error details
             ↓
     Controller β†’ Service β†’ Repository

Keeping everything in one place means you never have to hunt across layers to understand what an API expects.


6. Frontend vs Backend Validation

A critical distinction that is often misunderstood:

Frontend ValidationBackend Validation
PurposeUser experience β€” immediate feedbackSecurity & data integrity
When it runsBefore the API callAfter the API call reaches the server
Can be bypassed?Yes β€” Postman, curl, direct API callsNo β€” always runs
Required?Recommended, not mandatoryMandatory

Never rely on frontend validation for security. A server has many clients β€” a web app, a mobile app, Postman, curl, other servers. Only the backend can enforce constraints unconditionally.

Bad assumption: "Our React form validates the email, so the backend doesn't need to"
Reality: Anyone can call POST /api/users with a raw HTTP request and bypass the form entirely

Rule: Design backend validations independently of whatever the frontend does. Frontend validation is a UX courtesy; backend validation is a security requirement.


7. Summary

ConceptDefinitionLocation
Type validationCheck data types match expected typesEntry point of controller
Syntactic validationCheck value matches a structural pattern (email, phone, date)Entry point of controller
Semantic validationCheck value is logically meaningful (DOB not in future, age ≀ 120)Entry point of controller
Complex validationCross-field rules (password match, conditional required fields)Entry point of controller
TransformationCast/normalize/format data before passing to service layerEntry point of controller

Quick Revision Checklist

  • Validations run at the controller entry point β€” after route match, before any service/DB calls
  • Failed validation β†’ 400 Bad Request with descriptive error; never let bad data reach the service layer
  • Type validation: correct data type (string, number, boolean, array)
  • Syntactic validation: structural pattern match (email, phone, date format, string length)
  • Semantic validation: logical correctness (DOB not in future, age within realistic range)
  • Complex validation: cross-field rules (password confirmation, conditional required fields)
  • Transformation: cast types (query params are always strings β†’ cast to number), normalize (email β†’ lowercase), format (phone β†’ add +)
  • Validation + transformation live in one pipeline β†’ single source of truth for input contract
  • Frontend validation = UX only; Backend validation = security + data integrity; never substitute one for the other
  • Validation errors double as implicit API documentation β€” empty payload returns all expected fields