Using typeKey
and required
throws a TypeError: Invalid schema configuration #14950
Closed
Description
opened on 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
- use
typeKey: $type
in the options - use
required: true
for the field
Expected Behavior
I expect it work just as it does without using a custom typeKey
.
Activity