docs(schema): add instanceof/typeof checks to all default getters for populated doc and nullish value handling#16272
docs(schema): add instanceof/typeof checks to all default getters for populated doc and nullish value handling#16272vkarpov15 wants to merge 2 commits into
Conversation
… populated doc and nullish value handling Fix #16250
There was a problem hiding this comment.
Pull request overview
This PR updates the inline SchemaType API documentation examples for several built-in types to make the “default getter” snippets more robust when values are nullish and when values are not of the expected primitive/BSON type (for example, when an ObjectId path is populated).
Changes:
- Adjusted getter examples to guard with
typeof/instanceof/Buffer.isBuffer()before calling type-specific methods. - Updated doc comments to mention null/undefined handling (and populated docs for ObjectId).
- Kept the examples in the SchemaType source JSDoc in sync across multiple schema types (String, Number, Date, ObjectId, Buffer, Decimal128, BigInt).
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| lib/schema/string.js | Updates String getter example to guard before calling toLowerCase() (but example text/API reference needs correction). |
| lib/schema/objectid.js | Updates ObjectId getter example to guard with instanceof mongoose.Types.ObjectId. |
| lib/schema/number.js | Updates Number getter example to guard with typeof v === 'number'. |
| lib/schema/decimal128.js | Updates Decimal128 getter example to guard with instanceof mongoose.Types.Decimal128 (but API reference needs correction). |
| lib/schema/date.js | Updates Date getter example to guard with instanceof Date. |
| lib/schema/buffer.js | Updates Buffer getter example to guard with Buffer.isBuffer() (but the surrounding example has copy/paste errors). |
| lib/schema/bigint.js | Updates BigInt getter example to guard with typeof v === 'bigint' (but API reference needs correction). |
hasezoey
left a comment
There was a problem hiding this comment.
LGTM, except for the String text, which mentions numbers, but does nothing with numbers (as Copilot already pointed out).
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 7 out of 7 changed files in this pull request and generated 3 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| * // Always convert to string when getting an ObjectId. Check for instanceof ObjectId first | ||
| * // in case of null/undefined values as well as populated documents. |
| * // to account for null/undefined values as well as populated docs. | ||
| * mongoose.Schema.Types.String.get(v => typeof v === 'string' ? v.toLowerCase() : v); |
| * | ||
| * // Make all numbers round down | ||
| * mongoose.Number.get(function(v) { return Math.floor(v); }); | ||
| * mongoose.Number.get(function(v) { return typeof v === 'number' ? Math.floor(v) : v; }); |
Fix #16250
Summary
Default getters as described in the docs are currently somewhat brittle - they don't account for populated docs or nullish values. Fix that so these examples are more copy/pastable.
Examples