Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature request: nested schemas - fully declarative pojo schema definitions #7494

Closed
captaincaius opened this issue Feb 6, 2019 · 1 comment
Labels
enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature
Milestone

Comments

@captaincaius
Copy link
Contributor

Do you want to request a feature or report a bug?

feature

This is very related to #7181 but after starting to write there I decided it's probably off-topic.

TL;DR:

  • hail mary request: nested objects in schema configuration objects make child schemas by default - just like arrays already do.
  • next best thing request: an boolean OPTION to do it without needing to use { type: new Schema(...) }

What is the current behavior?

  • If you want validation on multiple levels (and don't want Mixed), you need a child schema.
  • When a schema configuration has an array, a child schema is created automatically, and a simple pojo can fully describe your schema (including subschemas) \o/.

But when it's not an array, it's impossible to do so without:

child: { type: new Schema({...}) }

That means there are certain schemas that can't be fully described with passing a single configuration pojo to Schema() (namely when you need single subdocuments).

"What's the big deal? Just wrap the child in 'new Schema()' and move on", you say?

I share my schema definitions between /packages/client and /packages/server, and I currently can't have just a simple pojo in /packages/shared.

Instead of this:

export const schema = {
  sub: {
    type: {
      from: Number,
      to: Number
    },
    validate: ...
  }
}

I have to turn it into a factory of sorts so I can use the right "mongoose" - something like this:

export const schemaFactory = (mongoose) => ({
  sub: {
    type: new mongoose.Schema({
      from: Number,
      to: Number
    }),
    validate: ...
  }
});

What is the expected behavior?

Making the two equivalent would be great, but I don't think everyone would want it. So if instead, we can have a subSchema: true option or something like that it would be awesome possum.

Please mention your node.js, mongoose and MongoDB version.

@vkarpov15
Copy link
Collaborator

I like this idea, it's related to #7408. Will look into the option approach for 5.5, making this change in a minor release is probably a bit too heavy

@vkarpov15 vkarpov15 added this to the 5.5 milestone Feb 8, 2019
@vkarpov15 vkarpov15 added the enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature label Feb 8, 2019
@vkarpov15 vkarpov15 modified the milestones: 5.5, 5.x Unprioritized Apr 1, 2019
@vkarpov15 vkarpov15 modified the milestones: 5.x Unprioritized, 5.8.0 Dec 12, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature
Projects
None yet
Development

No branches or pull requests

2 participants