question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

InterSchemaType does not properly infer "required" field for Schema.Types.ObjectId type

See original GitHub issue

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

Node.js version

16.13.1

MongoDB server version

6.x

Description

A schema field with {type: Schema.Types.ObjectId, required: true} cause faulty inference of Schema type. But, {type: Schema.Types.ObjectId, required: false} , {type: String, required: true} seem to work

Steps to Reproduce

The following works:

const campaignSchema = new Schema(
  {
    client: {
      type: Schema.Types.ObjectId,
      trim: true,
      ref: "User",
      required: false,
    },
  },
  { timestamps: true }
);

type ICampaign = InterSchemaType<ICampaignSchema>; // {client?: Types.ObjectId | undefined }

The following doesn’t work:

const campaignSchema = new Schema(
  {
    client: {
      type: Schema.Types.ObjectId, // works when the type is changed to "String"
      trim: true,
      ref: "User",
      required: true, // fails to interpret schema
    },
  },
  { timestamps: true }
);

type ICampaign = InterSchemaType<ICampaignSchema>; // DOES NOT return {client:  Types.ObjectId }

Screenshot-from-2022-08-03-09-31-43

Expected Behavior

type ICampaign = InterSchemaType<ICampaignSchema>;  // {client:  Types.ObjectId }

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
Uzlopakcommented, Aug 24, 2022
0reactions
ryaposovcommented, Sep 11, 2022

I can see the issue is in progress already, but just to confirm I have a similar issue even without specifying required.

...
type: {
    type: String,
    enum: ['income', 'expence', 'write-off'],
    required: true
  },
  procurementOrderId: {
    type: Schema.Types.ObjectId,
    ref: 'procurementOrders',
  },

Produces the following (notice type field resulting in unknown):

image

Removing procurementOrderId field from schema makes types work again perfectly.

Read more comments on GitHub >

github_iconTop Results From Across the Web

InferSchemaType generates Schema.Types.ObjectId instead ...
I learned from the documentation that when a Schema has a field whose type is Schema.Types.ObjectId, the corresponding Typescript type ...
Read more >
node.js - Populate Data In mongoose not working properly?
Here is the schema which I have created: var userSchema = new userSchema({ userId : {type : mongoose.Schema.Types.ObjectId ...
Read more >
Reference other Classes - typegoose
Schema.Types.ObjectId. note. The generic-parameter IDType from Ref is not automatically inferred from the generic-parameter Class yet (may be in the future)
Read more >
MongoDB | NestJS - A progressive Node.js framework
The schema types for these properties are automatically inferred thanks to TypeScript ... With this, you can indicate whether a property is required...
Read more >
Mongoose v6.8.2: Validation
The unique Option is Not a Validator; Custom Validators; Async Custom Validators; Validation Errors; Cast Errors; Global SchemaType Validation; Required ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found