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

175
Views
Mongoose: pre('validate') middleware is not working

This is my mongoose Setup. This happens only when i use class syntax. When i do the same thing with the use of functional programming it works fine. This is the first time i am using class syntax to do this. I think that's where the problem lies. I am doing something wrong with my class definition. This is my mongoose Setup. This happens only when i use class syntax. When i do the same thing with the use of functional programming it works fine. This is the first time i am using class syntax to do this. I think that's where the problem lies. I am doing something wrong with my class definition.

const mongooseService = require('./services/mongoose.service')
const slugify = require('slugify')
const { marked } = require('marked')
const createDomPurifier = require('dompurify')
const { JSDOM } = require('jsdom')
const dompurify = createDomPurifier(new JSDOM().window)

class ArticleDao {
  Schema = mongooseService.getMongoose().Schema

  articleSchema = new this.Schema({
    title: {
      type: String,
      required: true,
    },
    description: {
      type: String,
    },
    markdown: {
      type: String,
      required: true,
    },
    createdAt: {
      type: Date,
      default: new Date(),
    },
    slug: {
      type: String,
      required: true,
      unique: String,
    },
    sanitizedHtml: {
      type: String,
      required: true,
    },
  })

  Article = mongooseService.getMongoose().model('Article', this.articleSchema)

  constructor() {
    console.log(`created new instance of DAO`)
    this.setPreValidation()
  }

  setPreValidation() {
    console.log('h')
    this.articleSchema.pre('save', (next) => {
      if (this.title) {
        this.slug = slugify(this.title, { lower: true, strict: true })
      }
      if (this.markdown) {
        this.sanitizedHtml = dompurify.sanitize(marked(this.markdown))
      }
      next()
    })
  }

  async addArticle(articleFields) {
    const article = new this.Article(articleFields)
    await article.save()
    return article
  }

  async getArticleById(articleId) {
    return this.Article.findOne({ _id: articleId }).exec()
  }

  async getArticleBySlug(articleSlug) {
    return this.Article.findOne({ slug: articleSlug })
  }

  async getArticles() {
    return this.Article.find().exec
  }

  async updateArticleById(articleId, articleFields) {
    const existingArticle = await this.Article.findOneAndUpdate({
      _id: articleId,
      $set: articleFields,
      new: true,
    }).exec()
    return existingArticle
  }

  async removeArticleById(articleId) {
    await this.Article.findOneAndDelete({ _id: articleId }).exec()
  }
}

module.exports = new ArticleDao()

This is the error i get:

Article validation failed: sanitizedHtml: Path `sanitizedHtml` is required., slug: Path `slug` is required.
about 4 years ago · Juan Pablo Isaza
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!