Skip to content

Convert JSON schema to mongoose 5 schema

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE
MIT
LICENSE.md
Notifications You must be signed in to change notification settings

kristianmandrup/convert-json-schema-to-mongoose

Repository files navigation

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.

Alternatives

Some of the alternatives...

Installation

npm i convert-json-schema-to-mongoose -S

Usage

import { createMongooseSchema } from 'convert-json-schema-to-mongoose';

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

// 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);

About

Convert JSON schema to mongoose 5 schema

Resources

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE
MIT
LICENSE.md

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published