-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
docs: add documentation for Union Schema Type #15721
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
docs: add documentation for Union Schema Type #15721
Conversation
timheerwagen
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's hard to review because of all the formatting changes made by the editor, but LGTM!
In my opinion, the Array of Unions should be added directly here after the merge of #15720.
|
Thank you! Yes, I can add documentation for Array of Unions in a follow-up PR after this is merged. Should I open a new issue for it or just create the PR directly? |
vkarpov15
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Union type docs are good, but can you please undo all the unnecessary formatting changes? I can do that as well, but I would need to create a new PR for that.
53f6fc0 to
510e622
Compare
There was a problem hiding this 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 adds documentation for the new Union SchemaType in Mongoose, which allows a schema path to accept multiple types with automatic casting.
- Adds
Unionto the list of available SchemaTypes - Documents usage examples showing how to define union types with the
ofarray - Explains casting behavior, error handling, options support, and query/update behavior
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| await Model.create({ value: 42 }); | ||
|
|
||
| // Query with string - gets cast to number | ||
| const doc = await Model.findOne({ value: '42' }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This one is incorrect because of the "exact match" rule - since String is one of the valid types, Mongoose will leave '42' as a string.
Added examples and explanations for Union SchemaType.
Closes #15719
Summary
This PR adds documentation for the Union Schema Type feature that was introduced in Mongoose 8.18. The Union type was missing documentation as reported in issue #15719, making it difficult for users to discover and use this feature.
The Union SchemaType allows developers to define fields that can accept multiple types (e.g., a field that can be either a String or a Number), with automatic type casting. This is useful for flexible schemas where a field's type may vary based on the data source or use case.
Examples
The documentation includes comprehensive examples covering:
All examples are based on the test cases in
test/schema.union.test.jsand the implementation inlib/schema/union.js.