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.

SetLocale is not working as expected - Instance/Babel problem

See original GitHub issue

Problem

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:closed
  • Created 5 years ago
  • Reactions:69
  • Comments:15 (1 by maintainers)

github_iconTop GitHub Comments

9reactions
jquensecommented, Aug 20, 2018

you need to set the locale before you import yup elsewhere. schema constructed before you use setLocale will have the old values

5reactions
MarcoScabbiolocommented, Mar 30, 2019

If anyone has this problem using TypeScript you have to define all your schemas as a function that takes yup, for example:

import * as yup from 'yup';

export default (y: typeof yup) =>
  y
    .string()
    .email()
    .max(300);

Pass yup (import * as yup from 'yup') to your schemas after calling setLocale on it.

Read more comments on GitHub >

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

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 Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found