Skip to content

schema type auto infer bugs #12030

@imranbarbhuiya

Description

@imranbarbhuiya

Prerequisites

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

Mongoose version

6.4.2

Node.js version

18

MongoDB server version

5.0

Description

I've found a few more bugs in auto infer type

  1. Adding an object directly to the array doesn't infer type correctly
const userSchema = new Schema({
	users: [
		{
			username: { type: String }
		}
	]
});
  1. passing function in default makes the key optional
const userSchema = new Schema({
	createdAt: { type: Date, default: Date.now }
});
  1. Array type becomes Array instead of Types.DocumentArray which makes pushing fields difficult
const userSchema = new Schema({
	users: [
		new Schema({
			username: { type: String },
			credit: { type: Number, default: 0 }
		})
	]
});

const userModel = model('user', userSchema);

const data = await userModel.findOne();
data!.users.push({ username: 'parbez' });
// ts warns to add createdAt which can be solved using Types.DocumentArray
  1. Adding empty object as default doesn't makes the key required
const userSchema = new Schema({
	data: { type: { role: Object }, default: {} }
});
  1. Assigning an object directly to a key doesn't infer type correctly
const userSchema = new Schema({
	data: {
		username: String
	}
});

Steps to Reproduce

added code blocks above

Expected Behavior

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions