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

164
Views
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 answers
Answer question

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