tsed: [BUG] Body Validation not working as expected

Describe the bug

I’m currently migrating a express application to Ts.ED. I used joi for the validation before and currently try to make AJV work, as i like the direct support & swagger integration.

I have a problem making the following work:

import { Integer, Required } from '@tsed/schema';

export class Test {
  @Integer()
  @Required()
  value: number;
}
@Post('/some-url')
async someFunction(@BodyParams() body: Test) {
  ...
}

Also writing @BodyParams(Test) ... does not work.

I don’t find an exact documentation for this, except from Any model used on parameter and annotated with one of JsonSchema decorator will be validated with AJV. written here. So i guess my usage is correct and should work?

Also in my server.ts i have:

import '@tsed/ajv';

What does work for me is something like:

@Post('/some-url')
async someFunction(@BodyParams('value') @Integer() @Required value: number) {
  ...
}

but i would like to validate the whole body. Is this possible?

To Reproduce

Should be quite is to set up, see above.

Because this is also a kind of “should this be even possible” - issue, i dind’t create an extra reproducable repo. If you need one, let me know.

Expected behavior

I would expect the full body to be validated correctly when used as described above. The request should ail with the ajv error.

Code snippets

No response

Repository URL example

No response

OS

macOS

Node version

18.17.1

Library version

7.41.2

Additional context

No response

About this issue

  • Original URL
  • State: closed
  • Created 8 months ago
  • Comments: 16 (5 by maintainers)

Commits related to this issue

Most upvoted comments

Hey, can’t reproduce this in a starter, will dig into whats going on in my project.

Thank you and i really appreciate this project, works great so far 👍🏼