Home > Blockchain >  Why Joi validation throwing error on nested object? (potentially related to dot notation in name att
Why Joi validation throwing error on nested object? (potentially related to dot notation in name att

Time:04-05

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.

CodePudding user response:

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

  • Related