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

Using typeKey and required throws a TypeError: Invalid schema configuration #14950

Closed
2 tasks done
greenafrican opened this issue Oct 10, 2024 · 1 comment · Fixed by #14956
Closed
2 tasks done

Using typeKey and required throws a TypeError: Invalid schema configuration #14950

greenafrican opened this issue Oct 10, 2024 · 1 comment · Fixed by #14956
Labels
typescript Types or Types-test related issue / Pull Request
Milestone

Comments

@greenafrican
Copy link

greenafrican commented Oct 10, 2024

Prerequisites

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

Mongoose version

8.6.3

Node.js version

20.x

MongoDB server version

6.0.16

Typescript version (if applicable)

5.5.1

Description

When defining a model like:

const SightingSchema = new Schema(
  {
    _id: { $type: Schema.Types.ObjectId, required: true },
    ...,
    location: {
      type: { $type: String, required: true },
      coordinates: [{ $type: Number }],
    },
  },
  {
    typeKey: "$type",
  }
);

I get the error:

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.

      3 |
    > 4 | const SightingSchema = new Schema(
        |                        ^
      5 |   {
      6 |     _id: { $type: Schema.Types.ObjectId, required: true }

      at Schema.add (node_modules/mongoose/lib/schema.js:740:13)
      at Schema.add (node_modules/mongoose/lib/schema.js:778:12)
      at new Schema (node_modules/mongoose/lib/schema.js:143:10)
      ...

When I don't use the typeKey: $type and just use type it works but can't be right for GeoJSON and as recommended in the Mongoose docs - https://mongoosejs.com/docs/guide.html#typeKey) and I get a warning in my tests:

Property 'coordinates' does not exist on type '{ type?: string | null | undefined; required?: unknown; }'

Steps to Reproduce

  1. use typeKey: $type in the options
  2. use required: true for the field

Expected Behavior

I expect it work just as it does without using a custom typeKey.

@vkarpov15
Copy link
Collaborator

I'm unable to repro, the following script runs successfully:

import mongoose from 'mongoose';

const { Schema } = mongoose;

const SightingSchema = new Schema(
  {
    _id: { $type: Schema.Types.ObjectId, required: true },
    location: {
      type: { $type: String, required: true },
      coordinates: [{ $type: Number }],
    },
  },
  {
    typeKey: "$type",
  }
);

Are you sure you don't have somewhere in your schema where you're using type instead of $type? For example, the following script prints the exact error message you're seeing, notice that test uses type instead of $type.

const SightingSchema = new Schema(
  {
    _id: { $type: Schema.Types.ObjectId, required: true },
    location: {
      type: { $type: String, required: true },
      coordinates: [{ $type: Number }],
    },
    test: { type: String, required: true }
  },
  {
    typeKey: "$type",
  }
);

We're investigating a workaround for the Property 'coordinates' does not exist on type '{ type?: string | null | undefined; required?: unknown; }' issue that doesn't involve using typeKey.

@vkarpov15 vkarpov15 added the typescript Types or Types-test related issue / Pull Request label Oct 11, 2024
@vkarpov15 vkarpov15 added this to the 8.7.2 milestone Oct 11, 2024
vkarpov15 added a commit that referenced this issue Oct 11, 2024
vkarpov15 added a commit that referenced this issue Oct 14, 2024
types: correct schema type inference when using nested typeKey like `type: { type: String }`
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
2 participants