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

749
Visualizações
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 à 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