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

Support for date-time, time string format.

Open DonVietnam opened this issue 2 years ago • 6 comments

Quite often, when developing an API, there is a need to work with time, the most popular input data format with this approach is ISO 8601 ( 2018-11-13T20:20:39+00:00), it would be great to add validation support for this format out of the box. This format supports the JSON Schema specifications and is called 'date-time', there are also two additional formats for 'time' and 'date', since in this case 'date' is already occupied, we can use the 'date-time' type, under which all three JSON Schema formats will fit, or call type 'date' as 'date-string'. If support for this format has already been implemented and I just haven't found it, I apologize.

DonVietnam avatar Sep 19 '23 13:09 DonVietnam

@DonVietnam it's already done by "date" ?

date with convert will just handled all the format from the Date constructor : MDN .

So, ISO 8601 is already supported . with +00:00 (2018-11-13T20:20:39+00:00) timezone, or Z (2018-11-13T20:20:39Z) . Ou can also pass only date 2018-11-13 and so it will output the date to midnight .

Or I missunderstand ?

thib3113 avatar Oct 08 '23 19:10 thib3113

@DonVietnam it's already done by "date" ?

date with convert will just handled all the format from the Date constructor : MDN .

So, ISO 8601 is already supported . with +00:00 (2018-11-13T20:20:39+00:00) timezone, or Z (2018-11-13T20:20:39Z) . Ou can also pass only date 2018-11-13 and so it will output the date to midnight .

Or I missunderstand ?

No, I mean, exactly check if string contains date in ISO 8601 format, not any date string. Date constructor can accept many formats.

DonVietnam avatar Oct 09 '23 06:10 DonVietnam

Oh ! ok, you want to be precise on the check ? not "all what can be a date" ? ( did you have any use case ? ) . Else, you can maybe do it with a string, and a regex pattern ?

thib3113 avatar Oct 09 '23 07:10 thib3113

Oh ! ok, you want to be precise on the check ? not "all what can be a date" ? ( did you have any use case ? ) . Else, you can maybe do it with a string, and a regex pattern ?

Yes, I want to check the date for exact compliance with the format, the essence of such a check is to use the validation library, specifying the desired format to it, and then just put the incoming data in the database. Why do I need a data validation library if I'm going to write regular expressions for data validation myself or translate everything into a Date object, and then drag out the desired format from there myself. Why this particular format? As I have already written, this is the most commonly used format in the API. For example, it is recommended in the Postgresql DBMS, and it is also checked in the JSONSchema standard for a reason.

DonVietnam avatar Oct 09 '23 07:10 DonVietnam

Why do I need a data validation library if I'm going to write regular expressions for data validation myself or translate everything into a Date object, and then drag out the desired format from there myself.

it can handle more than that . it can check multiples properties and return the object . ( and so "date" can be only one of the properties ), if you need it only for date validation, it's maybe not needed ?

About your use case, ok ... but so, you just need to do date.toISOString() ? or, converting to json will already handle a date and convert it to ISO ... And I'm pretty sure your library will already handle it too .

thib3113 avatar Oct 09 '23 07:10 thib3113

I solved it using type string with a regex pattern

FerX avatar Jun 20 '24 16:06 FerX