Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
mongoose-ajv-plugin
Advanced tools
You love Mongoose for all it's convenience methods and
valiate-before-saving logic, but but you store complex objects using
Schema.Types.Mixed
which lacks validation in Mongoose, or you just wish
you could validate objects, strings, etc. using a richer
JSON-schema vocabulary than is included with
Mongoose.
The mongoose-ajv-plugin
lets you use the awesome AJV JSON-Schema
validation library, to validate individual attributes or entire
documents, giving you access to it's rich extensible schema vocabulary and convenience
formats like email, Date, hostname, ect.
Import mongoose and add in the mongoose-ajv-plugin
:
var mongoose = require("mongoose");
mongoose.plugin(require("mongoose-ajv-plugin"))
Now use your favorite AJV Schema, such as the ajv_contact_schema
defined
below, to validate entire documents using the "ajv-schema"
keyword, like
so:
var Contact_schema = new mongoose.Schema({
"name": String ,
"email": String,
"birthday": String,
// let AJV validate this entire document
"ajv-schema": ajv_contact_schema
});
Or use AJV to validate one or more attributes of a document using the "ajv-schema"
option:
// use AJV to validate fields within a document
var Player_schema = new Schema({
"user_name": String,
"rank": Number,
"ip_address": {
"type": String,
// let AJV validate this string attribute
"ajv-schema": {
"type": 'string',
"format": 'ipv4' /
}
},
"contact-info": {
"type": Schema.Types.Mixed ,
// let AJV validate this nested object
"ajv-schema": contact_json_schema
},
});
If you wish to extend the Ajv instance used for validation with additional schemata, formats, or keywords, you can pass your own (extended) ajv instance to the plugin, like so:
// create an Ajv instance
var Ajv = require("ajv");
var ajv = new Ajv();
// add custom schema, keywords, or formats
ajv.addSchema(...);
// or
ajv.addKewword(...)
// or
ajv.addFormat(...)
// or
require("my-ajv-plugin")(ajv)
// use this ajv instance to compile every new validator
mongoose.plugin(require("mongoose-ajv-plugin",{"ajv":ajv})
// or use this ajv instance to compile validators for an individual
// mongoose schema
var my_schema = new mongoose.Schema({...});
my_schema.plugin(require("mongoose-ajv-plugin",{"ajv":ajv})
And finally, here's the definition of ajv_contact_schema
used in the
above examples:
var ajv_contact_schema = {
"type":"object",
"properties":{
"name": {
"type":"string"
},
"email": {
"type":"string",
"fomrat":"email"
},
"birthday": {
"oneOf":[
{"$ref":"#/definitions/date"},
{"$ref":"#/definitions/date-time"}
]
}
},
"required":[
"name",
"email"
],
"definitions":{
"date":{
"type":"string",
"format":"date"
},
"date-time":{
"type":"string",
"format":"date-time"
}
}
};
FAQs
AJV plugin for Mongoose
The npm package mongoose-ajv-plugin receives a total of 1 weekly downloads. As such, mongoose-ajv-plugin popularity was classified as not popular.
We found that mongoose-ajv-plugin 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
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.