Skip to content

Optional body throw ValidateError if using body-parser #1839

@MathieuRA

Description

@MathieuRA

I'm currently using a body-parser middleware, and it transforms an undefined body into an empty object ({}).
This causes a validation problem when a body is marked as optional.

Example

@Post('')
myMethod(@Body() body?: {foo: boolean}){
 ...
}

If the user does not send a body, body-parser will inject {} into request.body, then getValidatedArgs will test the schema on an empty object instead of undefined, which will throw a ValidateError because foo is marked as required.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions