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

joistick

v1.0.1

Published

Helpers for Joi

Downloads

72

Readme

Joistick

Default values for joi schemas.

Build Status

Install

$ npm install joistick

Usage

Purpose

Joi's default value API (any.default([value, [description]])) supports being passed a function that, when called, returns the default value.

If you want the default value to be an Object, Array or other complex data type, you need a function that returns a new instance. If you just pass a value literal it would be shared between calls, usually we don't want that.

Joistick helps reduce boilerplate in your shemas.

Importing

Access features as properties of the whole module:

const Joi = require('joi');
const Joistick = require('joistick');

const arrayFactory = Joistick.NewArray();
const dateFactory = Joistick.NewDate();
const objectFactory = Joistick.NewObject();

Or import a feature directly:

const Joi = require('joi');
const NewArray = require('joistick/new-array');
const NewDate = require('joistick/new-date');
const NewObject = require('joistick/new-object');

const arrayFactory = NewArray();
const dateFactory = NewDate();
const objectFactory = NewObject();

API

NewArray([default])

Returns a function that returns a new Array where:

  • value - An optional array of cloneable values to seed a new array with.
const Joi = require('joi');
const NewArray = require('joistick/new-array');

// An empty Array
const schema = {
    job: Joi.string().required(),
    actions: Joi.array().default(NewArray(), 'log of actions')
};

// An Array with default items
const schema2 = {
    job: Joi.string().required(),
    actions: Joi.array().default(NewArray(['setup', 'step1']), 'log of actions')
};

NewDate([default])

Returns a function that returns a new Date where:

  • default - An optional value that is passed to the Date constructor.
const Joi = require('joi');
const NewDate = require('joistick/new-date');

// A fresh Date (now)
const schema = {
    name: Joi.string().required(),
    created: Joi.date().default(NewDate(), 'time of creation')
};

// A specificed Date
const schema2 = {
    name: Joi.string().required(),
    created: Joi.date().default(NewDate('1982-01-01T00:42:00'), 'time of creation')
};

NewObject([default])

Returns a function that returns a new Object where:

  • value - An optional Object of cloneable values to seed a new object with. Uses Object.assign to create a new object when seed is defined.
const Joi = require('joi');
const NewObject = require('joistick/new-object');

// An empty object
const schema = {
    uri: Joi.string().required(),
    headers: Joi.object().default(NewObject(), 'map of headers')
};

// An object with default fields
const schema = {
    uri: Joi.string().required(),
    headers: Joi.object().default(NewObject({ status: 200 }), 'map of headers')
};

Have a question?

Any issues or questions (no matter how basic), open an issue. Please take the initiative to read relevant documentation and be pro-active with debugging.

Want to contribute?

Contributions are welcome. If you're changing something non-trivial, you may want to submit an issue before creating a large pull request.

License

MIT