0

Using validator I try to create a simple and elegant validation flow. All validation requirements should be placed in a single json config file, like this one(for example):

{
 name: {
  required: true,
  validator: isLength,
  config: {min:3, max:99}
  }
}

I wrote a function that builds a string: var func = 'Validator.'+funcName+'('+value+','+config+')';

Now I want to execute that string as a function.

I tried it like this(where result is func):

var bla = new Function(Validator, result);

But I get the following warning:

 Validator.isLength(jonathan,{min:0, max:10})
 validator deprecated you tried to validate a undefined but this      library (validator.js) validates strings only. Please update your code as this will be an error soon. native v8natives.js:1259:8

Is there a better way to do what I want, and how can I get it working ?

Thanks

jona jürgen
  • 1,789
  • 4
  • 22
  • 31
  • 1
    Why don't you just do `Validator[funcName](value, config)`? That saves you the hassle of serialising `value` and `config`. – Bergi Feb 18 '16 at 20:43
  • Because I did not kow that was possible in JS :/ Thank you very much ? How exactly does it work ? Do you have a link were I can read up on it ? – jona jürgen Feb 19 '16 at 05:44
  • 1
    It's called "bracket notation" for [property access](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_Accessors) (it's not different for methods than for non-function properties). – Bergi Feb 19 '16 at 14:02

0 Answers0