1353
Views
7
Comments
Check input parameter is "Date Type"
Question

Hi. I'm Takahiro.


I am trying to making an API but trouble in checking the Date Type validation .

Would you please tell me the good method to check the Date Type validation?


<Detail>

I want to let the valid input parameter go through, such as '2018-06-19' .

On the other hand, I don't want to let the invalid parameter go through and raze the exception, such as '2018-06-99'.


 

2022-10-20 02-09-39
JeffT

hi Takahiro,

If input parameter is date type , then it will automatically convert into null date if invalid date format given.

You just have to check and throw exception when date is NullDate() :

Cheer,

Rocky

2019-05-22 11-30-09
Marcelo Ferreira

Hi Takahiro,

Just for reference in built-in actions you also have TextToDateValidate() if you want to check if a text is a date.

Regards,

Marcelo


2020-09-15 13-07-23
Kilian Hekhuis
 
MVP

Hi Takahiro,

If your date variable is not in date format, and your API defines it as type Date, you get an error (there's no silent conversion to NullDate() like Rocky claimed). If it's a URL parameter, the error is:

{     "Errors": [         "Failed to parse. 'Date' compatible data type is required for parameter 'Date'."     ],     "StatusCode": 400 }

If it's part of JSON, the error is:

{
    "Errors": [
        "Failed to parse JSON request content."
    ],
    "StatusCode": 400
}

So it's automatically checked for you, no need to check it yourself.

@Rocky: please don't post incorrect information.

@Marcelo: though you are right, this doesn't help much, as TextToDateValidate() is of no use in this case.

UserImage.jpg
Takahiro Esaki

Dear All,


I am grateful for your polite answers.


I use JSOM body, and the error is below. This is not match for my situation.

I intended to handle the request parameter error by the server side.

So,  I wonder if I should use the "Text Type" instead of  "Date Type" and convert "Text" to "Date" after parsing the JSON request content.


Best regards,

Takahiro


2020-09-15 13-07-23
Kilian Hekhuis
 
MVP

Hi Takahiro,

That would indeed be the best solution, I think. Alternatively, you could check stuff in the On After Response, but you only have the raw JSON there, so that might not be the way to go in this case.

That said, since a REST API should never be called without the calling-side do some checks and make sure there's no garbage sent, I'm not sure why you want to check for validity yourself?

UserImage.jpg
Takahiro Esaki

Hi,Kilian. 


Thank you for your response.


I'm sure that.

The reason of handling the error by the called side is ...

We are creating a mobile app and the calling side is being created by Swift.

So I think it is better to handle the whole request parameter exception check by the called side, creating by OSP.


I try to use Text Type and convert it.

Thank you for all.


Takahiro



2020-09-15 13-07-23
Kilian Hekhuis
 
MVP

Hi Takahiro,

Of course, you can do what you want, but if Swift sends an invalid date, you'll have to respond to that in such a way it understands there's some low-level error, which should never happen...

Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.