-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
added support for promises #636
Conversation
Sorry but I don't think I'll merge that. It's easy enough to promisify in about 5 lines of code at most, and I don't want to add another dependency while there's an effort to limit those to allow browserification. |
You can even Promisify in 1 line (in case someone needs it). |
If you want to use bluebird. ;) |
Marsup, thanks for considering it. I'll refactor it into an npm module. Regarding browserify-isation... when ES6 drops in the browsers, you might reconsider since it defaults to using global.Promise. |
Currently there's no need at all, joi is fully synchronous whatever API you chose, I'll bother when async is supported. |
@Marsup Is this still true? I'm a new Joi user and the fact that |
Still true yes, the callback is just to be compatible with hapi's custom validation method. |
Apologies for still being confused. Are you saying that I can just use joi synchronously, like |
Yes Joi can be used sync or async |
@simon-p-r @tjwebb Don't confuse accepts a callback parameter as being the same as asyncronous. Both of the following are synchronous: Joi.validate({ a: 'a string' }, schema, (err, value) => {
...
});
const result = Joi.validate({ a: 'a string' }, schema); The first example accepts a callback, but that callback is called before the next line of code executes, there's no async behaviour yet in Joi. |
Ah yes, sorry. I wasn't precise. But I understand the API better now, On Fri, Mar 25, 2016 at 6:43 PM, Matt Harrison notifications@github.com
|
just to chime in on the "synchronous" callback. Its an anti-pattern! It's not others fault for not understanding that. Here's a good article on it: http://markdawson.tumblr.com/post/43705616205/honour-thy-async-signature |
@simoami Late reply but you're wrong in your understanding of this article. What matters is consistency, you mustn't have callbacks called synchronously and asynchronously from the same function, and this is never happening in joi. I know well about this topic and it will change when joi supports asynchronous operations, but until then I'm fine with our current solution. |
Could we update the README validation examples to be synchronous? It's only now, reading this, that I realized that validation could be synchronous. I think most people would want to use validation without a callback so the examples are confusing. |
Always open to documentation improvements. I hope the line will be less blurry whenever I manage to finish async validations though. |
Most people wouldn't want to do validation with callbacks as that adds unnecessary complexity to program flow. See hapijs#636 (comment)
Most people wouldn't want to do validation with callbacks as that adds unnecessary complexity to program flow. See hapijs#636 (comment)
I'm just curious, @Marsup are async validations actually on the roadmap for some future release? If so, is there a tracking issue? Thanks! |
Without cluttering up the README.md and keeping it as minimal as possible, added clarification that the callback is executed synchronously. This matches what is included in API.md and makes it clearer to new users without having to dig through the API docs or archived issues (hapijs#636) that there is no performance benefit of passing in a callback to validate, as typical node users would expect when such a callback option is provided.
(docs) Improve README with clarification on callbacks (#636)
It is, no issue because I have no need for it yet. |
This thread has been automatically locked due to inactivity. Please open a new issue for related bugs or questions following the new issue template instructions. |
Extends
Joi.validate()
to return an ES6-compliant "resolve" or "reject" Promise,configureable via options.
If
options.asPromise
istrue
, joi.validate() returnsPromise.resolve(value)
on success and
Promise.reject(errors)
on failure.If optional
options.withPromises
is set to an ES6 Promise Specification compliantPromise function, it will be used instead of the default Promise implementation,
(which is native Promise on ES6 platforms or require('es6-promise').polyfill()).
Supports any ES6 compliant Promise implementation that provides
Promise.resolve()
and
Promise.reject()
.Tests provided for:
README.md updated with usage, working examples, and links to tested Promise modules
and pertinent specs.