1602 / jugglingdb

Multi-database ORM for nodejs: redis, mongodb, mysql, sqlite3, postgresql, arango, in-memory...
http://1602.github.io/jugglingdb/
2.04k stars 241 forks source link

Add the ability to create new generic validator #112

Closed mightymephisto closed 7 years ago

mightymephisto commented 12 years ago

Hello

It would be nice to be able to add a custom validator that could be used again against different fields. Yes I can declare as a callback, but for neatness I feel that it would be better.

anatoliychakkaev commented 12 years ago

You can do it using validate and validateAsync methods.

mightymephisto commented 12 years ago

Yeah that's what I have been doing, but for instance I wanted to have a function such as

validatesTypeOf

It would be something I'd use again and again, so it would be good to add my own validators.

Is it possible to chain validators too?

On a side note, if you add an async custom function which doesn't function asyncronously, it causes the callback passed into isValid to be called syncronously, if you have any other async calls, such as validatesUniquenessOf then the callback is called twice

In my code I had an email validator, where I was using the node dns functions to check the MX records. Before this was done I was checking the the email field was a string and matched a regex, as there was no point in doing the dns check if that failed. If I returned early by a string fail or regex failed, I got the odd behavior; I got around it by wrapping in a process.nextTick.

Not saying it's a bug in your code, it was something in mine, but it maybe worth adding to the documenation that done() needs to be called inside an async function, so process.nextTick is needed if the function is not.

Thanks for the fantastic module though, it is really helping me with my project :)

Sent from my ZX81

On 23 Aug 2012, at 08:12, Anatoliy Chakkaev notifications@github.com wrote:

You can do it using validate and validateAsync methods.

— Reply to this email directly or view it on GitHubhttps://github.com/1602/jugglingdb/issues/112#issuecomment-7961781.

1602 commented 12 years ago

Ah, yeah I see what you mean. It's nice feature, I agree. Probably it should be modified in Validatable:

require('jugglingdb').Validatable.validatesTypeOf = function () { ... };

I will think about the best API implementation.

I don't get point about chain validations.

About sync/async validations, I believe callback should not called twice. Thanks for spotting, I'll try to reproduce. Unit test case are welcome as well.

On Thu, Aug 23, 2012 at 1:16 PM, Rob Bruce notifications@github.com wrote:

Yeah that's what I have been doing, but for instance I wanted to have a function such as

validatesTypeOf

It would be something I'd use again and again, so it would be good to add my own validators.

Is it possible to chain validators too?

On a side note, if you add an async custom function which doesn't function asyncronously, it causes the callback passed into isValid to be called syncronously, if you have any other async calls, such as validatesUniquenessOf then the callback is called twice

In my code I had an email validator, where I was using the node dns functions to check the MX records. Before this was done I was checking the the email field was a string and matched a regex, as there was no point in doing the dns check if that failed. If I returned early by a string fail or regex failed, I got the odd behavior; I got around it by wrapping in a process.nextTick.

Not saying it's a bug in your code, it was something in mine, but it maybe worth adding to the documenation that done() needs to be called inside an async function, so process.nextTick is needed if the function is not.

Thanks for the fantastic module though, it is really helping me with my project :)

Sent from my ZX81

On 23 Aug 2012, at 08:12, Anatoliy Chakkaev notifications@github.com wrote:

You can do it using validate and validateAsync methods.

— Reply to this email directly or view it on GitHubhttps://github.com/1602/jugglingdb/issues/112#issuecomment-7961781.

— Reply to this email directly or view it on GitHubhttps://github.com/1602/jugglingdb/issues/112#issuecomment-7964121.

Thanks, Anatoliy Chakkaev

mightymephisto commented 12 years ago

I'll see if I can add it that way, thanks for the pointer.

Chained validators would be say

Validate the data type, if that passes then do the next validators, if that passes do the next and so on.

Basically, if you have multiple validators on the same field, have the option to stop the others from running if it fails further up the chain.

Code to reproduce would be something like,

User.validatesUniquenessOf('email'); User.validateAsync('email', function(err, done) { done(); });

var u = new User({ email: "rob@foo.com" });

u.isValid(function(valid) { console.log('boooom'); });

Sorry on my phone and had some beers so maybe not be quite right, but hope you get what I mean!!

Sent from my ZX81

On 23 Aug 2012, at 22:37, Anatoliy Chakkaev notifications@github.com wrote:

Ah, yeah I see what you mean. It's nice feature, I agree. Probably it should be modified in Validatable:

require('jugglingdb').Validatable.validatesTypeOf = function () { ... };

I will think about the best API implementation.

I don't get point about chain validations.

About sync/async validations, I believe callback should not called twice. Thanks for spotting, I'll try to reproduce. Unit test case are welcome as well.

On Thu, Aug 23, 2012 at 1:16 PM, Rob Bruce notifications@github.com wrote:

Yeah that's what I have been doing, but for instance I wanted to have a function such as

validatesTypeOf

It would be something I'd use again and again, so it would be good to add my own validators.

Is it possible to chain validators too?

On a side note, if you add an async custom function which doesn't function asyncronously, it causes the callback passed into isValid to be called syncronously, if you have any other async calls, such as validatesUniquenessOf then the callback is called twice

In my code I had an email validator, where I was using the node dns functions to check the MX records. Before this was done I was checking the the email field was a string and matched a regex, as there was no point in doing the dns check if that failed. If I returned early by a string fail or regex failed, I got the odd behavior; I got around it by wrapping in a process.nextTick.

Not saying it's a bug in your code, it was something in mine, but it maybe worth adding to the documenation that done() needs to be called inside an async function, so process.nextTick is needed if the function is not.

Thanks for the fantastic module though, it is really helping me with my project :)

Sent from my ZX81

On 23 Aug 2012, at 08:12, Anatoliy Chakkaev notifications@github.com wrote:

You can do it using validate and validateAsync methods.

— Reply to this email directly or view it on GitHubhttps://github.com/1602/jugglingdb/issues/112#issuecomment-7961781.

— Reply to this email directly or view it on GitHubhttps://github.com/1602/jugglingdb/issues/112#issuecomment-7964121.

Thanks, Anatoliy Chakkaev — Reply to this email directly or view it on GitHub.

1602 commented 12 years ago

Right now all validations are independent, as you can see, so, no chais supported. About custom validations. It doesn't work at the moment. Some coding required. And last point - I only get one 'BOOM':

var j = require('jugglingdb');

var s = new j.Schema('redis', {}); var User = s.define('User', {email: String});

j.Validatable.haha = function () { console.log(this._validations); };

User.validatesUniquenessOf('email'); User.validateAsync('email', function(err, done) { done(); });

var u = new User({ email: "rob@foo.com" });

u.isValid(function (valid) { console.log('boooom'); console.log(valid); });

On Fri, Aug 24, 2012 at 1:58 AM, Rob Bruce notifications@github.com wrote:

I'll see if I can add it that way, thanks for the pointer.

Chained validators would be say

Validate the data type, if that passes then do the next validators, if that passes do the next and so on.

Basically, if you have multiple validators on the same field, have the option to stop the others from running if it fails further up the chain.

Code to reproduce would be something like,

User.validatesUniquenessOf('email'); User.validateAsync('email', function(err, done) { done(); });

var u = new User({ email: "rob@foo.com" });

u.isValid(function(valid) { console.log('boooom'); });

Sorry on my phone and had some beers so maybe not be quite right, but hope you get what I mean!!

Sent from my ZX81

On 23 Aug 2012, at 22:37, Anatoliy Chakkaev notifications@github.com wrote:

Ah, yeah I see what you mean. It's nice feature, I agree. Probably it should be modified in Validatable:

require('jugglingdb').Validatable.validatesTypeOf = function () { ... };

I will think about the best API implementation.

I don't get point about chain validations.

About sync/async validations, I believe callback should not called twice. Thanks for spotting, I'll try to reproduce. Unit test case are welcome as well.

On Thu, Aug 23, 2012 at 1:16 PM, Rob Bruce notifications@github.com wrote:

Yeah that's what I have been doing, but for instance I wanted to have a function such as

validatesTypeOf

It would be something I'd use again and again, so it would be good to add my own validators.

Is it possible to chain validators too?

On a side note, if you add an async custom function which doesn't function asyncronously, it causes the callback passed into isValid to be called syncronously, if you have any other async calls, such as validatesUniquenessOf then the callback is called twice

In my code I had an email validator, where I was using the node dns functions to check the MX records. Before this was done I was checking the the email field was a string and matched a regex, as there was no point in doing the dns check if that failed. If I returned early by a string fail or regex failed, I got the odd behavior; I got around it by wrapping in a process.nextTick.

Not saying it's a bug in your code, it was something in mine, but it maybe worth adding to the documenation that done() needs to be called inside an async function, so process.nextTick is needed if the function is not.

Thanks for the fantastic module though, it is really helping me with my project :)

Sent from my ZX81

On 23 Aug 2012, at 08:12, Anatoliy Chakkaev notifications@github.com

wrote:

You can do it using validate and validateAsync methods.

— Reply to this email directly or view it on GitHub< https://github.com/1602/jugglingdb/issues/112#issuecomment-7961781>.

— Reply to this email directly or view it on GitHub< https://github.com/1602/jugglingdb/issues/112#issuecomment-7964121>.

Thanks, Anatoliy Chakkaev — Reply to this email directly or view it on GitHub.

— Reply to this email directly or view it on GitHubhttps://github.com/1602/jugglingdb/issues/112#issuecomment-7985671.

Thanks, Anatoliy Chakkaev

anatoliychakkaev commented 12 years ago

This fix allows to add custom validation as I suggested before:

diff --git a/lib/abstract-class.js b/lib/abstract-class.js
index aa958c5..4b7361b 100644
--- a/lib/abstract-class.js
+++ b/lib/abstract-class.js
@@ -9,7 +9,8 @@ var DEFAULT_CACHE_LIMIT = 1000;

 exports.AbstractClass = AbstractClass;

-jutil.inherits(AbstractClass, Validatable);
+AbstractClass.__proto__ = Validatable;
+AbstractClass.prototype.__proto__ = Validatable.prototype;
 jutil.inherits(AbstractClass, Hookable);