How to enforce a date value input in a REST API?
The user needs to make a POST
to /api/date
with something like March 13, 2019
or 08/19/2020
. As long as it's a date, it should be accepted.
I have something like this (Using Dropwizard framework)
@POST
public void post(String date)
{
validateDate(date);
//continue
}
private void validateDate(String date)
{
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
try
{
LocalDateTime.parse(date, formatter);
}
catch (DateTimeParseException e)
{
//not a date
}
}
I'm not sure if I'm in the right approach, there must be a better way to validate strings as dates.
java rest
|
show 4 more comments
The user needs to make a POST
to /api/date
with something like March 13, 2019
or 08/19/2020
. As long as it's a date, it should be accepted.
I have something like this (Using Dropwizard framework)
@POST
public void post(String date)
{
validateDate(date);
//continue
}
private void validateDate(String date)
{
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
try
{
LocalDateTime.parse(date, formatter);
}
catch (DateTimeParseException e)
{
//not a date
}
}
I'm not sure if I'm in the right approach, there must be a better way to validate strings as dates.
java rest
1
you are guessing the format (yyyy-MM-dd"
) while first you need to figure out what the format is
– Andrew Tobilko
Nov 21 '18 at 15:03
Is it better to only accept one date format? I was hoping to keep it flexible so different formats can be used.
– RonApple1996
Nov 21 '18 at 15:04
1
@RonApple1996 Just use one date format. APIs should be well-defined contracts.
– Michael
Nov 21 '18 at 15:08
1
@RonApple1996 If you need a date, ask for a date. Given you have the control, I would use an ISO-8601 standard date format e.g. 2011-12-21
– Michael
Nov 21 '18 at 15:17
1
Yea @Hulk, I was just thinking about that. Ahhh dates have so much involved.. I'm returning UTC time zone, so I think logically it makes sense to accept only UTC time as well
– RonApple1996
Nov 21 '18 at 15:23
|
show 4 more comments
The user needs to make a POST
to /api/date
with something like March 13, 2019
or 08/19/2020
. As long as it's a date, it should be accepted.
I have something like this (Using Dropwizard framework)
@POST
public void post(String date)
{
validateDate(date);
//continue
}
private void validateDate(String date)
{
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
try
{
LocalDateTime.parse(date, formatter);
}
catch (DateTimeParseException e)
{
//not a date
}
}
I'm not sure if I'm in the right approach, there must be a better way to validate strings as dates.
java rest
The user needs to make a POST
to /api/date
with something like March 13, 2019
or 08/19/2020
. As long as it's a date, it should be accepted.
I have something like this (Using Dropwizard framework)
@POST
public void post(String date)
{
validateDate(date);
//continue
}
private void validateDate(String date)
{
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
try
{
LocalDateTime.parse(date, formatter);
}
catch (DateTimeParseException e)
{
//not a date
}
}
I'm not sure if I'm in the right approach, there must be a better way to validate strings as dates.
java rest
java rest
asked Nov 21 '18 at 14:58
RonApple1996RonApple1996
947
947
1
you are guessing the format (yyyy-MM-dd"
) while first you need to figure out what the format is
– Andrew Tobilko
Nov 21 '18 at 15:03
Is it better to only accept one date format? I was hoping to keep it flexible so different formats can be used.
– RonApple1996
Nov 21 '18 at 15:04
1
@RonApple1996 Just use one date format. APIs should be well-defined contracts.
– Michael
Nov 21 '18 at 15:08
1
@RonApple1996 If you need a date, ask for a date. Given you have the control, I would use an ISO-8601 standard date format e.g. 2011-12-21
– Michael
Nov 21 '18 at 15:17
1
Yea @Hulk, I was just thinking about that. Ahhh dates have so much involved.. I'm returning UTC time zone, so I think logically it makes sense to accept only UTC time as well
– RonApple1996
Nov 21 '18 at 15:23
|
show 4 more comments
1
you are guessing the format (yyyy-MM-dd"
) while first you need to figure out what the format is
– Andrew Tobilko
Nov 21 '18 at 15:03
Is it better to only accept one date format? I was hoping to keep it flexible so different formats can be used.
– RonApple1996
Nov 21 '18 at 15:04
1
@RonApple1996 Just use one date format. APIs should be well-defined contracts.
– Michael
Nov 21 '18 at 15:08
1
@RonApple1996 If you need a date, ask for a date. Given you have the control, I would use an ISO-8601 standard date format e.g. 2011-12-21
– Michael
Nov 21 '18 at 15:17
1
Yea @Hulk, I was just thinking about that. Ahhh dates have so much involved.. I'm returning UTC time zone, so I think logically it makes sense to accept only UTC time as well
– RonApple1996
Nov 21 '18 at 15:23
1
1
you are guessing the format (
yyyy-MM-dd"
) while first you need to figure out what the format is– Andrew Tobilko
Nov 21 '18 at 15:03
you are guessing the format (
yyyy-MM-dd"
) while first you need to figure out what the format is– Andrew Tobilko
Nov 21 '18 at 15:03
Is it better to only accept one date format? I was hoping to keep it flexible so different formats can be used.
– RonApple1996
Nov 21 '18 at 15:04
Is it better to only accept one date format? I was hoping to keep it flexible so different formats can be used.
– RonApple1996
Nov 21 '18 at 15:04
1
1
@RonApple1996 Just use one date format. APIs should be well-defined contracts.
– Michael
Nov 21 '18 at 15:08
@RonApple1996 Just use one date format. APIs should be well-defined contracts.
– Michael
Nov 21 '18 at 15:08
1
1
@RonApple1996 If you need a date, ask for a date. Given you have the control, I would use an ISO-8601 standard date format e.g. 2011-12-21
– Michael
Nov 21 '18 at 15:17
@RonApple1996 If you need a date, ask for a date. Given you have the control, I would use an ISO-8601 standard date format e.g. 2011-12-21
– Michael
Nov 21 '18 at 15:17
1
1
Yea @Hulk, I was just thinking about that. Ahhh dates have so much involved.. I'm returning UTC time zone, so I think logically it makes sense to accept only UTC time as well
– RonApple1996
Nov 21 '18 at 15:23
Yea @Hulk, I was just thinking about that. Ahhh dates have so much involved.. I'm returning UTC time zone, so I think logically it makes sense to accept only UTC time as well
– RonApple1996
Nov 21 '18 at 15:23
|
show 4 more comments
2 Answers
2
active
oldest
votes
You can accept multiple formats for a date time using the optional syntax ([<your format>])*
. eg
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(
"[yyyy-MM-dd][dd-MM-yyyy][MMMM dd, yyyy]");
add a comment |
EDIT: It is not really clear if you want to know how to validate dates correctly or how to handle invalid inputs to your REST API. My answer shows the latter.
You should use a return value for your post method. You can return javax.ws.rs.core.Response, with that you can control the HTTP code and response object you want to return.
On success, you would normally return the created object with a 200 success code.
On failure, you would return an error code (like 400 Bad request) with a detailed error message ("Date must be in the format yyyy-MM-dd").
To create the response, you can use the ResponseBuilder.
Example:
Response.ok( yourObject ).build(); //success
Response.status( Status.BAD_REQUEST ).entity( yourErrorMessageObject ).build(); // failure
So I would change the code to this:
@POST
public Response post(String date)
{
if(!isDateValid(date)){
return Response.status( Status.BAD_REQUEST ).entity( buildErrorMessage()).build();
}
//continue
Response.ok().build(); // returns nothing on success (like void)
}
private boolean isDateValid(String date)
{
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
try
{
LocalDateTime.parse(date, formatter);
return true;
}
catch (DateTimeParseException e)
{
//not a date
return false;
}
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53414809%2fhow-to-enforce-a-date-value-input-in-a-rest-api%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can accept multiple formats for a date time using the optional syntax ([<your format>])*
. eg
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(
"[yyyy-MM-dd][dd-MM-yyyy][MMMM dd, yyyy]");
add a comment |
You can accept multiple formats for a date time using the optional syntax ([<your format>])*
. eg
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(
"[yyyy-MM-dd][dd-MM-yyyy][MMMM dd, yyyy]");
add a comment |
You can accept multiple formats for a date time using the optional syntax ([<your format>])*
. eg
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(
"[yyyy-MM-dd][dd-MM-yyyy][MMMM dd, yyyy]");
You can accept multiple formats for a date time using the optional syntax ([<your format>])*
. eg
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(
"[yyyy-MM-dd][dd-MM-yyyy][MMMM dd, yyyy]");
answered Nov 21 '18 at 15:13
flakesflakes
6,91212053
6,91212053
add a comment |
add a comment |
EDIT: It is not really clear if you want to know how to validate dates correctly or how to handle invalid inputs to your REST API. My answer shows the latter.
You should use a return value for your post method. You can return javax.ws.rs.core.Response, with that you can control the HTTP code and response object you want to return.
On success, you would normally return the created object with a 200 success code.
On failure, you would return an error code (like 400 Bad request) with a detailed error message ("Date must be in the format yyyy-MM-dd").
To create the response, you can use the ResponseBuilder.
Example:
Response.ok( yourObject ).build(); //success
Response.status( Status.BAD_REQUEST ).entity( yourErrorMessageObject ).build(); // failure
So I would change the code to this:
@POST
public Response post(String date)
{
if(!isDateValid(date)){
return Response.status( Status.BAD_REQUEST ).entity( buildErrorMessage()).build();
}
//continue
Response.ok().build(); // returns nothing on success (like void)
}
private boolean isDateValid(String date)
{
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
try
{
LocalDateTime.parse(date, formatter);
return true;
}
catch (DateTimeParseException e)
{
//not a date
return false;
}
}
add a comment |
EDIT: It is not really clear if you want to know how to validate dates correctly or how to handle invalid inputs to your REST API. My answer shows the latter.
You should use a return value for your post method. You can return javax.ws.rs.core.Response, with that you can control the HTTP code and response object you want to return.
On success, you would normally return the created object with a 200 success code.
On failure, you would return an error code (like 400 Bad request) with a detailed error message ("Date must be in the format yyyy-MM-dd").
To create the response, you can use the ResponseBuilder.
Example:
Response.ok( yourObject ).build(); //success
Response.status( Status.BAD_REQUEST ).entity( yourErrorMessageObject ).build(); // failure
So I would change the code to this:
@POST
public Response post(String date)
{
if(!isDateValid(date)){
return Response.status( Status.BAD_REQUEST ).entity( buildErrorMessage()).build();
}
//continue
Response.ok().build(); // returns nothing on success (like void)
}
private boolean isDateValid(String date)
{
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
try
{
LocalDateTime.parse(date, formatter);
return true;
}
catch (DateTimeParseException e)
{
//not a date
return false;
}
}
add a comment |
EDIT: It is not really clear if you want to know how to validate dates correctly or how to handle invalid inputs to your REST API. My answer shows the latter.
You should use a return value for your post method. You can return javax.ws.rs.core.Response, with that you can control the HTTP code and response object you want to return.
On success, you would normally return the created object with a 200 success code.
On failure, you would return an error code (like 400 Bad request) with a detailed error message ("Date must be in the format yyyy-MM-dd").
To create the response, you can use the ResponseBuilder.
Example:
Response.ok( yourObject ).build(); //success
Response.status( Status.BAD_REQUEST ).entity( yourErrorMessageObject ).build(); // failure
So I would change the code to this:
@POST
public Response post(String date)
{
if(!isDateValid(date)){
return Response.status( Status.BAD_REQUEST ).entity( buildErrorMessage()).build();
}
//continue
Response.ok().build(); // returns nothing on success (like void)
}
private boolean isDateValid(String date)
{
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
try
{
LocalDateTime.parse(date, formatter);
return true;
}
catch (DateTimeParseException e)
{
//not a date
return false;
}
}
EDIT: It is not really clear if you want to know how to validate dates correctly or how to handle invalid inputs to your REST API. My answer shows the latter.
You should use a return value for your post method. You can return javax.ws.rs.core.Response, with that you can control the HTTP code and response object you want to return.
On success, you would normally return the created object with a 200 success code.
On failure, you would return an error code (like 400 Bad request) with a detailed error message ("Date must be in the format yyyy-MM-dd").
To create the response, you can use the ResponseBuilder.
Example:
Response.ok( yourObject ).build(); //success
Response.status( Status.BAD_REQUEST ).entity( yourErrorMessageObject ).build(); // failure
So I would change the code to this:
@POST
public Response post(String date)
{
if(!isDateValid(date)){
return Response.status( Status.BAD_REQUEST ).entity( buildErrorMessage()).build();
}
//continue
Response.ok().build(); // returns nothing on success (like void)
}
private boolean isDateValid(String date)
{
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
try
{
LocalDateTime.parse(date, formatter);
return true;
}
catch (DateTimeParseException e)
{
//not a date
return false;
}
}
edited Nov 21 '18 at 15:19
answered Nov 21 '18 at 15:08
Michael KleimannMichael Kleimann
37217
37217
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53414809%2fhow-to-enforce-a-date-value-input-in-a-rest-api%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
you are guessing the format (
yyyy-MM-dd"
) while first you need to figure out what the format is– Andrew Tobilko
Nov 21 '18 at 15:03
Is it better to only accept one date format? I was hoping to keep it flexible so different formats can be used.
– RonApple1996
Nov 21 '18 at 15:04
1
@RonApple1996 Just use one date format. APIs should be well-defined contracts.
– Michael
Nov 21 '18 at 15:08
1
@RonApple1996 If you need a date, ask for a date. Given you have the control, I would use an ISO-8601 standard date format e.g. 2011-12-21
– Michael
Nov 21 '18 at 15:17
1
Yea @Hulk, I was just thinking about that. Ahhh dates have so much involved.. I'm returning UTC time zone, so I think logically it makes sense to accept only UTC time as well
– RonApple1996
Nov 21 '18 at 15:23