Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

218
Visualizações
Validating a number field with yup, using custom message and checking for empty field

I am validating a field that can only take a positive decimal number. My Yup schema looks as follow:

yup.object().shape({
Duration: yup
    .number()
    .typeError('Please enter a duration. The field cannot be left blank.')
    .positive('Must be a positive number.'),
})

As a result,

  1. When the user enters a negative number (including 0), the 'Must be a positive number' message is displayed
  2. When the user leaves the field blank, the 'Please enter a duration. The field cannot be left blank' message is displayed.
  3. But, when the user enters a string such as (two minutes or any other string), the 'Please enter a duration. The field cannot be left blank' message is displayed which does not make sense.

I could change the typeError() message for something more generic but I was wondering if there was a more comprehensive way to handle the scenario.

So, when the user enters

  1. a string I can display a 'Must be a number type' message and,
  2. when the field is left blank, I can display the 'Please enter a duration. The field cannot be left blank' message.
about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda