Skip to content
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

Closed
wants to merge 1 commit into from
Closed

added support for promises #636

wants to merge 1 commit into from

Conversation

raisch
Copy link

@raisch raisch commented Apr 24, 2015

Extends Joi.validate() to return an ES6-compliant "resolve" or "reject" Promise,
configureable via options.

If options.asPromise is true, joi.validate() returns Promise.resolve(value)
on success and Promise.reject(errors) on failure.

If optional options.withPromises is set to an ES6 Promise Specification compliant
Promise 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.

@Marsup
Copy link
Collaborator

Marsup commented Apr 24, 2015

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.

@Marsup Marsup closed this Apr 24, 2015
@Marsup Marsup added the feature New functionality or improvement label Apr 24, 2015
@Marsup Marsup self-assigned this Apr 24, 2015
@DavidTPate
Copy link
Contributor

You can even Promisify in 1 line (in case someone needs it).

@raisch
Copy link
Author

raisch commented Apr 24, 2015

If you want to use bluebird. ;)

@raisch
Copy link
Author

raisch commented Apr 24, 2015

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.

@Marsup
Copy link
Collaborator

Marsup commented Apr 24, 2015

Currently there's no need at all, joi is fully synchronous whatever API you chose, I'll bother when async is supported.

@bookercodes
Copy link

@Marsup Is this still true? I'm a new Joi user and the fact that validate takes a callback had me thinking validate was asynchronous.

@Marsup
Copy link
Collaborator

Marsup commented Nov 6, 2015

Still true yes, the callback is just to be compatible with hapi's custom validation method.

@tjwebb
Copy link

tjwebb commented Mar 25, 2016

Apologies for still being confused. Are you saying that I can just use joi synchronously, like value = joi.validate(stuff, schema)? I've only seen the async version in the docs, so I've been wrapping all my stuff in Promises. It'd be great if I didn't need to do that

@simon-p-r
Copy link

Yes Joi can be used sync or async

@mtharrison
Copy link
Contributor

@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.

@tjwebb
Copy link

tjwebb commented Mar 25, 2016

Ah yes, sorry. I wasn't precise. But I understand the API better now,
thanks.

On Fri, Mar 25, 2016 at 6:43 PM, Matt Harrison notifications@github.com
wrote:

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 yet in Joi.


You are receiving this because you commented.
Reply to this email directly or view it on GitHub
#636 (comment)

@simoami
Copy link

simoami commented Jun 18, 2016

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

@Marsup Marsup mentioned this pull request Jul 12, 2016
@Marsup
Copy link
Collaborator

Marsup commented Jul 24, 2016

@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.

@KyleAMathews
Copy link
Contributor

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.

@Marsup
Copy link
Collaborator

Marsup commented Sep 21, 2016

Always open to documentation improvements. I hope the line will be less blurry whenever I manage to finish async validations though.

KyleAMathews added a commit to KyleAMathews/joi that referenced this pull request Sep 21, 2016
Most people wouldn't want to do validation with callbacks as that adds unnecessary complexity to program flow.

See hapijs#636 (comment)
KyleAMathews added a commit to KyleAMathews/joi that referenced this pull request Sep 21, 2016
Most people wouldn't want to do validation with callbacks as that adds unnecessary complexity to program flow.

See hapijs#636 (comment)
@AaronHarris
Copy link
Contributor

I'm just curious, @Marsup are async validations actually on the roadmap for some future release? If so, is there a tracking issue? Thanks!

AaronHarris added a commit to AaronHarris/joi that referenced this pull request Feb 26, 2017
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.
Marsup added a commit that referenced this pull request Mar 1, 2017
(docs) Improve README with clarification on callbacks (#636)
@Marsup
Copy link
Collaborator

Marsup commented Mar 1, 2017

It is, no issue because I have no need for it yet.

@lock
Copy link

lock bot commented Jan 9, 2020

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.

@lock lock bot locked as resolved and limited conversation to collaborators Jan 9, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature New functionality or improvement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants