icebob / fastest-validator

:zap: The fastest JS validator library for NodeJS
MIT License
1.41k stars 88 forks source link

Support for date-time, time string format. #337

Open DonVietnam opened 10 months ago

DonVietnam commented 10 months ago

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.

thib3113 commented 9 months ago

@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 ?

DonVietnam commented 9 months ago

@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.

thib3113 commented 9 months ago

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 ?

DonVietnam commented 9 months ago

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.

thib3113 commented 9 months ago

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 .

FerX commented 4 weeks ago

I solved it using type string with a regex pattern