Skip to content

Latest commit

History

18 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

Known VulnerabilitiesbitHound Overall ScorebitHound Dependencies

Mongoosify

Mongoosify is a Javascript library to parse a JSON Schema into a Mongoose Schema.

Installation

npm install mongoosify --save

Usage

Let suppose the JSON object is saved as jschema.json

{
"type":"object",
"properties":{
"address":{
"type":"object",
"properties":{
"street":{"type":"string"},
"house":{"type":"string"},
"city":{"type":"string"}
}
},
"firstName":{"type":"string"},
"lastName":{"type":"string"},
"title":{
"type":"string",
"enum":["Dr","Prof.","Ph.D."]
},
"email":{
"type":"array",
"items":{"type":"string"}
},
"age":{"type": "integer"}
}
}

The following code demonstrate how to parse a JSON schema from the jschema.json file into a mongoose schema (I called it mongoosify :D)

varmongoosify=require("mongoosify");varmongoose=require("mongoose");varmyJsonSchemaFile=require("./jschema.json");varmySchema=mongoosify(myJsonSchemaFile);varSchema=mongoose.Schema;varmongooseSchema=newSchema(mySchema);//now you are ready to go...

How to run tests

$ npm test

Important notes

Mongoosify will always try to cast type implicitly if a schema and the data don't have a same type. If a casting is not possible (e.g. "Hallo" can not be cast into a number but "10" can be) and CastError will be throw by Mongoose.

Current task in progress

Unit tests for the actual code

Planned features

  1. Enable reusability with keywords $ref, $schema and id.
  2. Evaluation of the following constraints:
    • schema keywords:
      • additionalProperties
      • required
    • array keywords:
      • minItems
      • maxItem
      • uniqueItems
    • numeric types keywords:
      • minimum
      • maximum
      • multipleOf
    • string keyword:
      • format

You can contribute!

Want to contributor? Just need a pull request! I look forward to have as contributor on board!

About

A JavaScript library to convert a JSON Schema into a Mongoose Schema, https://www.npmjs.com/package/mongoosify

Resources

Stars

4 stars

Watchers

2 watching

Forks

Releases

Packages

Used by

Contributors

Languages