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
Samples
Guessing you have some module with a model definition:
'use strict';// models.js const mongoose = require; const Schema = mongoose.Schema; const BookSchema = new Schema; const PersonSchema = new Schema; module.exports = exports = ;
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}$' _id: type: 'string' pattern: '^[0-9a-fA-F]{24}$' __v: type: 'number' required: 'title' 'author' id: '#person' title: 'Person' type: 'object' properties: firstName: type: 'string' lastName: type: 'string' dateOfBirth: type: 'string' format: 'date-time' _id: type: 'string' pattern: '^[0-9a-fA-F]{24}$' __v: type: 'number' required: 'firstName' 'lastName'
The same case with JSON formatted output
jsonschema-builder --json ./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}$" "_id": "type": "string" "pattern": "^[0-9a-fA-F]{24}$" "__v": "type": "number" "required": "title" "author" "id": "#person" "title": "Person" "type": "object" "properties": "firstName": "type": "string" "lastName": "type": "string" "dateOfBirth": "type": "string" "format": "date-time" "_id": "type": "string" "pattern": "^[0-9a-fA-F]{24}$" "__v": "type": "number" "required": "firstName" "lastName"
The module is passing to the tool could also export single instance of
Mongoose.Model
or an array of such instances.
Single instance:
'use strict';//book.js moduleexports = exports = Book;
Building schema for single model:
jsonschema-builder -j ./book > book.json
Opening book.json
:
"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}$" "_id": "type": "string" "pattern": "^[0-9a-fA-F]{24}$" "__v": "type": "number" "required": "title" "author"
Array of models:
'use strict';//list.js const models = ; moduleexports = exports = modelsBook modelsPerson;
Building schema for single model:
jsonschema-builder -j ./list > models.json
Opening models.json
:
"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}$" "_id": "type": "string" "pattern": "^[0-9a-fA-F]{24}$" "__v": "type": "number" "required": "title" "author" "id": "#person" "title": "Person" "type": "object" "properties": "firstName": "type": "string" "lastName": "type": "string" "dateOfBirth": "type": "string" "format": "date-time" "_id": "type": "string" "pattern": "^[0-9a-fA-F]{24}$" "__v": "type": "number" "required": "firstName" "lastName"
Prepared samples
jsonschema-builder
project includes some sample model definitions that
located in the samples folder in the project root directory.
The structure is following:
In order to run jsonschema-builder
with prepared samples you should:
git clone https://github.com/DScheglov/jsonschema-builder.gitcd jsonschema-buildernpm installnpm link
When you finish sample running, just do unlink:
cd jsonschema-buildernpm unlink
Using the jsonschema-builder
for single model:
jsonschema-builder samples/models/book
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}$' _id: type: 'string' pattern: '^[0-9a-fA-F]{24}$' __v: type: 'number' required: 'title' 'author'
To get schema for all models located in the samples/models directory in JSON format
jsonschema-builder --dir --json samples/models
"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}$" "_id": "type": "string" "pattern": "^[0-9a-fA-F]{24}$" "__v": "type": "number" "required": "title" "author" "id": "#person" "title": "Person" "type": "object" "properties": "firstName": "type": "string" "lastName": "type": "string" "dateOfBirth": "type": "string" "format": "date-time" "_id": "type": "string" "pattern": "^[0-9a-fA-F]{24}$" "__v": "type": "number" "required": "firstName" "lastName"
Please consider the difference between result of jsonschema-builder
call
with in option --dir
and without that option:
jsonschema-builder --json samples/models
The jsonschema-builder
will search for module by path samples/models
and
without --dir
option will find samples/models.js instead of samples/models/
folder in case when --dir
option specified
Output:
"id": "#point" "title": "Point" "type": "object" "properties": "x": "type": "number" "y": "type": "number" "title": "type": "string" "_id": "type": "string" "pattern": "^[0-9a-fA-F]{24}$" "__v": "type": "number" "id": "#line" "title": "Line" "type": "object" "properties": "start": "title": "start" "type": "object" "properties": "x": "type": "number" "y": "type": "number" "title": "type": "string" "_id": "type": "string" "pattern": "^[0-9a-fA-F]{24}$" "__v": "type": "number" "end": "title": "end" "type": "object" "properties": "x": "type": "number" "y": "type": "number" "title": "type": "string" "_id": "type": "string" "pattern": "^[0-9a-fA-F]{24}$" "__v": "type": "number" "title": "type": "string" "_id": "type": "string" "pattern": "^[0-9a-fA-F]{24}$" "__v": "type": "number"
Schema id
Please note that jsonschema-builder
adds the id
field to each schema. To
avoid this behavior use --noid
option.
You could specify the style of id
field. Currently supported the following
options:
dash
(default) - createsid
frommodel.name
in dash-style (model-name
)underline
- entails the dash_style ofid
(model_name
)camel
- removes all underlines and dashes form themodel.name
and returns camelStyleid
(modelName)lower
- transforms all letters inmodel.name
in the lower case analogs (modelname)
API
You can use jsonschema-builder
inside of your code:
'use strict'; const jsonSchemaBuilder = ; let schemas =
The result will be the same as you call jsonschema-builder
via command-line:
jsonschema-builder -jrd samples
jsonSchemaBuilder
Builds the json schema based on the Mongooose model definition specified by the path to appropriate module
Declaration:
{ ... }
Parameters:
- pathToModels:
String
- the path to model definition(s) - options:
Object
- the options for schema creation- noid:
Boolean
- preventsid
- json:
Boolean
- entails the JSON-formated result - id:
String
- defines theid
field format - dir:
Boolean
- enforces the function to considerpathToModels
as a directory - recursive:
Boolean
- enforces to find all models inpathToModels
and in all its sub-dirctories
- noid:
- Returns
Object
|String
- json schema
Methods:
extendMongoose
Extends the mongoose.Model
and mongoose.Schema
with method jsonSchema
and
returns Mongoose
instance
Declaration:
{ ... }
In case you need to create json schema for mongoose.Model
or mongoose.Schema
instance you should use mongoose-schema-jsonschema
module that is the core
of jsonschema-builder
.
You can import core-module directly by its name or call extendMongoose
method of jsonSchemaBuilder
object;
'use strict'; const jsonSchemaBuilder = ;const mongoose = jsonSchemaBuilder; const Book = ; consoledirBook depth: null;
Output:
More details by the link mongoose-schema-jsonschema
Contribution
Please register issues you met in the github-repository: https://github.com/DScheglov/jsonschema-builder/issues
Also I'll be thankful for code review.