Comments (9)
It's fairly easy to add this yourself. Try this:
var Validator = require('node-validator').Validator;
Validator.prototype.userExists = function (callback) {
User.findOne({username: this.str, function (err, user) {
callback(err, !!user)
});
}
You still need use the method asynchronously and add a callback though, so you're not really removing any..
Validator.check('[email protected]').userExists(function (err, exists) {
//
});
from validator.js.
Thanks for the tip. I wasn't looking at the source carefully. Do you think it can be made chainable, though? For now the callback version will have to work.
from validator.js.
Hmm I'll have to think about that.. There may be a way to chain callbacks so that errors are carried forward. It does raise some questions though, e.g. what if a validation relies on previous one passing? What if you need to check whether a user exists, and then check if one of their details matches?
from validator.js.
It's also worth noting (although it's generally opposed by the node.js community) that there is a way to convert these simple async methods into sync ones using fibers. I describe the technique here if you're interested.
require('synchronous')
Validator.prototype.syncGetter('userExists');
var exists = Validator.check('[email protected]').userExists();
//Throws an exception if the user doesn't exist
from validator.js.
Yeah, not a trivial problem.
from validator.js.
I'm new to the framework, so this is probably naive, but...
Would it be better to have the validation methods accept and return an independent object rather than the validator itself? Once the set-up method (#check) is adjusted to be async, that little change would allow it to handle multiple validation chains running asynchronously without creating new validation objects for each (which would inefficiently create a new set of validation methods for each object). Then we could use the set up method to break off a new validation chain to run on nextTick, allowing multiple validations to run effectively in parallel. We could set a method on the Validator prototype to handle the end of the validation chain, such as #then. We might end up with a call in our models such as validator.check('email').isEmail.then(callback).
With a validations hash (object) in a model, we could have all the validations on the object run asynchronously (though within a single validation chain it will run synchronously of course), and the change to the current API is virtually nil.
from validator.js.
@introspectif: I sort of like the way you don't have to add a callback explicitly, and have everything resolved in an error handler. I know it's very similar to callbacks, but it's nicely tucked away from the actual validation code, and that's perfect in my book. I don't want to mix error handling code with validation, and clutter the request handlers that way.
So, the example with sychornous perfectly shows the ideal situation where you could have async code in a validator function and still keep the nice callback-less chain.
Anyway, the debate about fibers vs callbacks exploded on Node mailing list, and I think this particualr case, for me, is ideal for stuff like fibers.
@chriso: What happens if I want to add more validators here:
Validator.check('[email protected]').userExists(function (err, exists) {
// HERE
});
Would I do it like this?
Validator.prototype.userExists = function (callback) {
var that = this;
User.findOne({username: this.str, function (err, user) {
callback(err, !!user, that)
});
}
from validator.js.
I want to convert userId to User, if not exists, throw an error.
Filter.prototype.toUser = function(){
User.findOne({username: this.str, function(err, user){
if(err) this.error(this.msg || 'User does not exists');
this.str = user;
}
}
but Filter and validator does not share this.error and this.msg.
I really think Filter and Validator can share error
msg
str
and combine check
modify
together.
from validator.js.
I think this is outside the scope of the library. I wrote node-validator as a simple, synchronous way of validating / sanitising / filtering strings with chainable methods. Incorporating validation beyond strings (or asynchronous validators) is a bit of a far-cry from what I originally intended and IMO not the right separation of duties that you should have in your app. Also, chainable asynchronous methods are difficult to get right and would require a significant refactor.
from validator.js.
Related Issues (20)
- Including special characters HOT 1
- Hyphen before a dot is a valid url HOT 1
- `javascript: ...` treated as a valid URL HOT 3
- `isDate` validation is broken after upgrading to validator v13.11.0 HOT 4
- isStrongPassword function is not considering \ as a Special Character
- isISO8601 doesn't support year with more than 4 digits HOT 3
- SyntaxError: Invalid regular expression HOT 1
- isEmail accepts incorrect domain. HOT 1
- isDate throws when using a non-valid string with a format HOT 1
- isPassport - Azerbaijan validation fails HOT 4
- Npm and git master out of sync HOT 1
- [Enhancement] isHash should validate algorithm name HOT 5
- Implement a new method, **isJWTExpiration**, to validate the expiration of JWTs using the "exp" claim. HOT 1
- validator.js timestamp validation support HOT 5
- Add support for Cron format validation HOT 2
- Test script does not work, breaks before running any test HOT 1
- isDate : iOS and Android return different results HOT 2
- Unable to run tests HOT 1
- Incorrect regex pattern HOT 4
- Add a new feature to validate UPI HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from validator.js.