-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
fix: add validation support for Union schema types #15734
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
base: master
Are you sure you want to change the base?
Conversation
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 validation support for Union schema types in Mongoose, enabling proper validation of required fields and custom validators within union schemas and their array elements.
- Implements
doValidateanddoValidateSyncmethods for the Union SchemaType to validate values against union member schemas - Adds validation path optimization checks to prevent Union types from being skipped during validation
- Includes best match scoring logic to select the most appropriate schema when casting union values
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| lib/schema/union.js | Implements validation methods (doValidate, doValidateSync) and enhances casting logic with best match scoring for object values |
| lib/document.js | Adds instance !== 'Union' checks to validation path optimization to ensure Union types are not skipped during validation |
| test/schema.union.validation.test.js | Comprehensive test coverage for Union validation including required fields, custom validators, array validation, and synchronous validation |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
lib/schema/union.js
Outdated
| return castedVal; | ||
| } | ||
|
|
||
|
|
Copilot
AI
Nov 10, 2025
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.
[nitpick] Extra blank line should be removed for consistency with the codebase style.
test/schema.union.validation.test.js
Outdated
| assert.ifError(err2); | ||
| }); | ||
|
|
||
| it('should not skip validation for arbitrary fields', async function() { |
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 makes no sense to perform an additional test to see if the same incorrect input fails with an extra field. Instead, it needs to check that the arbitrary field has been removed from the object. It is better to not error out on creation and check the saved document to see if the arbitrary field was removed.
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.
Thanks @timheerwagen for the review. I have made the necessary changes. Please have a look into it
Updated the test to reflect the removal of arbitrary fields from subdocuments on save, ensuring validation is correctly applied.
This PR implements validation for Union schema types to properly validate required fields and custom validators in union schemas and their array elements.
Breaking Changes: None
Test Results: All 3946 tests passing with 5 new Union validation tests
This commit message follows best practices with:
Clear prefix (fix:) indicating the type of change
Concise subject line summarizing the main change
Detailed body explaining what was changed and why
List of specific files and modifications
Reference to the issue being fixed
Test coverage information