Define custom message as function, not string
See original GitHub issueI need to integrate yup
with i18n-next
for translations. That’s why I need to be able to define messages as functions. There was discussion about this in #71, but I cannot make it work:
import { setLocale } from 'yup/lib/customLocale';
setLocale({
string: {
required: (obj) => {
// obj is undefined
return `${obj.path} is required`; // this woudnt work as I don't have obj.path available
// return 'required'; doesn't work either as this message is never displayed
},
},
});
Above is just an example, but it seems that functions are not allowed. But this is really weird, as function is used in the default locale - https://github.com/jquense/yup/blob/master/src/locale.js#L8 . So question is, are functions allowed and if yes, what I am doing wrong here?
Issue Analytics
- State:
- Created 6 years ago
- Reactions:14
- Comments:7 (2 by maintainers)
Top Results From Across the Web
Add custom messages in assert? - c++ - Stack Overflow
A hack I've seen around is to use the && operator. Since a pointer "is true" if it's non-null, you can do the...
Read more >Error.prototype.message - JavaScript - MDN Web Docs
The message property combined with the name property is used by the Error.prototype.toString() method to create a string representation of the Error.
Read more >Handle and return errors from your custom function
The CustomFunctions.Error object is used to return an error back to the cell. When you create the object, specify which error you want...
Read more >Test Functions | qmake Manual - Qt Documentation
This function never returns a value. qmake displays string as an error message to the user and exits. This function should only be...
Read more >Throw error and display message - MATLAB error - MathWorks
Create a function hello that requires one input argument. Add a suggested input argument "world" to the error message. ... Call the function...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@mshahov you shouldn’t put any t function in schema, based on your example, your email field should be
email: Yup.string() .email('invalidEmail').required('required')
, so notice, in schema you put i18n CODE, not translation. You need to yout
function in React, so for instance{form.errors.email && t(form.errors.email)}
But generally error message are repetitive, thats why you might just have email schema like
email: Yup.string() .email() .required()
, with configuredsetLocale
to provide defaults.@klis87 thanks a lot. @jquense Why that isn’t in the documentation yet?