You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In the above, if avatar were required in the interface, the schema's non-required avatar would not throw a compile time error. Similarly, if name were optional in the interface, no error is reported from the schema's name being required. This appears to be expected (see docs):
You as the developer are responsible for ensuring that your document interface lines up with your Mongoose schema. For example, Mongoose won't report an error if email is required in your Mongoose schema but optional in your document interface.
What is the expected behavior?
I would like for the schema to type-check whether a field is required based on the interface. This is possible using typescript's typing system (e.g. Stackoverflow):
typeMappedC<A,B>={[KinkeyofA&keyofB]:
A[K]extendsB[K]
? never
: K};typeOptionalKeys<T>=MappedC<T,Required<T>>[keyofT];
If there is interest, I'm happy to contribute a PR.
What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
This appears to be true for all versions of Mongoose and MongoDB.
The text was updated successfully, but these errors were encountered:
That's perfectly valid, even though avatar is not required in the original schema definition. The issue comes down to the fact that the schema definition doesn't necessarily represent the complete state of the schema.
Another example: you can also make all string fields required by default using mongoose.SchemaTypes.String.set('required', true);, which would mean all strings are required even though you don't set required in the schema definition.
vkarpov15
added
discussion
If you have any thoughts or comments on this issue, please share them!
and removed
new feature
This change adds new functionality, like a new method or class
labels
Jan 5, 2022
Do you want to request a feature or report a bug?
Request a feature
What is the current behavior?
Consider the following example:
In the above, if
avatar
were required in the interface, the schema's non-requiredavatar
would not throw a compile time error. Similarly, ifname
were optional in the interface, no error is reported from the schema'sname
being required. This appears to be expected (see docs):What is the expected behavior?
I would like for the schema to type-check whether a field is required based on the interface. This is possible using typescript's typing system (e.g. Stackoverflow):
If there is interest, I'm happy to contribute a PR.
What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
This appears to be true for all versions of Mongoose and MongoDB.
The text was updated successfully, but these errors were encountered: