Security News
New axobject-query Maintainer Faces Backlash Over Controversial Decision to Support Legacy Node.js Versions
A JavaScript library maintainer is under fire after merging a controversial PR to support legacy versions of Node.js.
ajv-mongoose-converter
Advanced tools
Changelog
[1.1.1] 2024/02/08
Readme
This module converts a ajv schema into mongoose schema
Usage:
const ajv2mongoose = require('ajv-mongoose-converter');
const ajv_schema = {...};
const mongoose_schema = ajv2mongoose(ajv_schema);
As this object will be converted
const ajv_schema = {
properties: {
"_id": { type: "string", pattern: '^[0-9a-fA-F]{24}$' },
"name": { type: "string", example: "Emiliano" },
"age": { type: "integer", example: 55 },
"weight": { type: "number", example: 65.5 },
"accepted": { type: 'boolean', example: true },
"car": {
properties: {
"id": { type: "integer", example: 1 },
"model": { type: "string", example: "Ford" },
}
},
"birthday": { type: "string", format: 'date' },
"ts": { type: "timestamp" },
"pets": { type: 'array', items: { type: "string" } },
"custom_type": { type: 'custom_type' }
},
required: ["name", "car.id"],
};
into
{
_id: { type: 'ObjectId' },
name: { type: 'String', required: true },
age: { type: 'Number' },
weight: { type: 'Number' },
accepted: { type: 'Boolean' },
car: { id: { type: 'Number', required: true }, model: { type: 'String' } },
birthday: { type: 'Date' },
ts: { type: 'Date' },
pets: { type: [ 'String' ] },
custom_type: { type: 'Mixed' }
}
npm i ajv-mongoose-converter -s
In fastify-mongoose-api you could have an object which describes Mongoose schema and validation schema.
Without ajv-mongoose-converter your should write something like this
const personSchema = {
name: 'person',
ref: [{
$id: 'person',
properties: {
"_id": { type: "string", pattern: '^[0-9a-fA-F]{24}$' },
"name": { type: "string", example: "Emiliano" },
"age": { type: "integer", example: 55 },
"weight": { type: "number", example: 65.5 },
"accepted": { type: 'boolean', example: true },
"car": {
properties: {
"id": { type: "integer", example: 1 },
"model": { type: "string", example: "Ford" },
}
},
"birthday": { type: "string", format: 'date' },
"ts": { type: "timestamp" },
"pets": { type: 'array', items: { type: "string" } },
"custom_type": { type: 'custom_type' }
},
required: ["name", "car.id"],
}],
schema: {
_id: { type: 'ObjectId' },
name: { type: 'String', required: true },
age: { type: 'Number' },
weight: { type: 'Number' },
accepted: { type: 'Boolean' },
car: { id: { type: 'Number', required: true }, model: { type: 'String' } },
birthday: { type: 'Date' },
ts: { type: 'Date' },
pets: { type: [ 'String' ] },
custom_type: { type: 'Mixed' }
},
routeGet: { ... },
routePost: { ... },
routeList: { ... },
routePut: { ... },
routePatch: { ... },
routeDelete: { ... },
}
with duplicated infos in schema
and ref
.
With ajv-mongoose-converter this can be rewrite intro
const conv = require('ajv-mongoose-converter');
const personSchema = {
name: 'person',
ref: [{
$id: 'person',
properties: {
"_id": { type: "string", pattern: '^[0-9a-fA-F]{24}$' },
"name": { type: "string", example: "Emiliano" },
"age": { type: "integer", example: 55 },
"weight": { type: "number", example: 65.5 },
"accepted": { type: 'boolean', example: true },
"car": {
properties: {
"id": { type: "integer", example: 1 },
"model": { type: "string", example: "Ford" },
}
},
"birthday": { type: "string", format: 'date' },
"ts": { type: "timestamp" },
"pets": { type: 'array', items: { type: "string" } },
"custom_type": { type: 'custom_type' }
},
required: ["name", "car.id"],
}],
get schema() {
return conv(this.ref[0]);
},
...
}
This module supports both ESM and CommonJS. If you are using CommonJS, you can import it like so:
const conv = require('avj-mongoose-converter');
For feature requests or help, please visit the discussions page on GitHub.
For bug reports, please file an issue on the issues page on GitHub.
Contributions welcome! Please open a pull request on GitHub with your changes. You can run them by me first on the discussions page if you'd like. Please add tests for any changes.
Clone fastify-mongoose-api, run npm install
in its directory and run npm test
to run unit tests.
Licensed under GPLv3
FAQs
Convert an ajv into a mongoose schema
The npm package ajv-mongoose-converter receives a total of 0 weekly downloads. As such, ajv-mongoose-converter popularity was classified as not popular.
We found that ajv-mongoose-converter 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
A JavaScript library maintainer is under fire after merging a controversial PR to support legacy versions of Node.js.
Security News
Results from the 2023 State of JavaScript Survey highlight key trends, including Vite's dominance, rising TypeScript adoption, and the enduring popularity of React. Discover more insights on developer preferences and technology usage.
Security News
The US Justice Department has penalized two consulting firms $11.3 million for failing to meet cybersecurity requirements on federally funded projects, emphasizing strict enforcement to protect sensitive government data.