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