Skip to content

feat(model): add standard schema adapter for models - #16308

Merged
vkarpov15 merged 4 commits into
masterfrom
vkarpov15/gh-16280
Jun 3, 2026
Merged

feat(model): add standard schema adapter for models#16308
vkarpov15 merged 4 commits into
masterfrom
vkarpov15/gh-16280

Conversation

@vkarpov15

Copy link
Copy Markdown
Collaborator

Fix #16280

cc @ssalbdivad

Summary

Support for ~standard validate adapter. See standard schema docs.

Outstanding questions:

  1. Casting: is User['~standard'].validate({ age: '42' }) ok given that Mongoose will convert string '42' to number 42 and return the result in result?

Examples

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 first-class support for the Standard Schema v1 ~standard adapter on Mongoose models, enabling model-based validation that returns Standard Schema-compatible { value } / { issues } results (Fixes #16280).

Changes:

  • Add Model['~standard'] runtime implementation that wraps Model.validate() and converts Mongoose validation errors into Standard Schema issue arrays.
  • Add TypeScript declarations for mongoose.StandardSchemaV1 and the Model['~standard'] property.
  • Add runtime + type-level tests covering success/failure results, pathsToSkip, and discriminator behavior.

Reviewed changes

Copilot reviewed 4 out of 5 changed files in this pull request and generated no comments.

Show a summary per file
File Description
types/models.d.ts Defines StandardSchemaV1 types and adds Model['~standard'] to the public model type surface.
test/types/models.test.ts Adds type tests asserting adapter shape and result typing/narrowing behavior.
test/model.validate.test.js Adds runtime tests for Standard Schema result format, pathsToSkip, and discriminator support.
lib/standardSchema/convertErrorToIssues.js Converts Mongoose ValidationError into Standard Schema issues with array-based path.
lib/model.js Implements Model['~standard'] via a getter that wraps Model.validate() and formats results.

@hasezoey hasezoey left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Looks good to me from a mongoose perspective, i dont know if it is correct according to Standard Schema though.

Comment thread types/models.d.ts
Comment thread types/models.d.ts
Comment thread test/model.validate.test.js
@hasezoey hasezoey added the new feature This change adds new functionality, like a new method or class label May 29, 2026
@vkarpov15
vkarpov15 merged commit 19e2280 into master Jun 3, 2026
53 checks passed
@vkarpov15
vkarpov15 deleted the vkarpov15/gh-16280 branch June 3, 2026 18:48
Comment thread lib/model.js

/**
* Standard Schema adapter for this model.
* Calls [`Model.validate()`](#Model.validate()) internally with the provided `libraryOptions`

@hasezoey hasezoey Jun 4, 2026

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This link got messed up.

Nevermind, didnt see the closing ), still all links should start with mongoosejs.com as they are replaced automatically and so can work in jsdoc too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

new feature This change adds new functionality, like a new method or class

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Standard Schema support

3 participants