Skip to content

fix(types): correct Model.validate() return type to Promise<TRawDocType>#16340

Merged
vkarpov15 merged 1 commit into
Automattic:masterfrom
chatman-media:fix/model-validate-return-type
Jun 23, 2026
Merged

fix(types): correct Model.validate() return type to Promise<TRawDocType>#16340
vkarpov15 merged 1 commit into
Automattic:masterfrom
chatman-media:fix/model-validate-return-type

Conversation

@chatman-media

Copy link
Copy Markdown
Contributor

Summary

Model.validate() is typed to resolve to void, but at runtime it returns the casted-and-validated copy of the input object.

In lib/model.js, Model.validate() does obj = this.castObject(obj) and ends with return obj;. The JSDoc on the implementation already documents this:

@return {Promise<object>} casted and validated copy of \obj` if validation succeeded`

However, the TypeScript declarations in types/models.d.ts typed every overload as Promise<void>, so consumers cannot use the resolved value:

const validated = await MyModel.validate({ a: 'string' });
validated.a; // ts(2339): Property 'a' does not exist on type 'void'.

This PR changes the return type to Promise<TRawDocType>, mirroring the adjacent castObject(...): TRawDocType declaration (validate() returns exactly what castObject() produced). The existing Model['~standard'].validate() adapter already assumes validate resolves to the data (it wraps the result as { value }), and its type test asserts result.value is the raw doc type — consistent with this change.

Fixes #16338

Examples

Added type tests in test/types/models.test.ts (modelValidateReturnsCastedObject):

const validated = await User.validate({ name: 'Val', age: 42 });
expect(validated).type.toBe<IUser>();
expect(validated.name).type.toBe<string>();
expect(validated.age).type.toBe<number>();

These fail before the fix (Property 'age' does not exist on type 'void') and pass after. Full npm run test:types suite passes (39 files, 978 assertions), eslint clean.

Model.validate() casts and validates the input and returns the
casted-and-validated copy (see lib/model.js: returns obj after
this.castObject(obj)), as the JSDoc already documents
(@return casted and validated copy of obj). The TypeScript
declarations incorrectly typed all overloads as Promise<void>,
so the resolved value could not be used.

Mirror castObject()'s TRawDocType return type and add type tests.

Fixes Automattic#16338

@vkarpov15 vkarpov15 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.

Thanks 👍

@vkarpov15 vkarpov15 added this to the 9.7.3 milestone Jun 23, 2026
@vkarpov15 vkarpov15 merged commit 0734de7 into Automattic:master Jun 23, 2026
3 checks passed
@vkarpov15 vkarpov15 modified the milestones: 9.7.3, 8.24.2 Jun 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Model.validate() types are wrong

2 participants