Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
mongoose-schema-jsonschema
Advanced tools
Mongoose extension that allows to build json schema for mongoose models, schemas and queries
The module allows to create json schema from Mongoose schema by adding
jsonSchema
method to mongoose.Schema
, mongoose.Model
and mongoose.Query
classes
npm install mongoose-schema-jsonschema
Since v1.4.0 it is able to configure how jsonSchema()
works.
To do that package was extended with config
function.
const config = require('mongoose-schema-jsonschema/config');
config({
// ... options go here
});
Currently there are two options that affects build process:
forceRebuild: boolean
-- mongoose-schema-jsonschema caches json schemas built for mongoose schemas.
That means we cannot built updated jsonSchema after some updates were made in the mongoose schema
that already has jsonSchema.
To resolve this issue the forceRebuild
was added (see sample bellow)
fieldOptionsMapping: { [key: string]: string } | Array<string|[string, string]>
- allows to specify how to convert some custom options specified in the mongoose field definition.
const mongoose = require('mongoose-schema-jsonschema')();
const config = require('mongoose-schema-jsonschema/config');
const { Schema } = mongoose;
const BookSchema = new Schema({
title: { type: String, required: true, notes: 'Book Title' },
year: Number,
author: { type: String, required: true },
});
const fieldOptionsMapping = {
notes: 'x-notes',
};
config({ fieldOptionsMapping });
console.dir(BookSchema.jsonSchema(), { depth: null });
config({ fieldOptionsMapping: [], forceRebuild: true }); // reset
console.dir(BookSchema.jsonSchema(), { depth: null });
Output:
{
type: 'object',
properties: {
title: { type: 'string', 'x-notes': 'Book Title' },
year: { type: 'number' },
author: { type: 'string' },
_id: { type: 'string', pattern: '^[0-9a-fA-F]{24}$' }
},
required: [ 'title', 'author' ]
}
{
type: 'object',
properties: {
title: { type: 'string' },
year: { type: 'number' },
author: { type: 'string' },
_id: { type: 'string', pattern: '^[0-9a-fA-F]{24}$' }
},
required: [ 'title', 'author' ]
}
Let's build json schema from simple mongoose schema
const mongoose = require('mongoose');
require('mongoose-schema-jsonschema')(mongoose);
const Schema = mongoose.Schema;
const BookSchema = new Schema({
title: { type: String, required: true },
year: Number,
author: { type: String, required: true },
});
const jsonSchema = BookSchema.jsonSchema();
console.dir(jsonSchema, { depth: null });
Output:
{
type: 'object',
properties: {
title: { type: 'string' },
year: { type: 'number' },
author: { type: 'string' },
_id: { type: 'string', pattern: '^[0-9a-fA-F]{24}$' }
},
required: [ 'title', 'author' ]
}
The mongoose.Model.jsonSchema method allows to build json schema considering the field selection and population
const mongoose = require('mongoose');
require('mongoose-schema-jsonschema')(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
});
const Book = mongoose.model('Book', BookSchema);
const Person = mongoose.model('Person', PersonSchema)
console.dir(Book.jsonSchema('title year'), { depth: null });
console.dir(Book.jsonSchema('', 'author'), { depth: null });
Output:
{
title: 'Book',
type: 'object',
properties: {
title: { type: 'string' },
year: { type: 'number' },
_id: { type: 'string', pattern: '^[0-9a-fA-F]{24}$' }
}
}
{
title: 'Book',
type: 'object',
properties: {
title: { type: 'string' },
year: { type: 'number' },
author: {
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' ],
'x-ref': 'Person',
description: 'Refers to Person'
},
_id: { type: 'string', pattern: '^[0-9a-fA-F]{24}$' },
__v: { type: 'number' }
},
required: [ 'title', 'author' ]
}
const mongoose = require('mongoose');
const extendMongoose = require('mongoose-schema-jsonschema');
extendMongoose(mongoose);
const { Schema } = mongoose;
const BookSchema = new Schema({
title: { type: String, required: true },
year: Number,
author: { type: Schema.Types.ObjectId, required: true, ref: 'Person' }
});
const Book = mongoose.model('Book', BookSchema);
const Q = Book.find().select('title').limit(5);
console.dir(Q.jsonSchema(), { depth: null });
Output:
{
title: 'List of books',
type: 'array',
items: {
type: 'object',
properties: {
title: { type: 'string' },
_id: { type: 'string', pattern: '^[0-9a-fA-F]{24}$' }
}
},
maxItems: 5
}
Created by mongoose-schema-jsonschema json-schema's could be used for document validation with:
Builds the json schema based on the Mongoose schema. if schema has been already built the method returns new deep copy
Method considers the schema.options.toJSON.virtuals
to included
the virtual paths (without detailed description)
Declaration:
function schema_jsonSchema(name) { ... }
Parameters:
String
- Name of the objectObject
- json schemaBuilds json schema for model considering the selection and population
if fields
specified the method removes required
constraints
Declaration:
function model_jsonSchema(fields, populate) { ... }
Parameters:
String
|Array
|Object
- mongoose selection objectString
|Object
- mongoose population optionsObject
- json schemaBuilds json schema considering the query type and query options.
The method returns the schema for array if query type is find
and
the schema for single document if query type is findOne
or findOneAnd*
.
In case when the method returns schema for array the collection name is used to
form title of the resulting schema. In findOne*
case the title is the name
of the appropriate model.
Declaration:
function query_jsonSchema() { ... }
Parameters:
Object
- json schemaIf you use custom Schema Types you should define the jsonSchema method for your type-class(es).
The base functionality is accessible from your code by calling base-class methods:
newSchemaType.prototype.jsonSchema = function() {
// Simple types (strings, numbers, bools):
const jsonSchema = mongoose.SchemaType.prototype.jsonSchema.call(this);
// Date:
const jsonSchema = Types.Date.prototype.jsonSchema.call(this);
// ObjectId
const jsonSchema = Types.ObjectId.prototype.jsonSchema.call(this);
// for Array (or DocumentArray)
const jsonSchema = Types.Array.prototype.jsonSchema.call(this);
// for Embedded documents
const jsonSchema = Types.Embedded.prototype.jsonSchema.call(this);
// for Mixed documents:
const jsonSchema = Types.Mixed.prototype.jsonSchema.call(this);
/*
*
* Place your code instead of this comment
*
*/
return jsonSchema;
}
minlenght
and maxlength
issue#21forceRebuild
and fieldOptionsMapping
)required
fields: if required
is a function, it is not considered as required fieldrequired
: the minItems
constraint is removed from JSON schemaFAQs
Mongoose extension that allows to build json schema for mongoose models, schemas and queries
The npm package mongoose-schema-jsonschema receives a total of 13,634 weekly downloads. As such, mongoose-schema-jsonschema popularity was classified as popular.
We found that mongoose-schema-jsonschema demonstrated a healthy version release cadence and project activity because the last version was released less than 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.
Security News
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.