Comments (12)
This would definitely be useful. I'll have to think about this for a little. I would like to keep the synchronous API the same to ensure backwards compatibility. Ideally, in the event that 1 or more async rules are passed into new Validator()
, then you could pass in callbacks to passes()
and fails()
like you mentioned above which would get invoked when all asynchronous requests have completed. The implementation also needs to be promise library agnostic since this library can be used in the node environment.
What do you think of the following API?
Validator.registerAsync('username_available', function(value, requirement, attribute) {
return function(passes, fails) {
return $.ajax({
url: '/some-user-lookup'
}).then(function() {
passes();
}, function() {
fails();
});
};
}, 'The :attribute is not available.');
from validatorjs.
That looks awesome mate. Just a few things to consider:
Having two different ways of determining passing/failing might catch a few people out if they don't realise that some of the rules they're testing against is async. One way around this could be to just detect if the validator has async rules .hasAsyncRules()
and if so, check to see if a callback has been passed to passes
and fails
and throw
an uber short warning message validator is async, <method:passes/fails> needs a callback
.
Also if a callback has been supplied to passes()
or fails()
it should ALWAYS work and invoke the callback, if even no async rules. Personally I'll always use the validator that way, as it is the best of both worlds without any gotchas.
For the error messages in async, ideally there should be a nice way to supply a custom error message. For instance, my username_available
async rule checks a few things and will return the error message (400 Bad Request) Username has already been taken
or Username is reserved
, else returns 200 if all ok. Something like that below would be nice and simple. If you supply a custom error message when calling the fails
callback it will use that one, if not it will default to the one supplied in the registerAsync
.
Validator.registerAsync('username_available', function(value, requirement, attribute) {
return function(passes, fails) {
return $.ajax({
url: '/some-user-lookup'
}).then(function() {
passes();
}, function(xhr) {
fails(xhr.responseJSON.error_message);
});
};
}, 'The :attribute is not available.');
from validatorjs.
@skaterdav85 anymore thoughts on this?
from validatorjs.
I tried the database querying on db but passes() gets fired before my call finishes. This causes my request to get stuck without a respond. implementing async is a must. Becuase database validation would be really nice feature in this validation plugin.
Validator.register('unique', function(value, requirement, attribute) {
var reqs = requirement.split(",");
var query = {};
query[reqs[0]] = value;
var result;
User.findOne(query).exec(function(err, docs){
result = docs;
});
return setTimeout(function () {
if(result.length>0)
{
//BULUNDU
return false;
}
else
{
return true;
}
}, 100)
}, 'email already exists.');
from validatorjs.
@Oguzhanpiskin It's on my top priority list next, will be in version 2.0
from validatorjs.
Just looking into this now. Have got a working implementation, just getting the tests all working now. Asynchronous is such a pain in jasmine, so tempted just to switch everything to mocha. Do you have any strong preference for jasmine over mocha @skaterdav85 ?
from validatorjs.
Ah ya this project still runs on Jasmine 1. I am indifferent to either but asynchronous testing in Jasmine 2 is the same as in Mocha. I think switching to Jasmine 2 might be less work if any of the tests are using Jasmine spies.
from validatorjs.
Just trying out jasmine 2, getting some weird issues where it cuts out half way through tests. Can't figure it out. I'll push what I got for async so far, would be grateful if you could take a look :)
from validatorjs.
Heh, I wrote a blog post about upgrading not too long ago. http://thejsguy.com/2015/01/29/Upgrading-Jasmine-from-1.3-to-2.1.html
from validatorjs.
Interesting! Most of the tests are pretty basic so I haven't come across any that needed changing to support jasmine 2.0 so far.... maybe I missed something? Heres the PR #53
from validatorjs.
I've switched all tests to using mocha, having much more success than Jasmine. Tests seem to be faster too which is nice.
from validatorjs.
great! thanks!
from validatorjs.
Related Issues (20)
- My email address is in your security policy HOT 2
- Custom replacements not working for custom rules HOT 1
- Wildcard object validation supported? HOT 1
- Validator.passes() throws an exception when using in AWS Lambda deployed with AWS CDK HOT 13
- An error with a valid regexp HOT 1
- Local Server URL Marked as Invalid HOT 2
- Other validations are being ignored when value is null HOT 3
- Accept only keys defined in rule object HOT 1
- Accessing request object inside register() registerAsync() HOT 1
- can't create custom message by mask .*. HOT 1
- [Unhandled promise rejection: TypeError: undefined is not an object (evaluating 'messages.def')] HOT 2
- TypeError: Cannot read properties of undefined (reading 'def') HOT 1
- Vite + React - leads to undefined error HOT 5
- Validator.registerAsync() argument message should be optional
- Validator.registerAsync() callback documentation
- Ability to register a rule per validator instance
- Is project still maintained HOT 6
- Validator.register: TypeError: Cannot set properties of undefined HOT 5
- Required_if not working with numbers
- required_without accepts empty arrays but laravel doesn't
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 validatorjs.