Skip to content

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.

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

“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.”

  1. Add [Required], [MaxLength(200)], and [Range(1000, 2024)] to the Book class
  2. Add [Required] and [MaxLength(100)] to the Author class
  3. Run the API and confirm in Swagger that POST /books rejects an empty title with a 400 response
  • 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.

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:

  1. Update the POST /books endpoint to return Results.ValidationProblem() when validation fails
  2. Test in Swagger — confirm the 400 response includes which fields failed and why
  3. Repeat for POST /authors

Core skills:

  • Accept page and pageSize query 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:

  1. Add page and pageSize query parameters to GET /books (defaults: page=1, pageSize=10)
  2. Use .Skip((page - 1) * pageSize).Take(pageSize) in the database query
  3. Return total count alongside the paged results
  4. Seed 15+ books and test fetching page 1 and page 2 in Swagger
  • Simple title search using .Contains()
  • Filter books by author name
  • Sorting

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