Validation and Transformation
Ensuring data integrity and proper formatting
What is Validation?
Validation is the step where incoming data is checked before business logic runs. It ensures the request is safe, correct, and meaningful.
In a backend flow, validation usually happens after the request hits the API and before it reaches controllers/services.
Why Validation is Important
- Prevents bad or malicious data
- Avoids runtime errors
- Keeps business logic clean
- Improves API reliability
Types of Validation
1. Syntactic Validation (Structure Check)
Checks how data looks.
Examples:
- Required fields are present
- Correct JSON format
- String length, min/max values
{
"email": "user@example.com",
"age": 25
}✔ email exists
✔ age is a number
❌ Missing fields or invalid format → rejected here
2. Type Validation (Data Type Check)
Ensures values are of the expected type.
Examples:
agemust be a numberisAdminmust be booleancreatedAtmust be a date
age: number
isActive: boolean❌ age: "twenty" → invalid
3. Semantic Validation (Meaning Check)
Checks if data makes sense in real life.
Examples:
- Email must be unique
- User must exist in DB
- Start date must be before end date
startDate < endDate✔ Structure is valid ✔ Types are valid ❌ Logic is invalid → rejected here
What is Transformation?
Transformation means changing data into a format your system understands and uses internally.
It happens after validation and before business logic.
Why Transformation is Needed
- Convert raw input into domain models
- Normalize data
- Remove unnecessary fields
- Prepare data for services & database
Common Transformations
1. Type Conversion
"25" → 25
"true" → true2. Rename Fields
user_name → userName3. Add Derived Fields
fullName = firstName + lastName4. Remove Extra Fields
{
;(email, password)
} // drop unknown fields5. Format Standardization
email → lowercase
phone → E.164 formatValidation + Transformation Flow (Mermaid)
Where This Fits in Your Diagram
-
Validation Layer
- Syntactic
- Type
- Semantic
-
Transformation Layer
- DTO → Domain Model
- Clean & normalized data
-
Controller
- No validation logic
- Just orchestration
-
Service
- Pure business rules
Simple Rule to Remember
Validate for correctness, transform for usefulness.