Skip to content

fix(error): only stringify ValidatorError properties used in the message - #16763

Open
MIbtehajNasar wants to merge 1 commit into
Automattic:masterfrom
MIbtehajNasar:fix-16762-validator-message-tostring
Open

MIbtehajNasar wants to merge 1 commit into
Automattic:masterfrom
MIbtehajNasar:fix-16762-validator-message-tostring

Conversation

@MIbtehajNasar

Copy link
Copy Markdown
Contributor

Fix #16762

Summary

When hydration fails to cast a value, init() wraps the CastError in a ValidatorError. formatMessage() then calls msg.replace() once for each property, including value. That stringifies the value even when the message has no {VALUE} placeholder, so a value like { toString: 1 } throws TypeError: Cannot convert object to primitive value instead of recording the cast error.

formatMessage() now stringifies a property only when its placeholder appears in the message.

Examples

Added the test case from the issue to test/model.hydrate.test.js. It fails on master with the TypeError and passes with this change. test/document.test.js, test/schema.validation.test.js, test/model.test.js, test/errors.validation.test.js and eslint pass.

This branch has not been deployed

No deployments
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.

"Cannot convert object to primitive value" when hydrating with a value with non-callable toString

1 participant