Skip to content

Conversation

@codervipul775
Copy link

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

Copy link
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 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 doValidate and doValidateSync methods 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.

return castedVal;
}


Copy link

Copilot AI Nov 10, 2025

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.

Suggested change

Copilot uses AI. Check for mistakes.
assert.ifError(err2);
});

it('should not skip validation for arbitrary fields', async function() {

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.

Copy link
Author

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

2 participants