README
jsonschema-builder
Command line tool to generate JSON-schemas by the Mongoose model definition
The tool puts to standard output stream the json schema or an array of such schemas.
It supports two formats for resulting schemas:
- javascript (produced by
console.dir(...)
) - json (produced by
JSON.stringify()
)
Contents
Installation
npm i -g jsonschema-builder
Usage
Getting options:
jsonschema-builder --help
Output:
Usage: jsonschema-builder [options] <pathToModels>
Options:
-h, --help output usage information
-V, --version output the version number
-d, --dir Process <pathToModels> as a directory
-r, --recursive Process directory recursively
-j, --json JSON format for output
-n, --noid Removes id field from resulting schema
-o, --object-def Output as json-schema definitions object
-i, --id [style] Style for json-schema identifiers
--no-color Prevents colorized output
Samples
Guessing you have some module with a model definition:
'use strict';
// models.js
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const BookSchema = new Schema({
title: {type: String, required: true},
year: Number,
author: {type: Schema.Types.ObjectId, required: true, ref: 'Person'}
});
const PersonSchema = new Schema({
firstName: {type: String, required: true},
lastName: {type: String, required: true},
dateOfBirth: Date
});
module.exports = exports = {
Book: mongoose.model('Book', BookSchema),
Person: mongoose.model('Person', PersonSchema)
};
Building json schema in javascript-object format:
jsonschema-builder ./models
Output:
[ { id: '#book',
title: 'Book',
type: 'object',
properties:
{ title: { type: 'string' },
year: { type: 'number' },
author:
{ type: 'string',
'x-ref': 'Person',
description: 'Refers to Person',
pattern: '^[0-9a-fA-F]{24}