express-json-schema

Adds express method to allow automatic generation of JSON schemas from a JS/JSON files containing @schema tags

Usage no npm install needed!

<script type="module">
  import expressJsonSchema from 'https://cdn.skypack.dev/express-json-schema';
</script>

README

express-json-schema

npm Linked In Twitter Follow

Adds express res.jsonSchema method to allow automatic generation of JSON schemas from a JS/JSON files containing @schema @schema tags

Installation

npm install --save express-json-schema

Usage

var app = require('express')(),
    expJsonSchema = require('express-json-schema');

// add as middleware
app.use(expJsonSchema);

// use within route handler
app.get('/', function(req, res){

    // return json schema for person.js file using new res.jsonSchema method
    res.jsonSchema('./examples/person.js');
});

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

Example person.js

/**
 * @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: '',

    /**
     * @schema.type object
     */
    address: {
        
    }
};

Example response

{
    "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"
        }
    }
}

Supported tags

A list of supported tags can be viewed at jsdoc-to-json-schema

License

ISC License © 2016 John Doherty