-
Notifications
You must be signed in to change notification settings - Fork 935
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
About setLocale and addMethod #2247
Comments
The This to me, is one of the great shortcomings of Yup. And you'll see that every example that shows how to fix this, is too simple: they assume you've already magiced up your translation from somewhere, to hardcode it into your schema. And I think we agree that translations, or translatable strings, do not belong in schemas. So in short, good to report this. But I don't think this will realistically be fixed short term, if ever. My workaround is to put these keys (e.g. How exactly this workaround is piped together, completely depends on the framework (if any) of your choice. |
I resolved this by adding a custom translation key property (e.g. setLocale({
mixed: {
default: (props) => function(props) {
const translationKey = props.i18nKey || 'mixed.default';
return t(translationKey, {...props})
}
},
}) addMethod(string, 'startsWith', function(expected, message) {
return this.test('startsWith', message, function(value) {
const { createError } = this;
return value?.toString().startsWith(expected) || createError({
params: {
expected, // << additional key for interpolation
i18nKey: "string.startsWith", // << pass translation-key parameter
},
});
});
}); |
I am experiencing an issue when using
setLocale
andaddMethod
in Yup. After creating a custom methodphone
and setting the locale, the error message returned is not what I expected.Code Used
Issue
When I run the code above, the error message I receive is:
I expected to get the message:
Expectations
I would like to understand why the error message generated does not match my expectations and how to fix it so that the
string.phone
error message appears when validation fails on thephone
method.Versions
Thank you for your help and clarification!
The text was updated successfully, but these errors were encountered: