class-validator
class-validator copied to clipboard
isDateString does not work properly
** I think it doesn't work properly.**
using postman, I send the following data.
The result:
The class:
I got to that, I thought, maybe it's the format:
The result :
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.
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").
I have performed the tests with these values, but I still have the same error
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 and what if we need datetime validation?
This is also bugging me...

This is also bugging me...
use @IsISO8601() it worked for me
@Julio-Vasquez I tried already, didn't work. Are you sure it works with latest version?

I did this example with version 0.13.1

I am getting must be a valid ISO 8601 date string for "2021-03-22T17:11:52.279Z"
Well it seems it's issue of dependency library (validator).
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.
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.
The solution in my case was to use @IsDate instead of @IsISO8601.
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.
Any update on this decorator @IsDateString()?
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....
The solution in my case was to use
@IsDateinstead 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
The solution in my case was to use
@IsDateinstead 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
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.
Seems the array one has no open bug report.
The confirmed bug is now tracked in https://github.com/typestack/class-validator/issues/1843.
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.