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

300
Views
Mongoose nested ObjectId Array validation Dto

I have a Dto which looks like this:

import { Type } from "class-transformer";
import { ArrayMinSize, IsArray, ValidateNested} from "class-validator";
import { ObjectId } from "mongoose";

export class MongoIdDto {
    @IsArray()
    @ValidateNested({each: true})
    @ArrayMinSize(1)
    @Type(() => ObjectId)
    ids: ObjectId[]
}

But this throws me an error: 'ObjectId' only refers to a type, but is being used as a value here.

How does this error occur?

about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

This is a common import mistake:

ObjectId can be imported from mongoose and from mongoDB.

The mongoose import is a Type

The mongodb import is a class representation of the bson ObjectId Type

So to fix this issue change your import to: import { ObjectId } from "mongodb";

But actually there is an option to validate MongoIds with this:

export class MongoIdArrayDto {
    @IsMongoId({each: true})
    @ArrayMinSize(1)
    ids: ObjectId[]
}
about 4 years ago · Santiago Gelvez 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!