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:
- 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"
- 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"
- 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"
}
