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

Schemas where index is an object fail to compile in Mongoose 6.6.5 #12562

Closed
2 tasks done
FlameWolf opened this issue Oct 17, 2022 · 1 comment
Closed
2 tasks done

Schemas where index is an object fail to compile in Mongoose 6.6.5 #12562

FlameWolf opened this issue Oct 17, 2022 · 1 comment
Labels
typescript Types or Types-test related issue / Pull Request
Milestone

Comments

@FlameWolf
Copy link

FlameWolf commented Oct 17, 2022

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.6.5

Node.js version

18.2.0

MongoDB server version

4.10.0

Typescript version (if applicable)

4.8.4

Description

I have a schema in my application that contains a partialFilterExpression inside an index. It was working fine until I upgraded Mongoose from 6.6.2 to 6.6.5. Now it fails to compile with TS 2322 errors on all the properties. But it will work fine if I remove the partial index specification. This happens with another model that contains a text index too, where the index is specified like index: { text: true, ... }.

Steps to Reproduce

Try commenting/uncommenting the index specification on the email field.

"use strict";

import { Schema, model } from "mongoose";

const userSchema = new Schema(
	{
		handle: {
			type: String,
			trim: true,
			required: true,
			unique: true,
			uniqueCaseInsensitive: true,
			index: true
		},
		password: {
			type: String,
			trim: true,
			required: true,
			select: false
		},
		email: {
			type: String,
			trim: true,
			index: {
				partialFilterExpression: {
					email: {
						$exists: true,
						$ne: null
					}
				}
			},
			select: false
		}
	},
	{
		timestamps: true,
		collation: {
			locale: "en",
			strength: 2
		}
	}
);

export default model("User", userSchema);

Expected Behavior

The schema should compile without errors.

@IslandRhythms IslandRhythms added the typescript Types or Types-test related issue / Pull Request label Oct 17, 2022
@IslandRhythms
Copy link
Collaborator

Thats weird

"use strict";

import { Schema, model } from "mongoose";

const emailRegExp = /@/
const userSchema = new Schema(
	{
		handle: {
			type: String,
			trim: true,
			required: true,
			unique: true,
			uniqueCaseInsensitive: true,
			index: true
		},
		password: {
			type: String,
			trim: true,
			required: true,
			select: false
		},
		email: {
			type: String,
			trim: true,
			validate: {
				validator: (value: string) => emailRegExp.test(value),
				message: "Email is not valid"
			},
			index: { // uncomment the index object and for me trim was throwing an error
				partialFilterExpression: {
					email: {
						$exists: true,
						$ne: null
					}
				}
			},
			select: false
		}
	},
	{
		timestamps: true,
		collation: {
			locale: "en",
			strength: 2
		}
	}
);

export default model("User", userSchema);

@vkarpov15 vkarpov15 added this to the 6.6.9 milestone Oct 24, 2022
@vkarpov15 vkarpov15 modified the milestones: 6.7.3, 6.7.2 Nov 2, 2022
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

No branches or pull requests

3 participants