peerigon / alamid

Framework for RESTful JavaScript web applications that run both on the server- and clientside.
http://www.alamidjs.com
MIT License
23 stars 3 forks source link

Handle validators response via args.length #126

Open meaku opened 12 years ago

meaku commented 12 years ago

It's a pain in the ass if you are waiting for a validator-function which never returns. I'll add a timeout which can be disabled or set the way mocha handles it.

If a validator times out, it will return "timeout" as an error message for the field and the whole validation will fail.

meaku commented 11 years ago

I'm not sure about timeouts anymore. It's hard to set a fixed limit for validators. We could do it like mocha and provide setters for the timeout in the validator-function. But i think it's not the best idea.

The problem is had was caused by a validator which returned undefined but was a sync-validator. So alamid was waiting for the callback, as it is expecting functions to be async if undefined is returned. (that's why this issue exists)

We could prevent this by counting the passed arguments, as validators have a fixed amount of arguments.

validate : function(fieldName, callback)

I think that's the right solution for now.

What do you think @jhnns ?

jhnns commented 11 years ago

If it's fixed, we may rely on the length-attribute. But it would be cool if alamid would be consistent in all these cases. So that alamid always checks for the length-attribute.

meaku commented 11 years ago

We should use arg-length checking whenever possible. The "undefined" way of sync-async handling will only be applied if the length of arguments is not fixed.

jhnns commented 11 years ago

Why did the validator return undefined? Using the return value seems to me a bit less error-prone.

meaku commented 11 years ago

There was a bug in my code causing the validator to return undefined instead of a boolean. In this case nothing happens anymore, because alamid is waiting for the callback. Stupid to debug.

I think using argument length in less error prone-prone. If you want the validator async you will have a callback argument anyway. If you return undefined in this case, alamid would treat it as "undefined" error code and return.

jhnns commented 11 years ago

Ok. So do we still need a timeout?

meaku commented 11 years ago

No timeout. Just different handling of the response in order to check if sync or async. I'll also have to add a test for slightly delayed async responses. I had a bug which passed the tests.

jhnns commented 11 years ago

What's the current state of it?