Skip to content

Commit

Permalink
Valid date message when date is invalid (aces#8279)
Browse files Browse the repository at this point in the history
When using /api/v0.0.3/candidates with an invalid date format, returns 400 Bad Requests/JSON errors.
This  checks the date of birth and returns the correct format in the message ('yyyy-mm-dd') and returns an appropriate error message if the check fails.
  • Loading branch information
regisoc authored and zaliqarosli committed Mar 6, 2023
1 parent de2a4dc commit 9a4ffe5
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
2 changes: 1 addition & 1 deletion modules/api/php/endpoints/candidates.class.inc
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ class Candidates extends Endpoint implements \LORIS\Middleware\ETagCalculator
$pscid,
$project->getId()
);
} catch (\LorisException $e) {
} catch (\LorisException | \InvalidArgumentException $e) {
return new \LORIS\Http\Response\JSON\BadRequest($e->getMessage());
}

Expand Down
13 changes: 9 additions & 4 deletions php/libraries/Candidate.class.inc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ define('PSCID_NOT_UNIQUE', 3);
define('PSCID_INVALID_STRUCTURE', 4);
define('EDC_NOT_SPECIFIED', 5);
define('DOB_NOT_SPECIFIED', 6);
define('DOB_INVALID', 7);

/**
* Wrapper around a candidate in Loris. Mostly, it gets information
Expand Down Expand Up @@ -273,15 +274,19 @@ class Candidate implements \LORIS\StudyEntities\AccessibleResource,
EDC_NOT_SPECIFIED
);
}
if ($dateOfBirth !== null
&& (DateTime::createFromFormat('Y-m-d', $dateOfBirth) === false
|| empty($dateOfBirth))
) {
if (empty($dateOfBirth)) {
throw new InvalidArgumentException(
"Date of Birth must be specified",
DOB_NOT_SPECIFIED
);
}
$dob = DateTime::createFromFormat('!Y-m-d', $dateOfBirth);
if ($dob === false) {
throw new InvalidArgumentException(
"Date of Birth is invalid (expected format: YYYY-MM-DD)",
DOB_INVALID
);
}

if ($PSCIDSettings['generation'] == 'user') {
// check pscid is specified
Expand Down

0 comments on commit 9a4ffe5

Please sign in to comment.