-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
feat(schema): support for union types #15574
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
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.
LGTM, though lint would need to be fixed and as Copilot already pointed out, the documentation for of
still refers to map
s.
Co-authored-by: Copilot <[email protected]>
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 introduces support for union types in Mongoose schemas, allowing a schema path to be one of multiple types. It implements a new Union
schema type with casting logic that tries each type in order and uses the first successful cast.
- Adds a new
Union
schema type that accepts an array of possible types in anof
property - Implements casting logic that preserves original values when possible and falls back to first successful cast
- Updates error messages to include string length information for better debugging
Reviewed Changes
Copilot reviewed 7 out of 10 changed files in this pull request and generated 2 comments.
Show a summary per file
File | Description |
---|---|
lib/schema/union.js | Implements the core Union schema type with casting and setter logic |
lib/schema/index.js | Exports the new Union schema type |
lib/schema.js | Updates schema interpretation to handle Union types |
lib/options/schemaUnionOptions.js | Defines options class for Union schema type |
lib/schemaType.js | Adds string length information to validation error context |
lib/error/messages.js | Updates error messages to include string length |
test/schema.union.test.js | Comprehensive test suite for Union functionality |
test/types/schema.test.ts | TypeScript type tests for Union schema inference |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
Co-authored-by: Copilot <[email protected]>
@@ -188,7 +188,7 @@ declare module 'mongoose' { | |||
_id?: boolean; | |||
|
|||
/** If set, specifies the type of this map's values. Mongoose will cast this map's values to the given type. */ |
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 comment should likely mention that the property is also valid for Union
now.
Fix #10894. There's still work to do on this PR (TypeScript types, options testing, thinking about how to handle getters/setters on unions) but I wanted to get this out there for review.
Summary
type: 'Union'
means a given path can be one of multiple types.For example, in the following schema,
purchased
can be either a Boolean or a Date.Order matters in
of
: Mongoose will loop through theOf
schema types and take the first one that casts successfully, unless one of the schema types returns the original value, in which case Mongoose just takes the original value.For example, in the above case,
purchased: 1
would get converted to a boolean (true) because Mongoose can cast 1 -> true. Butpurchased: 2
would get converted to a Date, because2
is not a value Mongoose can cast to a Boolean by default.Examples