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

jsdoc-to-json-schema

v0.1.9

Published

Generate JSON schemas from JavaScript code comments using a new jsDoc @schema tag.

Downloads

38

Readme

jsdoc-to-json-schema

Shippable branch npm Linked In Twitter Follow

Generate JSON schemas from JavaScript code comments using a new jsDoc @schema tag.

Installation

npm install --save jsdoc-to-json-schema

Example

Using this module within your node project

var toJsonSchema = require('jsdoc-to-json-schema');

// OPTION 1: generate a JSON schema for product.js and save it to disk
toJsonSchema('./example/product.js', './example/product.schema.json');

// OPTION 2: generate a JSON schema and return it as an object
toJsonSchema('./example/product.js').then(function(schema){
    // do something with it
});

// OPTION 3: generate a JSON schema, save it to disk and return it as an object
toJsonSchema('./example/product.js', './example/product.schema.json').then(function(schema){
    // do something with it
});

Using this module via the command line

# install the module globally
$ npm install -g jsdoc-to-json-schema

# execute from command line passing input and output paths
$ jsdoc-to-json-schema -i ./example/product.js -o ./example/product.schema.json

Using this module as an expressjs response

var express = require('express');
var app = express();
var toJsonSchema = require('../lib/jsdoc-to-json-schema.js');

// create an express route
app.get('/', function(req, res){

    // pipe schema directly to the response stream
    toJsonSchema('./examples/person.js', res);
});

// start the server listening on port 8080
app.listen(8080, function(){
    console.log('Example app listening on port 8080');
});

For use with express consider the dedicated express middleware project express-json-schema

Input (singleton)

JavaScript file containing jsDoc @schema tags used to define the JSON schema:

/**
 * @schema.name Person
 * @schema.description This is an example Person object marked up with JSON schema tags to allow schema generation
 */
var Person = {

    /**
     * @schema.title Name
     * @schema.description Please enter your full name
     * @schema.type string
     * @schema.maxLength 30
     * @schema.minLength 1
     * @schema.required true
     */
    name: '',

    /**
     * @schema.title Job Title
     * @schema.type string
     */
    jobTitle: '',

    /**
     * @schema.title Telephone Number
     * @schema.description Please enter telephone number including country code
     * @schema.type string
     * @schema.required true
     */
    telephone: '',

    /**
     * @schema.type string
     * @schema.required true
     */
    dateOfBirth: ''
};

Output

{
    "name": "Person",
    "description": "This is an example Person object marked up with JSON schema tags to allow schema generation",
    "properties": {
        "name": {
            "title": "Name",
            "description": "Please enter your full name",
            "type": "string",
            "maxLength": 30,
            "minLength": 1,
            "required": true
        },
        "jobTitle": {
            "title": "Job Title",
            "type": "string"
        },
        "telephone": {
            "title": "Telephone Number",
            "description": "Please enter telephone number including country code",
            "type": "string",
            "required": true
        },
        "dateOfBirth": {
            "type": "string",
            "required": true
        },
        "address": {
            "type": "object"
        }
    }
}

Input (instance)

JavaScript file containing jsDoc @schema tags used to define the JSON schema:

/**
 * @schema.name Product
 * @schema.description An example product marked up with json schema comments
 */
function Product(){

}

/**
 * @schema.type array
 * @schema.minItems 3
 * @schema.maxItems 6
 * @schema.required true
 */
Product.prototype.types = function(){

}

Output

{
    "name": "Product",
    "description": "An example product marked up with json schema comments",
    "properties": {
        "types": {
            "type": "array",
            "minItems": 3,
            "maxItems": 6,
            "required": true
        }
    }
}

Supported JSON Schema tags

The following JSON Schema v3 tags are supported, however undocumented @schema tags will also be generated in order to aid extensibility.

Note: @schema tags without an associated value will be ignored.

| Tag | Type | -------------------------- | --------- | @schema.name | string | @schema.description | string | @schema.extends | string | @schema.id | string | @schema.type | string | @schema.pattern | string | @schema.title | string | @schema.format | string | @schema.disallow | string | @schema.enum | array | @schema.minimum | integer | @schema.maximum | integer | @schema.minItems | integer | @schema.maxItems | integer | @schema.minLength | integer | @schema.maxLength | integer | @schema.exclusiveMinimum | integer | @schema.exclusiveMaximum | integer | @schema.divisibleBy | integer | @schema.required | boolean | @schema.uniqueItems | boolean | @schema.default | any

Running tests

Install dev dependencies and run tests:

$ npm install -d && npm test

License

Licensed under ISC License © John Doherty