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

748
Vistas
joi 17.6.0 error does not make sense per rule: alphanum returns error on empty string

I'm using joi for front-end validation in React. Not using joi-browser since I'm told that was deprecated. Here's a part of the joi schema:

schema = Joi.object({
    namePrefix: Joi.string()
      .trim()
      .alphanum()
      .regex(/[a-b]/i)
      .min(0)
      .max(100)
      .messages({
        "string.pattern.base": "Name parts should be letters only",
      }),
    givenName: Joi.string()
      .trim()
      .alphanum()
      .regex(/[a-b]/i)
      .min(1)
      .max(100)
      .messages({
        "string.pattern.base": "Name parts should be letters only",
      })
      .required()});

Here's a prop updating function which also does the validation:

handleUpdateProp = (e, propName) => {
    const rule = this.schema.extract(propName);
    const subSchema = Joi.object({ [propName]: rule });
    const propValue = e.target.value;
    const objToValidate = { [propName]: propValue };
    const { error } = subSchema.validate(objToValidate);
    const validationResult = error ? error : null;
    const validationError = validationResult
      ? validationResult.details[0].message
      : null;
    const stateErrors = this.state.errors;
    stateErrors[propName] = validationError;
    this.setState({
      [propName]: propValue,
      errors: stateErrors,
    });
  };

namePrefix and givenName correspond to form fields and a div is conditionally rendered under them if there is an error for that field. If I type a character in namePrefix, no error: So far, so good. When I delete the character, joi returns error '"namePrefix" must only contain alpha-numeric characters'

Example: deleting characters throws alphanum error...

Obviously, no error should be returned because the string is allowed to be empty. Why is this happening? Please help!

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