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

Bug Adonisjs Indicative: dateFormat:'YYYY-MM-DD HH:mm:ss' always invalid #226

Closed begueradj closed 5 years ago

begueradj commented 5 years ago

Problem

dateFormat:'YYYY-MM-DD HH:mm:ss' never gets validated unless I remove HH:mm:ss or keep only: YYYY-MM-DD HH.

The exact error message I see in the console:

dateFormat validation failed on effective_datetime

Sample code

get rules () {
  return {
    name: 'required',
    effective_datetime: 'required|date|dateFormat:YYYY-MM-DD HH:mm:ss'
  }
}

Package version

Current

Node.js and npm version

npm: 6.9.0 Node.js: 11.8.0 AdonisJs: 4.1.0

BONUS (a sample repo to reproduce the issue)

Link

RomainLanz commented 5 years ago

Hey @begueradj! 👋

This rule is well tested: https://github.com/poppinss/indicative/blob/develop/test/node/validations.spec.js#L421

If you believe there's a bug, could you please create a test case that fail?

begueradj commented 5 years ago

There is at least this test that covers this case. However the demo I shared shows there is a problem with dateFormat(). Actually this was asked as a question on the forum, this means an other user stumbled on the exact same issue

webNeat commented 5 years ago

The Limitations section in the docs says:

Since the validation rules are defined using a string based expression, it has certain limitations. You cannot use reserved keywords like : or a |, within arguments.

So you should rewrite your code as

const {rule} = use('Validator')

//...
  get rules() {
    return {
      name: 'required',
      effective_datetime: [
        rule('required'),
        rule('date'),
        rule('dateFormat', 'YYYY-MM-DD HH:mm:ss')
      ]
    }
  }
begueradj commented 5 years ago

I read that section but I guess I did not take it seriously. I did not try your solution but I believe it will work. Thank you very much for the feedback. I will try it ASAP. @webNeat