sebbulon sebbulon - 10 months ago 49
Node.js Question

define a schema with JSON-Schema and use Mongoose?

I have a crux to bear with Mongoose.
Is there a way of using JSON-Shema with Mongoose Schemas?
Say I want to define my API data schema using a standard like JSON-Schema, because it's nice.
It seems like I need to define it again when I want to use Mongoose / MongoDB!
That's quite some ugly duplication I like to avoid. Ideally, changing the JSON-Schema definition would also change the MongoDB schema.
A similar problem would appear if I would use JOI.JS validation library.
Has anyone found a solution to that?
Or is there an alternative approach?



Try this library: There are others out there too. I created json-schema-to-mongoose since the other libraries didn't quite fit my needs.

Also, I like to generate the json-schema from TypeScript using Typson. It makes it so the json-schema is more statically typed.