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

joiify

v0.0.5

Published

Easy to read and fast to write Joi schemas

Downloads

10

Readme

joiify

Fast to write easy to read validation schemas, e.g.

{
  a: 'string',
  b: [],
  c: {
    d: 'integer',
    e: 'date'
  }
}

Joiify will convert the object above to a Joi schema. Why?

  • Semantically intuitive
  • Fast to write and easy to read
  • Portable - Joiify simply takes in a vanilla js/json object
  • Compact, especially if written inline e.g. {a: 'string', b: []}
  • Full power of Joi (see note below)

Note: Joiify will treat Joi objects as pass through so you can safely mix and match Joiify schemes with Joi schemas and do not have to worry about loss of functionality. E.G. Joiify(Joi.string()) compiles to Joi.string()

Note2: Portability is a big deal. You can trivially send a Joiify schema over http as JSON. However this will only work if you do not have any Joi objects in your scheme (re: note above).

Note3: Joiify does not cache the schema result, for optimal performance you should Joiify your scheme before validation time as you would with any Joi schema.

Usage

var Joiify = require('joiify')
var AccountSchema = Joiify({
  AccountID: 'string',
  lastLogin: 'date',
  //Any values that are already Joi objects will included verbatim
  role: Joi.any().valid(['user', 'manager', 'admin']),
  profile: {
    url: 'string',
    email: 'email',
    age: 'integer',
    //special rules for this object
    '*': {required: true, unknown: false, min: 1, max: 10}  
  })

AccountSchema.validate(data, function(err, data){
  //just a nornmal joi schema
})

//AccountSchema equivalent to:
Joi.object().keys({
  AccountID: Joi.string(),
  lastLogin: Joi.date(),
  role: Joi.any().valide(['user', 'manager', 'admin']),
  profile: Joi.object().requiredKeys(['url', 'email', 'age']).unknown(false).min(1).max(10).keys({
    url: Joi.string(),
    email: Joi.string().email(),
    age: Joi.number().integer(),
  })
})

API

Joiify accepts one argument the "scheme" which should be one of the following:

Joiify(scheme)

Convert the a joiify "scheme" into a Joi "schema" where the scheme argument is one of the following

  • string: A value which is converted to a corresponding Joi type
  • Object: An object. Object children will be recursively Joiified.
  • Array: An array. Array children will be used to validate array elements. See details below for conversion rules

Type Conversion

strings types

  • "string": Joi.string()
  • "email": Joi.string().email()
  • "hostname": Joi.string().hostname()
  • "alphanum": Joi.string().alphanum()
  • "hex": Joi.string().hex()
  • "token": Joi.string().token()

number types

  • "number": Joi.number()
  • "integer": Joi.number().integer()

other types

  • "date": Joi.date()
  • "boolean": Joi.boolean()
  • "binary": Joi.binary()
  • "func": Joi.func()
  • "array": Joi.array()
  • "object": Joi.object()
  • "forbidden": Joi.any().forbidden()

special (non strings)

  • undefined: Joi.any().forbidden()
  • null: Joi.any().valid(null)
  • []: Joi.array()
    • Array elements are added as valid types see Joi.array().includes()

objects

  • Joi Schema Object: will return the Joi schema verbatim.
  • {}: Joi.object()
    • Children will be recursively converted using the same rules above
    • '*' if set specifies special handlers for the object validation as follows
      • required: boolean, marks as required (true) or optional (false) all keys on the object
      • unknown: boolean, allows (true) or disallows (false) unknown keys on the object
      • max: integer, sets the maximum number of keys on the object
      • min: integer, sets the minimum number of keys on the object