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

166
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
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