ansman/validate.js

options are not passed to custom validator function when validate.single is used

Closed this issue · 2 comments

Hello!

Faced with following problem:
When I create and register custom validation function, and then it is called via validate.single, there is no way to pass any custom options (in my specific case i need to utilize attributes in my custom function, as in normal validation).

import validate from 'validate.js';
validate.validators.fooValidator = function(value, options, key, attributes) {
  // validation logic which requires a value of attributes
}
// ...
validate.single('', { fooValidator: true }, { my: 1, options: "object" });

Function is always invoked with following data:
image

Found a solution: its needed to introduce a firth argument for custom validator - globalOptions. This object will contain a desired options, passed as third argument to validate.single

Though, some clarification in documents will anyway be useful. Current behavior is not that obvious..