ansman / validate.js

A declarative validation library written javascript
https://validatejs.org
MIT License
2.63k stars 336 forks source link

Datetime Validator option functions not being called #152

Closed thomasrstorey closed 8 years ago

thomasrstorey commented 8 years ago

In the documentation, it states "validate.js allows the validators object and validator options to be a function that should return the constraints/options".

I am trying to make a Date Range field that will correctly validate ranges where the start is after the end, or the end is before the start, as invalid. I have implemented an earliest option on a date constraint for my date_end field as follows:

date: {
    earliest: function(val, attr, opts, attrs){
                  return moment.utc(attrs.date_start);
              }
}

Additionally I have defined a parse function on the validator as follows:

parse: function(value, options) {
            return +moment.utc(value);
       },

However on inspection, I find that the value getting passed to my parse function is not the return value of the above function, but the function object itself, which is evaluated by +moment.utc as NaN Shouldn't my parse be getting the return value of my earliest function instead of the function itself? This seems to be what is described in the documentation.

Thanks for a great library otherwise!

ansman commented 8 years ago

The value returned from your first example should be the options for the date validator meaning it should be an object with options, see which ones that are available here: http://validatejs.org/#validators-datetime

thomasrstorey commented 8 years ago

Ah I see. Thanks for clearing that up!