GithubHelp home page GithubHelp logo

Comments (12)

iamdtang avatar iamdtang commented on May 22, 2024

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.

garygreen avatar garygreen commented on May 22, 2024

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.

garygreen avatar garygreen commented on May 22, 2024

@skaterdav85 anymore thoughts on this?

from validatorjs.

tifidy avatar tifidy commented on May 22, 2024

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.

garygreen avatar garygreen commented on May 22, 2024

@Oguzhanpiskin It's on my top priority list next, will be in version 2.0

from validatorjs.

garygreen avatar garygreen commented on May 22, 2024

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.

iamdtang avatar iamdtang commented on May 22, 2024

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.

garygreen avatar garygreen commented on May 22, 2024

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.

iamdtang avatar iamdtang commented on May 22, 2024

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.

garygreen avatar garygreen commented on May 22, 2024

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.

garygreen avatar garygreen commented on May 22, 2024

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.

iamdtang avatar iamdtang commented on May 22, 2024

great! thanks!

from validatorjs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.