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

@ctrld/json-schema-to-mongoose

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@ctrld/json-schema-to-mongoose

A library for converting json-schema to mongoose schema

  • 0.2.6
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

JSON Schema to Mongoose (Schema)

A translation library between JSON Schema and Mongoose Schema. Written in TypeScript.

This project was created from the ashes of json-schema-converter. I took away some features and made it more single purpose and added features to the conversion.

Installation

npm install https://github.com/agrozyme/json-schema-to-mongoose.git

Usage

import createMongooseSchema from 'json-schema-to-mongoose';

// Or use plain javascript
// var createMongooseSchema = require('json-schema-to-mongoose').default;
// var util = require('util');

// example json-schema references
const refs =
{
    yep:
    {
        type: 'string',
        pattern: '^\\d{3}$'
    },
    idSpec: {
        type: 'object',
        properties:
        {
            id:
            {
                $ref: 'yep'
            }
        }
    }
};

// example schema to convert to mongoose schema
const schema =
{
    type: 'object',
    properties:
    {
        id:
        {
            $ref: 'yep'
        },
        address:
        {
            type: 'object',
            properties:
            {
                street: {type: 'string', default: '44', pattern: '^\\d{2}$'},
                houseColor: {type: 'string', default: '[Function=Date.now]', format: 'date-time'}
            }
        }
    }
};

//Convert the schema
const mongooseSchema = createMongooseSchema(refs, schema);

//Alternative syntax, which makes it so you can convert many at one time.

// var jsonSchemas = {commonRef: ..., good: ..., schema: ..., naming: ...}
// var convert = createMongooseSchema(jsonSchemas)
// var schemaNames = ['good', 'schema', 'naming']
// var schemas = _.map(schemaNames, (name) => { return jsonSchemas[name] })
// var mongooseSchemas = _.zipObject(schemaNames, schemas.reduce((mongooseSchemas, schema) => {
//     return mongooseSchemas.concat(convert(schema))
// }, []))

console.dir(mongooseSchema, {depth: null});

const Schema = new mongoose.Schema(mongooseSchema);

Keywords

FAQs

Package last updated on 06 Dec 2021

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