0.1.9 • Published 9 years ago

jsdoc-to-json-schema v0.1.9

Weekly downloads
7
License
ISC
Repository
github
Last release
9 years ago

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.

TagType
@schema.namestring
@schema.descriptionstring
@schema.extendsstring
@schema.idstring
@schema.typestring
@schema.patternstring
@schema.titlestring
@schema.formatstring
@schema.disallowstring
@schema.enumarray
@schema.minimuminteger
@schema.maximuminteger
@schema.minItemsinteger
@schema.maxItemsinteger
@schema.minLengthinteger
@schema.maxLengthinteger
@schema.exclusiveMinimuminteger
@schema.exclusiveMaximuminteger
@schema.divisibleByinteger
@schema.requiredboolean
@schema.uniqueItemsboolean
@schema.defaultany

Running tests

Install dev dependencies and run tests:

$ npm install -d && npm test

License

Licensed under ISC License © John Doherty

0.1.9

9 years ago

0.1.8

9 years ago

0.1.7

9 years ago

0.1.6

9 years ago

0.1.5

9 years ago

0.1.4

9 years ago

0.1.3

9 years ago

0.1.2

9 years ago

0.1.1

9 years ago

0.1.0

9 years ago

0.0.9

10 years ago

0.0.8

10 years ago

0.0.7

10 years ago

0.0.6

10 years ago

0.0.5

10 years ago

0.0.4

10 years ago

0.0.2

10 years ago

0.0.1

10 years ago