Research
Security News
Malicious npm Package Targets Solana Developers and Hijacks Funds
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
jsonschema-builder
Advanced tools
Command line tool for mongoose-schema-jsonschema
The tool puts to standard output stream the json schema or an array of such schemas.
It supports two formats for resulting schemas:
console.dir(...)
)JSON.stringify()
)npm i -g jsonschema-builder
Getting options:
jsonschema-builder --help
Output:
Usage: jsonschema-builder [options] <pathToModels>
Options:
-h, --help output usage information
-V, --version output the version number
-j, --json JSON format for output
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',
format: 'uuid',
pattern: '^[0-9a-fA-F]{24}$' },
_id: { type: 'string', format: 'uuid', 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', format: 'uuid', 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",
"format": "uuid",
"pattern": "^[0-9a-fA-F]{24}$"
},
"_id": {
"type": "string",
"format": "uuid",
"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",
"format": "uuid",
"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
module.exports = exports = require('./models').Book;
Building schema for single model:
jsonschema-builder -j ./book > book.json
Opening book.json
:
{
"title": "Book",
"type": "object",
"properties": {
"title": {
"type": "string"
},
"year": {
"type": "number"
},
"author": {
"type": "string",
"x-ref": "Person",
"description": "Refers to Person",
"format": "uuid",
"pattern": "^[0-9a-fA-F]{24}$"
},
"_id": {
"type": "string",
"format": "uuid",
"pattern": "^[0-9a-fA-F]{24}$"
},
"__v": {
"type": "number"
}
},
"required": [
"title",
"author"
]
}
Array of models:
'use strict';
//list.js
const models = require('./models');
module.exports = exports = [
models.Book, models.Person
];
Building schema for single model:
jsonschema-builder -j ./list > models.json
Opening models.json
:
[
{
"title": "Book",
"type": "object",
"properties": {
"title": {
"type": "string"
},
"year": {
"type": "number"
},
"author": {
"type": "string",
"x-ref": "Person",
"description": "Refers to Person",
"format": "uuid",
"pattern": "^[0-9a-fA-F]{24}$"
},
"_id": {
"type": "string",
"format": "uuid",
"pattern": "^[0-9a-fA-F]{24}$"
},
"__v": {
"type": "number"
}
},
"required": [
"title",
"author"
]
},
{
"title": "Person",
"type": "object",
"properties": {
"firstName": {
"type": "string"
},
"lastName": {
"type": "string"
},
"dateOfBirth": {
"type": "string",
"format": "date-time"
},
"_id": {
"type": "string",
"format": "uuid",
"pattern": "^[0-9a-fA-F]{24}$"
},
"__v": {
"type": "number"
}
},
"required": [
"firstName",
"lastName"
]
}
]
Please note that the result for array of models is different from result for first case when models are exported by object.
In the first case (exports as object) the tool adds the id
field to the
resulting json schema. Currently there is no way to avoid this behavior.
The tool is just a command line interface for node-module mongoose-schema-jsonschema
.
So you could to import mentioned above module to your project and use it directly.
npm install mongoose-schema-jsonschema
More details by the link mongoose-schema-jsonschema
FAQs
Command line tool to generate JSON-schemas by the Mongoose model definition
We found that jsonschema-builder demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.