question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Define custom message as function, not string

See original GitHub issue

I 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:closed
  • Created 6 years ago
  • Reactions:14
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

22reactions
klis87commented, Jan 14, 2019

@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 you t 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 configured setLocale to provide defaults.

6reactions
fchubucommented, Mar 10, 2020

@klis87 thanks a lot. @jquense Why that isn’t in the documentation yet?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Hashnode Post

No results found