New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

ajv-mongoose-converter

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ajv-mongoose-converter

Convert an ajv into a mongoose schema

  • 1.1.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
2
decreased by-81.82%
Maintainers
1
Weekly downloads
 
Created
Source

Convert ajv into mongoose schema

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' }
}

Installation

npm i ajv-mongoose-converter -s

Usage with fastify-mongoose-api

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]);
    },
    ...
}


CommonJS

This module supports both ESM and CommonJS. If you are using CommonJS, you can import it like so:

const conv = require('avj-mongoose-converter');

Bugs / Help / Feature Requests / Contributing

Tests

Clone fastify-mongoose-api, run npm install in its directory and run npm test to run unit tests.

License

Licensed under GPLv3

Keywords

FAQs

Package last updated on 08 Feb 2024

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc