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

ilabs-joi-to-swagger

v1.3.0

Published

joi-to-swagger ==============

Downloads

4,316

Readme

joi-to-swagger

Greenkeeper badge

Conversion library for transforming Joi schema objects into Swagger schema definitions.

// input
joi.object().keys({
  id:      joi.number().integer().positive().required(),
  name:    joi.string(),
  email:   joi.string().email().required(),
  created: joi.date().allow(null),
  active:  joi.boolean().default(true),
})
// output
{
  "type": "object",
  "required": ["id", "email"],
  "properties": {
    "id": {
      "type": "integer",
      "minimum": 1
    },
    "name": {
      "type": "string"
    },
    "email": {
      "type": "string",
      "format": "email"
    },
    "created": {
      "type": [
        "string",
        "null"
      ],
      "format": "date-time"
    },
    "active": {
      "type": "boolean"
    }
  }
}

Usage

var j2s = require('joi-to-swagger');

var {swagger, definitions} = j2s(mySchema, existingDefinitions);

J2S takes two arguments, the first being the Joi object you wish to convert. The second optional argument is a collection of existing definitions to reference against for the meta className identifiers (see below).

J2S returns a result object containing swagger and definitions properties. swagger contains your new schema, definitions contains any definitions that were generated while parsing your schema.

Supported Conventions:

  • joi.object()

    • .unknown(false) -> additionalProperties: false
    • .required() on object members produces a "required": [] array
  • joi.array().items() defines the structure using the first schema provided on .items() (see below for how to override)

    • .min(4) -> "minItems": 4
    • .max(10) -> "maxItems": 10
    • .unique(truthy) -> "uniqueItems": true
  • joi.number() produces "type": "number" with a format of "float"

    • .precision() -> "format": "double"
    • .integer() -> "type": "integer"
    • .strict().only(1, 2, '3') -> "enum": [1, 2] (note that non-numbers are omitted due to swagger type constraints)
    • .allow(null) -> "type": ["number", "null"]
    • .min(5) -> "minimum": 5
    • .max(10) -> "maximum": 10
    • .positive() -> "minimum": 1
    • .negative() -> "maximum": -1
  • joi.string() produces "type": "string" with no formatting

    • .strict().only('A', 'B', 1) -> "enum": ["A", "B"] (note that non-strings are omitted due to swagger type constraints)
    • .alphanum() -> "pattern": "/^[a-zA-Z0-9]*$/"
    • .alphanum().lowercase()
    • .alphanum().uppercase()
    • .token() -> "pattern": "/^[a-zA-Z0-9_]*$/"
    • .token().lowercase()
    • .token().uppercase()
    • .email() -> "format": "email"
    • .isoDate() -> "format": "date-time"
    • .regex(/foo/) -> "pattern": "/foo/"
    • .allow(null) -> "type": ["string", "null"]
    • .min(5) -> "minLength": 5
    • .max(10) -> "maxLength": 10
  • joi.binary() produces "type": "string" with a format of "binary".

    • .encoding('base64') -> "format": "byte"
    • .min(5) -> "minLength": 5
    • .max(10) -> "maxLength": 10
    • .allow(null) -> "type": ["string", "null"]
  • joi.date() produces "type": "string" with a format of "date-time".

    • .allow(null) -> "type": ["string", "null"]
  • joi.alternatives() defines the structure using the first schema provided on .items() (see below for how to override)

  • any.default() sets the "default" detail.

Meta Overrides

The following may be provided on a joi .meta() object to explicitly override default joi-to-schema behavior.

className: By default J2S will be full verbose in its definitions. If an object has a className string, J2S will look for an existing definition with that name, and if a definition does not exist then it will create one. Either way, it will produce a $ref element for that schema definition. If a new definition is created it will be returned with the swagger schema.

swaggerIndex: Swagger's deterministic design disallows for supporting multiple type definitions. Because of this, only a single schema from .alternatives() and .array().items() may be converted to swagger. By default J2S will use the first definition. Defining a different zero based index for this meta tag will override that behavior.

swagger: To explicitly define your own swagger definition for a joi schema object, place that swagger object in the swagger meta tag. It will be mixed in to the schema that J2S produces.

swaggerOverride: If this meta tag is truthy, the swagger definition will replace the result for that schema instead of mixing in to it.