InterSchemaType does not properly infer "required" field for Schema.Types.ObjectId type
See original GitHub issuePrerequisites
- 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 }
Expected Behavior
type ICampaign = InterSchemaType<ICampaignSchema>; // {client: Types.ObjectId }
Issue Analytics
- State:
- Created a year ago
- Comments:7 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@mohammad0-0ahmad
I can see the issue is in progress already, but just to confirm I have a similar issue even without specifying
required
.Produces the following (notice
type
field resulting inunknown
):Removing
procurementOrderId
field from schema makes types work again perfectly.