question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Body validation is not working

See original GitHub issue

I’m submitting a…


[ ] Regression 
[x] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

When using https://github.com/nestjs/nest/tree/master/sample/01-cats-app example, doing POST / with empty body doesn’t trigger validation/doesn’t result in 400 Bad Request error.

Expected behavior

Test fails, as HTTP request returns error 400 (as described in https://docs.nestjs.com/techniques/validation).

Minimal reproduction of the problem with instructions

I have changed create method to

@Post() //@Roles(‘admin’) async create(@Body() createCatDto: CreateCatDto) { //this.catsService.create(createCatDto); }

and added a test case

it(/POST cats, () => { return request(app.getHttpServer()) .post(‘/cats’) .expect(201); });

to https://github.com/nestjs/nest/blob/master/sample/01-cats-app/e2e/cats/cats.e2e-spec.ts

What is the motivation / use case for changing the behavior?

Because request validation is importtan and critical feature (and according to documentation it supposed to work).

Environment

Nest version: Cloned repo from https://github.com/nestjs/nest/commit/5e2727bf905cc3a4ca26267c6a385793088ff43e

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
kamilmysliwieccommented, May 12, 2019

Create a helper function which takes app as an argument and applies configuration (pipes etc).

1reaction
kamilmysliwieccommented, May 12, 2019

Tests aren’t using ValidationPipe, see https://github.com/aeremin/nest/blob/a519f02b4482e28f6e93a355593a703a682bc927/sample/01-cats-app/e2e/cats/cats.e2e-spec.ts#L12-L21 Add:

app = module.createNestApplication();
app.useGlobalPipes(new ValidationPipe());
Read more comments on GitHub >

github_iconTop Results From Across the Web

Spring boot @Valid on requestBody in controller method not ...
The post mapping I have shared is working fine i.e. I am successfully getting my returned response "No validation error", just @Valid annotation ......
Read more >
Validate Request Body and Parameter in Spring Boot
In this tutorial, we will see how to use Spring features to validate the request body and request parameters.
Read more >
Validate Request Body and Parameter in Spring Boot - Medium
When Spring finds an argument annotated with @Valid, it automatically validates the argument and throws an exception if the validation fails. Run the ......
Read more >
Validation with Hibernate Validator - Quarkus
This guide covers how to use Hibernate Validator/Bean Validation for: ... It might not always be handy to have the validation rules declared...
Read more >
Validation with Spring Boot - the Complete Guide - Reflectoring
This article is accompanied by a working code example on GitHub. ... Note that the validation starter does no more than adding a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found