Skip to content

toJSONSchema - include regex for ObjectId #16334

Description

@tomkel

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the feature has not already been requested

🚀 Feature Proposal

When converting mongoose schema toJSONSchema, ObjectIds are converted like so:
_id: { type: 'string' },

However it would be better if they also included the regex validation which could be easily consumed by validators.
Adapting from lib/schema/objectId.js, the JSON schema would be:

    "_id": {
      "type": "string",
      "pattern": "^[A-Fa-f0-9]{24}$"
    }

You could also reuse this pattern with $defs/$ref:
https://tour.json-schema.org/content/06-Combining-Subschemas/01-Reusing-and-Referencing-with-defs-and-ref

Motivation

I want to be able to get a portable schema from mongoose with toJSONSchema, and feed it into other validators such as zod, ajv, etc.

Example

No response

Metadata

Metadata

Labels

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

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions