npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

overjoied

v0.1.0

Published

Joi for function signatures

Downloads

3

Readme

README

If we're using Joi all over the place for doing validations of our objects, then why not instrument the functions on these objects with Joi validations as well.

This stuff is experimental. Don't use this for production.

Why

Before anything else, Overjoied is just Joi, with some extras. The extra bit is has allows you to express additional expectations on the types of the function's arguments.

var Joi = require('overjoied');

var schema = Joi.object().keys({
	"increaseBy": Joi.func().params(Joi.number())
});

In the schema above, the "increaseBy":Joi.func() expresses we're expecting the object to have an increaseBy(…) method. The bit that is new is the .params(Joi.number()) bit. It specializes our expectation by stating that we expect the first parameter to be a number.

Now, with JavaScript's dynamic nature, we obviously cannot check up front if an object with an increaseBy(…) function accepts a single numeric parameter. However, when the method is invoked, we can check if the first argument getting passed in is numeric.

And that's exactly what Overjoied is doing: by adding the params(…) method, you can guarantee that the object getting returned by the Joi.validate(…) method has an increaseBy(…) method that checks if the first argument is numeric.

So, what is it?

In summary: Overjoied is just like Joi, with one extra feature: if params() is called on a func(), then Overjoied will cause the validated object to have assertions for all the parameter specifications you passed in.

So if this is your schema:

var Joi = require('overjoied');

var schema = Joi.object().keys({
	"increaseBy": Joi.func().params(Joi.number())
});

And if this the way you use it:

var obj = …;
Joi.validate(obj, schema, function(err, validated) {
	if (!err) {
		// Then this will be okay
		validated.increaseBy(12);
		// And this will throw a detailed error message,
		// stating that it expected a number.
		validated.increaseBy('aa'); // will throw an Error				
	}
});

The error will be fairly specific. Not as specific as I want it to be just yet – it would have been nice if I'd use information from the function signatures – but absolutely readable.