Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

159
Views
¿Por qué la validación de Joi arroja un error en el objeto anidado? (potencialmente relacionado con la notación de puntos en el atributo de nombre)

Mi validación joi arroja un error al enviar un formulario. El mensaje de error dice:

STACK: Error: "autor.apellido" no está permitido en module.exports.citationJoi

Por lo que puedo decir, mi joiSchema está bien:

 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(); } }

Lo que se relaciona con mi esquema de mangosta:

 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 } })

He estado atascado aquí por un tiempo y la única rareza (al menos para mi mente de novato) es que en el req.body se han agregado citas a las claves autor.apellido y autor.nombre:

 { 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: '' }

El atributo de nombre en la entrada es "autor.apellido" relacionado con el esquema Mongoose. Supongo que en algún momento se agregaron comillas a la clave como reacción a la notación de puntos que resultó en que Joi arrojara un error. ¿Estoy en la línea correcta? ¿Cómo puedo arreglar esto? Muchas gracias.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Hola, conseguí que esto funcionara al intentar esto:

 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(); } }

Debería funcionar si esta es la carga útil que entra:

 { 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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!