#Validation Error an unknown value was passed to the validate function

21 messages · Page 1 of 1 (latest)

rapid kiln
#

Im using a fresh installation of nestjs with typeorm configure
and i use the following package https://github.com/woowabros/nestjs-library-crud
it generates the routes properly and swagger is fine. how ever when i try to create an entity im getting this error

here is my entity

import { IsOptional } from 'class-validator';
import {
  Column,
  CreateDateColumn,
  DeleteDateColumn,
  Entity,
  PrimaryGeneratedColumn,
  UpdateDateColumn,
} from 'typeorm';

@Entity('apis')
export class API {
  @IsOptional()
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  test: string;

  @IsOptional()
  @CreateDateColumn({ type: 'timestamptz' })
  created_at: Date;

  @IsOptional()
  @UpdateDateColumn({ type: 'timestamptz' })
  updated_at: Date;

  @IsOptional()
  @DeleteDateColumn({ type: 'timestamptz' })
  deleted_at: Date;
}

I have configured validation pipe like so

app.useGlobalPipes(
    new ValidationPipe({
      forbidUnknownValues: false,
    }),
  );
{
  "message": [
    {
      "target": {
        "test": "test"
      },
      "children": [],
      "constraints": {
        "unknownValue": "an unknown value was passed to the validate function"
      }
    }
  ],
  "error": "Unprocessable Entity",
  "statusCode": 422
}
#

🥲🥲🥲🥲

#

i have not slept for 2 days and now this error is killing me

sly marsh
#

Can yiou provide a simple way to reproduce the error?

rapid kiln
#

well theres alot of generated modules i have.

#

the issue is ValidationPipe is not working i guess

#
app.useGlobalPipes(
    new ValidationPipe({
      forbidUnknownValues: false,
      forbidNonWhitelisted: true,
      errorHttpStatusCode: 500,
      exceptionFactory: (e) => {
        console.error(e);
        throw new BadRequestException('You shall not pass!');
      },
    }),
  );

even with this configuration it still throws 422

sly marsh
#

My immediate thought is that this may be an issue with conflicting reflect-metadata packages. If you were to remove the ValidationPipe, what happens?

rapid kiln
#

nothing happens

sly marsh
#

You get values in the controller as you expect to?

rapid kiln
#

nope when i call the api it throws that error

sly marsh
#

Even without the validation pipe?

rapid kiln
#

yup

#
curl -X 'POST' \
  'http://localhost:3000/api' \
  -H 'accept: application/json' \
  -H 'Content-Type: application/json' \
  -d '{
"test": "test"
}'
``` this is how i call the api
sly marsh
#

That shouldn't be happening, unless you end up throwing a 422 yourself

#

I wouldn't actually expect a 422 in the first place

rapid kiln
sly marsh
#

It's doing validation for you, the validation fails, and it throws the 422

rapid kiln
#

yeah, the thing is it ignores ValidationPipe completly
because it not doing the validation in an standard way i suppose?

sly marsh
#

Interceptors are triggered before pipes are, so this is behaving as expected from what I can tell