I'm really struggling to grasp what's happening here and where I might be going wrong. I've searched through similar threads, but none seem to address my specific issue (most discussions revolve around validating typed Arrays).
Let me present the class that I am trying to validate:
export class CreateActivityDto {
//other properties that are not causing any issues
@IsArray()
@ValidateNested({ each: true })
@Type(() => Plan)
plan: Plan[] //this part is functioning correctly
@IsObject()
@IsDefined()
@Type(() => Location) //imported from class-transformer
location: Location
}
class Location {
@IsString()
@IsNotEmpty()
address: string
@IsObject()
@Type(() => LatLng) //imported from class-transformer
coords: LatLng
}
class LatLng {
@IsNumber()
lng: number
@IsNumber()
lat: number
}
Oddly enough, if I declare location as an array of locations (location: Location[]), the error doesn't occur.
I encounter this error during runtime, just a few milliseconds after the server attempts to bootstrap:
[6:25:19 pm] Found 0 errors. Watching for file changes.
[fullDirHere]\src\activity\dtos\activity.dto.ts:52
location: Location
^
ReferenceError: Cannot access 'Location' before initialization
And here is a snippet from my main.ts file:
async function bootstrap() {
const app = await NestFactory.create(AppModule, { cors: true });
app.useGlobalPipes(new ValidationPipe({
whitelist: true,
transform: true,
transformOptions: {
enableImplicitConversion: true
}
}))
await app.listen(3000);
}
bootstrap();
Just to provide clarity, here is an example of a valid object that I could potentially receive:
{
"location": {
"address": "Av. Corrientes 2003, Buenos Aires, Argentina",
"coords": {
"lat": -34.6042967,
"lng": -58.39546360000001
},
...other properties...
}