Return a mongoose schema as JSON

1,730 views
Skip to first unread message

Mobin Motallebizadeh

unread,
Nov 30, 2014, 3:27:02 AM11/30/14
to mongoo...@googlegroups.com
Hi

how to return a mongoose  schema as JSON? for example convert:

var InfoSchema = new Schema({
  name: {
    type: String,
    required: true
  },
  title: {
    type: String,
    required: true,
    trim: true
  }
});

to somthing like:

schema = {
    type: "object",
    properties: {
      name: { type: "string"},
      title: {
        type: "string"
      }
    };

stefan...@sinnerschrader.com

unread,
Dec 10, 2014, 7:48:32 AM12/10/14
to mongoo...@googlegroups.com
Try out https://github.com/stemey/mongoose-schema:

var schemaGenerator = new require('mongoose-schema').JsonSchemaGenerator();
var jsonSchema = schemaGenerator.generate(mongooseSchema);

Ryan Wheale

unread,
Dec 10, 2014, 12:29:12 PM12/10/14
to mongoo...@googlegroups.com
YourSchema.paths
YourModel.schema.paths

You can overwrite the toJSON method on those objects if you want.  

--
Documentation - http://mongoosejs.com/
Plugins - http://plugins.mongoosejs.com/
Bug Reports - http://github.com/learnboost/mongoose
Production Examples - http://mongoosejs.tumblr.com/
StackOverflow - http://stackoverflow.com/questions/tagged/mongoose
Google Groups - https://groups.google.com/forum/?fromgroups#!forum/mongoose-orm
Twitter - https://twitter.com/mongoosejs
IRC - #mongoosejs
---
You received this message because you are subscribed to the Google Groups "Mongoose Node.JS ODM" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongoose-orm...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages