feat(schema): include ObjectId regex pattern in toJSONSchema() output#16335
Merged
vkarpov15 merged 2 commits intoJun 15, 2026
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Adds ObjectId string validation constraints to Mongoose’s portable schema.toJSONSchema() output by including a hex ObjectId regex pattern, while keeping the MongoDB $jsonSchema-oriented { useBsonType: true } output unchanged.
Changes:
- Update
SchemaObjectId#toJSONSchema()to includepattern: '^[A-Fa-f0-9]{24}$'when not usinguseBsonType. - Update existing AJV-based tests to use valid 24-hex
_idvalues now that the schema is stricter. - Add a focused regression test covering ObjectId paths (including
_id) for both portable JSON Schema anduseBsonTypemodes (gh-16334).
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| lib/schema/objectId.js | Adds ObjectId pattern to portable JSON Schema output; skips it for useBsonType where bsonType: 'objectId' is used. |
| test/schema.test.js | Updates expected schema snapshots and AJV validations; adds regression coverage for ObjectId pattern behavior. |
| test/schema.union.test.js | Updates union schema expectations and AJV validations to use valid ObjectId-like _id strings. |
hasezoey
approved these changes
Jun 15, 2026
vkarpov15
approved these changes
Jun 15, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #16334
schema.toJSONSchema()rendered ObjectId paths (including_id) as{ type: 'string' }, which puts no constraint on the value. This addspattern: '^[A-Fa-f0-9]{24}$'so the portable schema can validate ObjectId strings when fed into ajv, zod, etc.The pattern is only added to the plain JSON Schema representation. The
useBsonType: trueoutput is unchanged, sincebsonType: 'objectId'already validates ObjectIds and that's the representation used for MongoDB$jsonSchemacollection validators.