SetLocale is not working as expected - Instance/Babel problem
See original GitHub issueProblem
I’m trying to use the function setLocale
and It’s not translating the errors. The problem seems to be with the library instance, some babel configuration or import
problem.
When I use setLocale and return the same instance of yup
to my validation files It does work as expected.
Scenario
Should work like this. Right? index.js
import { setLocale } from 'yup';
setLocale({
mixed: {
required: 'Preencha esse campo para continuar'
},
string: {
email: 'Preencha um e-mail válido',
min: 'Valor muito curto (mínimo ${min} caracteres)',
max: 'Valor muito longo (máximo ${max} caracteres)'
},
number: {
min: 'Valor inválido (deve ser maior ou igual a ${min})',
max: 'Valor inválido (deve ser menor ou igual a ${max})'
}
});
personal-data-validations.js
import * as yup from 'yup';
const PersonalDataSchema = yup.object().shape({
fullName: yup.string().min(5).max(70).required()
});
export { PersonalDataSchema };
The workaround
I created a index to my validations -> validations.js
and export yup from there. After that I imported the yup instance from my validations index and now It’s working.
validations.js
import * as yup from 'yup';
yup.setLocale({
mixed: {
required: 'Preencha esse campo para continuar'
},
string: {
email: 'Preencha um e-mail válido',
min: 'Valor muito curto (mínimo ${min} caracteres)',
max: 'Valor muito longo (máximo ${max} caracteres)'
},
number: {
min: 'Valor inválido (deve ser maior ou igual a ${min})',
max: 'Valor inválido (deve ser menor ou igual a ${max})'
}
});
export default yup;
personal-data-validations.js
import yup from 'validation';
const PersonalDataSchema = yup.object().shape({
fullName: yup.string().min(5).max(70).required()
});
export { PersonalDataSchema };
Issue Analytics
- State:
- Created 5 years ago
- Reactions:69
- Comments:15 (1 by maintainers)
Top Results From Across the Web
locale - c - setlocale() not working as expect - Stack Overflow
The question is: The result of 2 execution are expected to be different format, due to different locale, but they are the same,...
Read more >setlocale - Manual - PHP
On Windows, setlocale(LC_ALL, '') sets the locale names from the system's regional/language settings (accessible via Control Panel). Return Values ¶. Returns ...
Read more >May 18, 2022•Knowledge 000118942 - Search - Informatica
To resolve this issue, do the following: Set the LANG and LC_ALL to C. Unset all the environment variables pointing to previous Informatica...
Read more >1949559 – "setlocale: No such file or directory" when running ...
I have compared 2 rhel 9 system, one with such issue and another has no such issue, and found it may related with...
Read more >How do I fix my locale issue? - Ask Ubuntu
Nothing suggested above worked in my case (Ubuntu Server 12.04LTS). ... a common problem if you are connecting remotely, so the solution is...
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 Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
you need to set the locale before you import yup elsewhere. schema constructed before you use setLocale will have the old values
If anyone has this problem using TypeScript you have to define all your schemas as a function that takes yup, for example:
Pass yup (
import * as yup from 'yup'
) to your schemas after callingsetLocale
on it.