class-validator icon indicating copy to clipboard operation
class-validator copied to clipboard

isDateString does not work properly

Open Julio-Vasquez opened this issue 6 years ago • 18 comments

** I think it doesn't work properly.**

using postman, I send the following data. 1

The result: 2

The class: 3

I got to that, I thought, maybe it's the format: 4

The result : 5

I am making a mistake or maybe isDateString does not work properly

I am using class-validator 0.11.0

I apologize if I'm not very clear, my English is bad.

Julio-Vasquez avatar Nov 25 '19 08:11 Julio-Vasquez

Looking at documentation @IsDateString() verifies date and time

Checks if a string is a complete representation of a date (e.g. "2017-06-07T14:34:08.700Z", "2017-06-07T14:34:08.700 or "2017-06-07T14:34:08+04:00").

BojanKogoj avatar Nov 25 '19 15:11 BojanKogoj

I have performed the tests with these values, but I still have the same error

Julio-Vasquez avatar Nov 26 '19 05:11 Julio-Vasquez

We have some discussion about this validator https://github.com/typestack/class-validator/issues/412.

Please try @IsISO8601() validator and use date in format 2019-10-04;

vlapo avatar Nov 26 '19 15:11 vlapo

@vlapo and what if we need datetime validation?

cjancsar avatar Dec 19 '19 06:12 cjancsar

This is also bugging me... image

msrumon avatar Jan 22 '21 06:01 msrumon

This is also bugging me... imagen

use @IsISO8601() it worked for me

Julio-Vasquez avatar Jan 22 '21 14:01 Julio-Vasquez

@Julio-Vasquez I tried already, didn't work. Are you sure it works with latest version?

image

msrumon avatar Jan 22 '21 15:01 msrumon

I did this example with version 0.13.1 image

Julio-Vasquez avatar Jan 22 '21 22:01 Julio-Vasquez

I am getting must be a valid ISO 8601 date string for "2021-03-22T17:11:52.279Z"

kodeine avatar Mar 22 '21 17:03 kodeine

Well it seems it's issue of dependency library (validator).

AdamHavlicek avatar May 26 '21 11:05 AdamHavlicek

Any news on this? I am getting this validation error, on version 0.13.2 (same as @kodeine):

{
          "value": "2022-04-16T00:00:00.000Z",
          "property": "myDate",
          "children": [],
          "constraints": {
            "isIso8601": "myDate must be a valid ISO 8601 date string"
          }
}

I tested this example against the regex in the actual validator code, and it matches that. So the regex itself seems fine.

braaar avatar Apr 20 '22 16:04 braaar

I have done some tinkering around and have learned something that I think could be valuable for others.

I was trying to validate a date which was sent as a string from the client application into a graphQL mutation, parsed by GraphQ as a Date object in the GrapQL input type in the backend, and then validated as a Date object.

The only stage where the date was a string was in the frontend (and in the request body), but in my backend, where the validation takes place, the date was never represented as a string.

If we look at this console.log of my object (in the backend, right before applying validation), we can see that the date is not a string due to the lack of quotation marks:

{
  complete: false,
  customer: '123abc,
  assignmentNumber: '3131111',
  myDate: 1970-01-01T00:00:00.000Z,
}

The error message's string representation of the value makes it look like the date is a valid ISO 8601 date string, but it is in fact a date object that cannot be validated as a string.

braaar avatar Apr 21 '22 10:04 braaar

The solution in my case was to use @IsDate instead of @IsISO8601.

braaar avatar Apr 21 '22 10:04 braaar

Just made an update from 0.9.1 to 0.13.2. The old code with the same validation worked - now it's not working anymore. Issue still open:

{
	"error": "Validation Error",
	"status": 400,
	"messages": [
		{
			"key": "dates",
			"messages": [
				"dates must be a valid ISO 8601 date string"
			],
			"value": [
				"2025-07-30T16:25:43.511Z"
			]
		}
	]
}

Validation Block:

@IsNotEmpty({ message: '...' })
@IsDateString({ each: true, message: '...' })
dates: string[];

Our use case needs an ISO timestamp and not just a date, so upper solutions are not valid for us. Downgrading to 0.9.1 is also not an option due to security vulnerabilities.

seanmoghadam avatar May 30 '22 12:05 seanmoghadam

Any update on this decorator @IsDateString()?

subashdbc avatar Jul 16 '22 17:07 subashdbc

I took a look at the validatorjs lib and checked the two regex they use internally:

const iso8601 = /^([\+-]?\d{4}(?!\d{2}\b))((-?)((0[1-9]|1[0-2])(\3([12]\d|0[1-9]|3[01]))?|W([0-4]\d|5[0-3])(-?[1-7])?|(00[1-9]|0[1-9]\d|[12]\d{2}|3([0-5]\d|6[1-6])))([T\s]((([01]\d|2[0-3])((:?)[0-5]\d)?|24:?00)([\.,]\d+(?!:))?)?(\17[0-5]\d([\.,]\d+)?)?([zZ]|([\+-])([01]\d|2[0-3]):?([0-5]\d)?)?)?)?$/;

// same as above, except with a strict 'T' separator between date and time
const iso8601StrictSeparator = /^([\+-]?\d{4}(?!\d{2}\b))((-?)((0[1-9]|1[0-2])(\3([12]\d|0[1-9]|3[01]))?|W([0-4]\d|5[0-3])(-?[1-7])?|(00[1-9]|0[1-9]\d|[12]\d{2}|3([0-5]\d|6[1-6])))([T]((([01]\d|2[0-3])((:?)[0-5]\d)?|24:?00)([\.,]\d+(?!:))?)?(\17[0-5]\d([\.,]\d+)?)?([zZ]|([\+-])([01]\d|2[0-3]):?([0-5]\d)?)?)?)?$/;

Source: https://github.com/validatorjs/validator.js/blob/master/src/lib/isISO8601.js

Both of them look actually fine. I tested them with: https://regex101.com/

and 2025-07-30T16:25:43.511Z was valid...

Now, what I will try in the next days is to pass down the iso8601StrictSeparator option from ValidatorJS. No Idea if that works

EDIT: okay the option is not yet implemented in the current validatorjs version used by class-validator - the regex there just looks like that:

var iso8601 = /^([\+-]?\d{4}(?!\d{2}\b))((-?)((0[1-9]|1[0-2])(\3([12]\d|0[1-9]|3[01]))?|W([0-4]\d|5[0-2])(-?[1-7])?|(00[1-9]|0[1-9]\d|[12]\d{2}|3([0-5]\d|6[1-6])))([T\s]((([01]\d|2[0-3])((:?)[0-5]\d)?|24:?00)([\.,]\d+(?!:))?)?(\17[0-5]\d([\.,]\d+)?)?([zZ]|([\+-])([01]\d|2[0-3]):?([0-5]\d)?)?)?)?$/;

Which is still fine....

seanmoghadam avatar Jul 21 '22 06:07 seanmoghadam

The solution in my case was to use @IsDate instead of @IsISO8601.

The problem with this is that it does not respect dates with timezones. So If I send in: 2022-09-15T17:05:58.077393Z it will return a javascript Date, in UTC

simplenotezy avatar Sep 15 '22 17:09 simplenotezy

The solution in my case was to use @IsDate instead of @IsISO8601.

Same issue here, seems GQL will parse ISO date string into Date instance by nature of DateTime Scalar, So using IsDate should be final answer if working with GQL

jackykwandesign avatar Sep 18 '22 03:09 jackykwandesign

For simple dates I cannot reproduce, the following works with the latest 0.13.2:

import { IsDateString, IsISO8601, validate } from 'class-validator';

class MyPayload {
  @IsISO8601()
  @IsDateString()
  field: string;

  constructor(field: string) {
    this.field = field;
  }
}


validate(new MyPayload('2009-10-01')).then(console.log); // returns no validation errors
validate(new MyPayload('2022-04-16T00:00:00.000Z')).then(console.log); // returns no validation errors

For arrays I can reproduce:

import { IsDateString, IsISO8601, validate } from 'class-validator';

class MyPayload {
  @IsISO8601({ each: true })
  @IsDateString({ each: true })
  field: string[];

  constructor(field: string[]) {
    this.field = field;
  }
}


validate(new MyPayload(['2009-10-01'])).then(console.log); // validation error
validate(new MyPayload(['2022-04-16T00:00:00.000Z'])).then(console.log); // validation error

This is a bug.

NoNameProvided avatar Nov 13 '22 15:11 NoNameProvided

Seems the array one has no open bug report.

NoNameProvided avatar Nov 13 '22 16:11 NoNameProvided

The confirmed bug is now tracked in https://github.com/typestack/class-validator/issues/1843.

NoNameProvided avatar Dec 09 '22 19:12 NoNameProvided

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

github-actions[bot] avatar Jan 09 '23 00:01 github-actions[bot]