leepowelldev / mongoose-validator

Validators for mongoose models utilising validator.js
MIT License
379 stars 43 forks source link

How extend the node-validator library if I use this module #6

Closed miwi100 closed 11 years ago

miwi100 commented 11 years ago

Hallo,

i want to use this plugin. But how I can use my extended node-validator plugins? The normal why https://github.com/chriso/node-validator#extending-the-library dosn't work, because I use var validate = require('mongoose-validator').validate instead of var Validator = require('validator').Validator;

So what is the best way to extend the node-validator library and use your code?

Thank you for your answer!

Michael

leepowelldev commented 11 years ago

Hi, I'm currently working on this, along with a couple of other tweaks - update should be released in the next few days.

miwi100 commented 11 years ago

Cool thank you very much!

leepowelldev commented 11 years ago

I have included this functionality, how would something like this work for you?

// method name, function, default error message require('mongoose-validator').extend('isBoolean', function(val) { return 'boolean' === typeof val; }, 'Not a boolean');

miwi100 commented 11 years ago

Yes sure, this looking good. Only one Question. Can I set function values like: len(min, max) ?

leepowelldev commented 11 years ago

Yes you'll be able to use the new custom validator the same as all the others:

validate('isBoolean', 'arg1', 'arg2');

miwi100 commented 11 years ago

Yes, cool I am glad to see the update :) Thank you!

leepowelldev commented 11 years ago

Should should be able to try out the new features on this pre-release branch - let me know how you get on:

https://github.com/leepowellcouk/mongoose-validator/tree/0.2.1

miwi100 commented 11 years ago

Hmm, I'm not sure. At the first time it seems to work, but I'm not sure.

So at the moment with the following code the value is undefined:

var mongoose = require('mongoose'), validate = require('mongoose-validator').validate;

require('mongoose-validator').extend("isBoolean", (function(val) { console.log(val, "Value"); // -> everytime undefined return "boolean" === typeof val; }), "Not a boolean");

miwi100 commented 11 years ago

Other test:

validate: [validate('testvalidator', 3, "String")]

require('mongoose-validator').extend("testvalidator", function (val, arg1, arg2) { console.log(val, "Value"); //3 console.log(arg1, "Arg1"); //String console.log(arg2, "Arg2"); //undefined return true; }, "testvalidator Error");

So I don't get the value?

miwi100 commented 11 years ago

Custom error messages works fine. Both in node and custom validators.

leepowelldev commented 11 years ago

Yes, sorry, just noticed an issue - will patch and submit to the branch asap.

leepowelldev commented 11 years ago

var mongoose = require('mongoose'), validate = require('mongoose-validator').validate;

require('mongoose-validator').extend("isBoolean", (function(val) { console.log(val, "Value"); // -> everytime undefined return "boolean" === typeof val; }), "Not a boolean");

This extension is incorrect as you're not passing in any arguments like

validate('isBoolean');

if you wanted to pass in an argument you can do so like:

validate('isBoolean', 'arg1', 'arg2');

Also, my example was incorrect, so instead of testing against 'val' you should be testing against this.str like so:

require('mongoose-validator').extend("isBoolean", (function() { console.log(arguments); // Will be empty unless you pass any like above return "boolean" === typeof this.str; }), "Not a boolean");

leepowelldev commented 11 years ago

So this would need to be:

validate: [validate('testvalidator', 3, "String")]

require('mongoose-validator').extend("testvalidator", function (val, arg1, arg2) { console.log(val, "Value"); //3 console.log(arg1, "Arg1"); //String console.log(arg2, "Arg2"); //undefined <-- This is correct as you only pass 2 arguments into function: 3 and "String" return true; }, "testvalidator Error");

To get the current value being validated, use this.str

leepowelldev commented 11 years ago

I have updated the documentation with the correct example

thomas-riccardi commented 10 years ago

This mongoose-validator extend feature still doesn't work with extended node-validator: we have to duplicate the extensions on mongoose-validator.

Ideally we should be able to use our extended node-validator like this for example:

var Validator = require('validator').Validator;
Validator.prototype.isSomething = function() {
  if (! this.str.match(/^something$/)) {
    this.error(this.msg || 'Invalid something');
  }
  return this;
};
var validate = require('mongoose-validator').extend(Validator);
...
validate('isSomething');

This way we can extend the original node-validator, use it, and also get for free the extension on mongoose-validator.

However it makes mongoose-validator potentially use a node-validator with an unsupported version. But I still think it would be a useful feature if this limitation is explained in the Readme.

leepowelldev commented 10 years ago

That's a good idea. You're welcome to submit a pull request with the extra functionality in place - or I'll take a look at it when I have time over the next few weeks.