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

158
Visualizações
Why Joi validation throwing error on nested object? (potentially related to dot notation in name attribute)

My joi validation is throwing an error when submitting a form. The error message reads:

STACK: Error: "author.surname" is not allowed at module.exports.citationJoi

As far as I can tell my joiSchema is ok:

const nestedScehma = Joi.object().keys({
    surname: Joi.string().trim().required(),
    firstname: Joi.string().trim().required()
  })

  
module.exports.citationJoi = (req, res, next) => {
  const memberSchema = Joi.object().keys({
    quote: Joi.string().trim().required(),
    author: nestedScehma,
    page: Joi.string().allow(null, '').trim(),
    year: Joi.string().allow(null, '').trim(),
    title: Joi.string().allow(null, '').trim(),
    publisher: Joi.string().allow(null, '').trim(),
    place: Joi.string().allow(null, '').trim()
  })

  const { error } = memberSchema.validate(req.body);
  if(error) {
    const msg = error.details.map(el => el.message).join(",");
    throw new AppError(msg, 400);
  } else {
    next();
  }
}

Which relates to my Mongoose Schema:

const quoteScheme = new Schema({
    quote: {
        type: String,
        required: true
    },
    author: {
        surname: {
            type: String,
            required: true
        },
        firstname: {
            type: String,
            required: true
        }
    },
    page: {
        type: String,
        required: false
    },
    year: {
        type: String,
        required: false
    },
    title: {
        type: String,
        required: false
    },
    publisher: {
        type: String,
        required: false
    },
    place: {
        type: String,
        required: false
    }

})

I have been stuck here for a while now and the only oddity (to my newbie mind at least) is that in the req.body has added quotations to the author.surname and author.firstname keys:

{
  quote: "Ozzy Osbourne's advice on how rock stars should treat young bands is wholesome as heck",
  'author.surname': 'tosser',
  'author.firstname': 'Oscar',
  page: '',
  year: '',
  title: '',
  publisher: '',
  place: ''
}

The name attribute on the input is "author.surname" relating to the Mongoose schema. I presume that somewhere along the way quotation marks have been added to the key as a reaction to the dot notation resulting in Joi throwing an error? Am I on the right lines? How can I fix this? Many thanks.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Hello I got this working by trying this:

module.exports.citationJoi = (req, res, next) => {
  const memberSchema = Joi.object({
    quote: Joi.string().trim().required(),
    "author.surname": Joi.string().trim().required(),
    "author.firstname": Joi.string().trim().required(),
    page: Joi.string().allow(null, "").trim(),
    year: Joi.string().allow(null, "").trim(),
    title: Joi.string().allow(null, "").trim(),
    publisher: Joi.string().allow(null, "").trim(),
    place: Joi.string().allow(null, "").trim(),
  })

  const {
    error
  } = memberSchema.validate(req.body);
  if (error) {
    const msg = error.details.map(el => el.message).join(",");
    throw new AppError(msg, 400);
  } else {
    next();
  }
}

It should work if this is the payload coming in:

{
  quote: "Ozzy Osbourne's advice on how rock stars should treat young bands is wholesome as heck",
  'author.surname': 'tosser',
  'author.firstname': 'Oscar',
  page: '',
  year: '',
  title: '',
  publisher: '',
  place: ''
}

about 4 years ago · Juan Pablo Isaza Relatório
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