Skip to content

docs(schema): add instanceof/typeof checks to all default getters for populated doc and nullish value handling#16272

Open
vkarpov15 wants to merge 2 commits into
7.xfrom
vkarpov15/gh-16250
Open

docs(schema): add instanceof/typeof checks to all default getters for populated doc and nullish value handling#16272
vkarpov15 wants to merge 2 commits into
7.xfrom
vkarpov15/gh-16250

Conversation

@vkarpov15
Copy link
Copy Markdown
Collaborator

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

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment thread lib/schema/string.js Outdated
Comment thread lib/schema/buffer.js Outdated
Comment thread lib/schema/decimal128.js Outdated
Comment thread lib/schema/bigint.js Outdated
Copy link
Copy Markdown
Collaborator

@hasezoey hasezoey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, except for the String text, which mentions numbers, but does nothing with numbers (as Copilot already pointed out).

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread lib/schema/objectid.js
Comment on lines +61 to +62
* // Always convert to string when getting an ObjectId. Check for instanceof ObjectId first
* // in case of null/undefined values as well as populated documents.
Comment thread lib/schema/string.js
Comment on lines +111 to +112
* // to account for null/undefined values as well as populated docs.
* mongoose.Schema.Types.String.get(v => typeof v === 'string' ? v.toLowerCase() : v);
Comment thread lib/schema/number.js
*
* // 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; });
Copy link
Copy Markdown
Collaborator

@AbdelrahmanHafez AbdelrahmanHafez left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other than Copilot's comments, LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants