Skip to content

feat(schema): include ObjectId regex pattern in toJSONSchema() output#16335

Merged
vkarpov15 merged 2 commits into
Automattic:masterfrom
AbdelrahmanHafez:feat/objectid-jsonschema-pattern
Jun 15, 2026
Merged

feat(schema): include ObjectId regex pattern in toJSONSchema() output#16335
vkarpov15 merged 2 commits into
Automattic:masterfrom
AbdelrahmanHafez:feat/objectid-jsonschema-pattern

Conversation

@AbdelrahmanHafez

Copy link
Copy Markdown
Collaborator

Closes #16334

schema.toJSONSchema() rendered ObjectId paths (including _id) as { type: 'string' }, which puts no constraint on the value. This adds pattern: '^[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: true output is unchanged, since bsonType: 'objectId' already validates ObjectIds and that's the representation used for MongoDB $jsonSchema collection validators.

new Schema({ author: { type: Schema.Types.ObjectId } }).toJSONSchema();
// {
//   type: 'object',
//   required: ['_id'],
//   properties: {
//     author: { type: ['string', 'null'], pattern: '^[A-Fa-f0-9]{24}$' },
//     _id: { type: 'string', pattern: '^[A-Fa-f0-9]{24}$' }
//   }
// }

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 include pattern: '^[A-Fa-f0-9]{24}$' when not using useBsonType.
  • Update existing AJV-based tests to use valid 24-hex _id values now that the schema is stricter.
  • Add a focused regression test covering ObjectId paths (including _id) for both portable JSON Schema and useBsonType modes (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.

@AbdelrahmanHafez AbdelrahmanHafez marked this pull request as ready for review June 15, 2026 08:26
@AbdelrahmanHafez AbdelrahmanHafez added this to the 9.7.1 milestone Jun 15, 2026
@AbdelrahmanHafez AbdelrahmanHafez added the enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature label Jun 15, 2026
@vkarpov15 vkarpov15 merged commit 2bbe775 into Automattic:master Jun 15, 2026
26 checks passed
@AbdelrahmanHafez AbdelrahmanHafez deleted the feat/objectid-jsonschema-pattern branch June 18, 2026 10:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

toJSONSchema - include regex for ObjectId

4 participants