poppinss / indicative

Indicative is a simple yet powerful data validator for Node.js and browsers. It makes it so simple to write async validations on nested set of data.
https://indicative.adonisjs.com/
MIT License
417 stars 52 forks source link

dateFormat: Can't ensures date is valid when using 'DD/MM/YYYY' as a format #195

Closed pellizzetti closed 5 years ago

pellizzetti commented 6 years ago

Problem

Using dateFormat:DD/MM/YYYY as rule will always fail validation.

Behavior

If I make a request with the following data:

console.log(request.input('birthday'))
// 02/05/1963

Using this rule:

const rules = {
  birthday: 'required|dateFormat:DD/MM/YYYY'
}

The validation fails. I think that happens because raw/dateFormat.js -> date-fns/format is receiving a String has first parameter. Passing a Date object return the right date/format:

const stringFmt = dateFns.format('02/05/1963', 'DD/MM/YYYY')
const dateFmt = dateFns.format(new Date(1963, 4, 2), 'DD/MM/YYYY')

console.log(stringFmt)  // 05/02/1963
console.log(dateFmt)    // 02/05/1963

JSFiddle link

Specifications

thetutlage commented 6 years ago

This is directly a shortcoming of Javascript and really hard to write a workaround for.

If you pass the raw string to the new Date function, it does swap the month with the date, since that is the default format for Javascript.

new Date('02/05/1963')
// Tue Feb 05 1963 00:00:00 GMT+0530 (India Standard Time)

Do u have any suggestions?

pellizzetti commented 6 years ago

This is directly a shortcoming of Javascript and really hard to write a workaround for.

I did not know that (I'm somewhat new to the JS ecosystem) but, yeah, I thought it wouldn't be an easy task.

Do u have any suggestions?

Not at the moment, sorry. But next month, when I have more free time, I can give some attention to the subject and try to get some solutions.

thetutlage commented 6 years ago

Sure, I will keep this issue opened and see if I can find something solid around it

thetutlage commented 5 years ago

After looking into multiple ways, I hardly think, there is any way to work around this issue. If you are still looking for a solution, then feel free to open the issue and discuss ways around it

MarcosGin commented 4 years ago

Hi, I'm using Adonis.js, and I'm having the same problem, any news about this?

radiumrasheed commented 3 years ago

Anyone else found a workaround for this?