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

Cannot create a schema with a subdocument containing the key "type" #13154

Closed
2 tasks done
JavaScriptBach opened this issue Mar 9, 2023 · 2 comments · Fixed by #13164
Closed
2 tasks done

Cannot create a schema with a subdocument containing the key "type" #13154

JavaScriptBach opened this issue Mar 9, 2023 · 2 comments · Fixed by #13164
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@JavaScriptBach
Copy link
Contributor

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

7.0.1

Node.js version

18

MongoDB server version

4.4

Typescript version (if applicable)

No response

Description

I can't create a schema with a subdocument containing the key "type". See below for examples

Steps to Reproduce

This code fails to create a schema:

const TestSchema = {
  action: {
    $type: {
      type: {
        $type: String,
        required: true,
      },
    },
    required: true
  },
};
const s = new Schema(TestSchema, { typeKey: "$type" });

The intention is to produce a document type like this:

{
  action: {
    type: String
  }
}

It gives the error:

Uncaught:
TypeError: Invalid schema configuration: `true` is not a valid type at path `required`. See https://bit.ly/mongoose-schematypes for a list of valid schema types.

Expected Behavior

Schema should successfully get created

@vkarpov15 vkarpov15 added this to the 7.0.2 milestone Mar 10, 2023
@vkarpov15 vkarpov15 added the has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue label Mar 10, 2023
@IslandRhythms IslandRhythms added confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. and removed has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue labels Mar 10, 2023
@IslandRhythms
Copy link
Collaborator

You have the option to use the working version until the issue is resolved.

const mongoose = require('mongoose');
const { Schema } = mongoose;

console.log('works');

const testSchema = new Schema({
  action: {
    type: {
      type: String,
      required: true
    },
  },
  name: {
    type: String,
    required: true
  }
})

const Test = mongoose.model('Test', testSchema);

console.log('does not work');

const TestSchema = {
  action: {
    $type: {
      type: {
        $type: String,
        required: true,
      },
    },
    required: true
  },
};


const s = new Schema(TestSchema, { typeKey: "$type" });

const S = mongoose.model('S', s);

@JavaScriptBach
Copy link
Contributor Author

Thanks @IslandRhythms, but it looks like your alternative schema wouldn't have the same behavior as action doesn't have the required validator set. Looking forward to the fix. Would it also be possible to backport the fix to Mongoose 6 when it's completed?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants