Week 5: Validation & Pagination
Add basic validation to entity classes so the API rejects obviously bad input. Optionally, implement pagination and validation error responses now — or defer them to a standalone extension page before the capstone.
Mandatory core (everyone completes this)
Section titled “Mandatory core (everyone completes this)”This part takes 1–2 hours. It is required before moving on to Week 6.
Core skills:
- Add Data Annotations to entity classes
- Confirm that invalid input is rejected in Swagger
Key resources
Section titled “Key resources”LLM prompts
Section titled “LLM prompts”“Add Data Annotations to my Book class: Title required and max 200 chars, Year between 1000 and 2024.”
“Add [Required] and [MaxLength(100)] to my Author class. Show me how the API automatically rejects invalid input.”
Learning activities
Section titled “Learning activities”- Add
[Required],[MaxLength(200)], and[Range(1000, 2024)]to the Book class - Add
[Required]and[MaxLength(100)]to the Author class - Run the API and confirm in Swagger that POST /books rejects an empty title with a 400 response
Key concepts
Section titled “Key concepts”- Data Annotations:
[Required],[MaxLength],[Range],[EmailAddress] - How ASP.NET Core automatically validates annotated models before the endpoint body runs
Extension: validation responses & pagination (do now or defer)
Section titled “Extension: validation responses & pagination (do now or defer)”This section covers returning structured validation errors and implementing pagination on list endpoints. It is required before starting the capstone, but you may complete it now or return to it later.
Validation error responses
Section titled “Validation error responses”Core skills:
- Return structured validation error messages from endpoints
- Use
Results.ValidationProblem()for consistent error format
LLM prompt:
“Show me how to check if a Book is valid in my POST endpoint and return a structured BadRequest response with field-level error messages.”
Learning activities:
- Update the POST /books endpoint to return
Results.ValidationProblem()when validation fails - Test in Swagger — confirm the 400 response includes which fields failed and why
- Repeat for POST /authors
Pagination
Section titled “Pagination”Core skills:
- Accept
pageandpageSizequery parameters on list endpoints - Use
.Skip()and.Take()to page through database results - Return total count alongside results
LLM prompts:
“Implement simple pagination for GET /books. Accept page and pageSize parameters, use Skip() and Take(), return total count.”
“Show me how to return a total count alongside a paginated list in a Minimal API endpoint.”
Learning activities:
- Add
pageandpageSizequery parameters to GET /books (defaults: page=1, pageSize=10) - Use
.Skip((page - 1) * pageSize).Take(pageSize)in the database query - Return total count alongside the paged results
- Seed 15+ books and test fetching page 1 and page 2 in Swagger
Optional (skip if needed)
Section titled “Optional (skip if needed)”- Simple title search using
.Contains() - Filter books by author name
- Sorting
Deliverable
Section titled “Deliverable”Mandatory: Book and Author entities have Data Annotations. Invalid input is rejected with a 400 response.
Full week: API also returns structured validation error messages and supports pagination on GET /books.
Week 3–4: PostgreSQL | Back to Overview | Week 6: Authentication | Extension: Validation & Pagination