StaffCoder
Spring Boot
Spring BootSpring Boot 4.1Java 25
12 modules · 48 challengesSign up
Module 3 · Validation and Error Handling

Blood Donation Eligibility Check

easy100%1 of 1 solved

Problem statement

Build a REST API for checking blood donation eligibility using a custom validation constraint.

Requirements

EligibleDonor and EligibleDonorValidator

DonorEligibilityRequest is annotated with EligibleDonor at class level.

Complete EligibleDonor so that:

  • The validation engine recognizes it as a constraint that applies to a class
  • It stays available at runtime
  • It routes to EligibleDonorValidator for checking
  • It declares the standard message, groups, and payload members every constraint has

Complete EligibleDonorValidator so that:

  • It evaluates three checks in this fixed order:
    1. weightKg must be at least 50; a donor weighing exactly 50 passes this check, and 49 fails it. Reason: "Donor must weigh at least 50 kg"
    2. age must be between 18 and 65 inclusive; donors aged exactly 18 or exactly 65 pass this check, while 17 and 66 fail it. Reason: "Donor age must be between 18 and 65"
    3. daysSinceLastDonation must be at least 90; exactly 90 days ago passes this check, and 89 fails it. Reason: "Last donation must be at least 90 days ago"
  • When a check fails, the constraint reports exactly one violation whose message is the reason of the first failing check in that order; later checks are not evaluated and never appear
  • When every check passes, the constraint reports no violation
  • When weightKg, age, or daysSinceLastDonation is null, the constraint reports no violation of its own; the required-field rules below cover missing values
  • The three checks belong to the constraint, not to the endpoint: validating a DonorEligibilityRequest object with the validation engine directly reports the same single reason

DonorController

POST /donors/eligibility

  • Accept a JSON body with the integer fields weightKg, age, and daysSinceLastDonation, and validate it
  • If weightKg is missing or null, return 400 Bad Request with error message: "Weight is required"
  • If age is missing or null, return 400 Bad Request with error message: "Age is required"
  • If daysSinceLastDonation is missing or null, return 400 Bad Request with error message: "Days since last donation is required"
  • A missing field is reported even when the supplied values would also be ineligible; the eligibility checks run only on complete requests
  • If the body is complete but a check fails, return 400 Bad Request whose error message is the reason of the first failing check
  • If the donor passes all three checks, return 200 OK with a body whose eligible field is true

Sample Requests & Responses

Expand to view details on sample requests and responses.

POST /donors/eligibility

Request:

{
   "weightKg": 70,
   "age": 30,
   "daysSinceLastDonation": 120
}

Response (200 OK):

{
   "eligible": true
}

POST /donors/eligibility

Request:

{
   "weightKg": 49,
   "age": 30,
   "daysSinceLastDonation": 120
}

Response (400 Bad Request):

{
   "error": "Donor must weigh at least 50 kg",
   "timestamp": "2026-07-30T10:00:07.754378",
   "path": "/donors/eligibility"
}

POST /donors/eligibility

Request:

{
   "weightKg": 70,
   "age": 66,
   "daysSinceLastDonation": 120
}

Response (400 Bad Request):

{
   "error": "Donor age must be between 18 and 65",
   "timestamp": "2026-07-30T10:00:07.770830",
   "path": "/donors/eligibility"
}

POST /donors/eligibility

Request:

{
   "weightKg": 70,
   "age": 30,
   "daysSinceLastDonation": 89
}

Response (400 Bad Request):

{
   "error": "Last donation must be at least 90 days ago",
   "timestamp": "2026-07-30T10:00:07.781497",
   "path": "/donors/eligibility"
}

POST /donors/eligibility

Request:

{
   "weightKg": 49,
   "age": 17,
   "daysSinceLastDonation": 89
}

Response (400 Bad Request):

{
   "error": "Donor must weigh at least 50 kg",
   "timestamp": "2026-07-30T10:00:07.792851",
   "path": "/donors/eligibility"
}

POST /donors/eligibility

Request:

{
   "age": 30,
   "daysSinceLastDonation": 120
}

Response (400 Bad Request):

{
   "error": "Weight is required",
   "timestamp": "2026-07-30T10:00:07.804227",
   "path": "/donors/eligibility"
}

More in Validation and Error Handling

See the module
Passport Application Form ValidatorThis problem covers checking an application form field by field before it is accepted, including an age cutoff on the date of birth.50%1 of 2 solvedeasyGlobal Error Response StandardizerThis problem covers turning every kind of failure in the application into one consistent error response.100%1 of 1 solvedeasyPizza Order Constraint ValidatorThis problem covers collecting every broken rule on a request instead of stopping at the first, and reporting them in a fixed order.100%1 of 1 solvedeasy

We use cookies that are necessary to run Staffcoder, and optional analytics cookies to see which pages get used. Analytics stays off unless you accept. Read our Cookie Policy.