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

types: allow defining document array using [{ prop: String }] syntax #14095

Merged
merged 3 commits into from
Nov 21, 2023

Conversation

vkarpov15
Copy link
Collaborator

Fix #13424

Summary

In #13424 we noticed that Mongoose doesn't infer { type: [{ name: String }] } as a documentarray, whereas { type: [new Schema({ name: String })] } does. This PR fixes that by explicitly looking through the inferred type of the array in the schema and checking against possible definitions like type: [String] and type: [Schema.Types.ObjectId].

Examples

Copy link
Collaborator

@hasezoey hasezoey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, with some questions

test/types/schema.test.ts Outdated Show resolved Hide resolved
test/types/schema.test.ts Outdated Show resolved Hide resolved
@hasezoey hasezoey added the typescript Types or Types-test related issue / Pull Request label Nov 19, 2023
@vkarpov15 vkarpov15 merged commit d53b1c5 into master Nov 21, 2023
3 checks passed
@hasezoey hasezoey deleted the vkarpov15/gh-13424 branch November 21, 2023 19:55
@imranbarbhuiya
Copy link

imranbarbhuiya commented Feb 16, 2024

Hi @vkarpov15 , today I was updating mongoose and found all the prop values of the schema types became unknown so I tried to check previous versions and found it's starting from 8.0.2, after checking commits and reverting them, and found this commit it causing the issue. It happens when the array of object has a type as a prop, and also if the schema has a prop with value SchemaTypes.Array

Here's my schema

export const UserSchema = new Schema(
    {
        reminders: {
            type: [
                {
                    type: { type: SchemaTypes.String },
                    date: { type: SchemaTypes.Date },
                    toggle: { type: SchemaTypes.Boolean },
                    notified: { type: SchemaTypes.Boolean },
                },
            ],
            default: [
                { type: 'vote', date: new Date(), toggle: false, notified: false },
                { type: 'daily', date: new Date(), toggle: false, notified: false },
                { type: 'drop', date: new Date(), toggle: false, notified: false },
                { type: 'claim', date: new Date(), toggle: false, notified: false },
                { type: 'work', date: new Date(), toggle: false, notified: false },
            ],
        },
        avatar: {
            type: SchemaTypes.String,
        },
    },
    { timestamps: true },
);

export type IUser = InferSchemaType<typeof UserSchema>;

Lemme know if I'm doing anything wrong with the schema. Or if it's a bug, I'll be happy to contribute, lemme me know how can I fix it.

If you want me to create a separate issue instead of this comment then also lemme know and I'll create a separate issue. Thanks

@hasezoey
Copy link
Collaborator

@imranbarbhuiya

If you want me to create a separate issue instead of this comment then also lemme know and I'll create a separate issue. Thanks

seeing as there has not been a reply and this not being the best place to report this, consider opening a new issue referencing this PR, if this is the problem

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
typescript Types or Types-test related issue / Pull Request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

When migrating from mongoose 5 to 7, how to get LeanDocument funcitonality?
4 participants