Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

217
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda