Closed miwi100 closed 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.
Cool thank you very much!
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');
Yes sure, this looking good. Only one Question. Can I set function values like: len(min, max) ?
Yes you'll be able to use the new custom validator the same as all the others:
validate('isBoolean', 'arg1', 'arg2');
Yes, cool I am glad to see the update :) Thank you!
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
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");
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?
Custom error messages works fine. Both in node and custom validators.
Yes, sorry, just noticed an issue - will patch and submit to the branch asap.
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");
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
I have updated the documentation with the correct example
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.
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.
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