question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Date is not validated correctly as type: string, format: date-time

See original GitHub issue

Hi

I want to return the created and lastModified timestamp from the database which I define in my ORM with type Date.

If I print the response object in my express router before returning it, it looks like this:

User {
   id: '1fe87c1d-5d31-4426-8757-aa5e62d2344e',
   lastModified: 2020-02-21T09:26:43.603Z,
   created: 2020-02-21T09:25:48.151Z,

If I call the endpoint I get the following reponse object:

{
  "data": [
    {
      "id": "1fe87c1d-5d31-4426-8757-aa5e62d2344e",
      "lastModified": "2020-02-21T09:26:43.603Z",
      "created": "2020-02-21T09:25:48.151Z"
    },
...

-> correctly stringified

As soon as I set the validateResponses option in the OpenApiValidator on true, I get the following error message:

{
  "error": ".response.data[0].lastModified should be string, .response.data[0].created should be string,

… although it is a string in the upper example

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:4
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
martinbarkcommented, Feb 25, 2020

I just hit this same issue today. It’s because the date is an object not a string. At the point the response it validated it’s still a Date object. When you print it date.toJSON() is called to convert it to a string. It’s not specific to Date, you would see this issue on any object that has a toJSON() function

The only reliable way i found to work around this is to JSON.stringify() then JSON.parse() the response body before it’s validated. This means the response body is now formatted as the destination will see the message before it’s validated.

I added this code after OpenApiValidator was been installed as a workaround

app.use((req, res, next) => {
  const original = res.json;
  res.json = function jsonp(json) {
    return original.call(this, JSON.parse(JSON.stringify(json)));
  };
  next();
});
1reaction
severinwullschlegercommented, Feb 25, 2020

Thanks a lot. The workaround worked for me too.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I validate a date string format in python?
import datetime >>> def validate(date_text): try: ... This raises a ValueError if the date is not formatted correctly:
Read more >
Python - Validate String date format - GeeksforGeeks
Given a date format and a string date, the task is to write a python program to check if the date is valid...
Read more >
Check If a String Is a Valid Date in Java - Baeldung
In the case of date inputs, we may need to verify the following: The input contains the date in a valid format, such...
Read more >
How to Validate Date and Time Strings - Datatest
To validate date and time formats, we can define a helper function that uses strftime codes to check for matching strings. In the...
Read more >
String is not recognized as a valid datetime - UiPath Forum
Try this. It will convert any type string date format to required string format. CDate("inputStrDate").Tostring("dd-MM-yyyy ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found