Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Custom .Error does not work on schemas other than @Required #2696

Closed
ktalebian opened this issue May 3, 2024 · 3 comments · Fixed by #2699
Closed

[BUG] Custom .Error does not work on schemas other than @Required #2696

ktalebian opened this issue May 3, 2024 · 3 comments · Fixed by #2699
Assignees

Comments

@ktalebian
Copy link
Contributor

Describe the bug

Here is a request object:

class MyRequest {
  @Property()
  @MinLength(10).Error('Title must be at least 10 characters long')
  title: string;
}

And the controller

@Controller("/")
export class HelloWorldController {
  @Inject()
  myService: MyService;

  @Post("/body")
  @Returns(200, String)
  async cache(@BodyParams() body: MyRequest) {
    return 'done';
  }
}

I expect to get a custom error message when I make curl localhost:8083/body -XPOST -H "Content-Type: application/json" -d '{"title":"short"}' but still get the generic message:

{
  "name": "AJV_VALIDATION_ERROR",
  "message": "Bad request on parameter \"request.body\".\nMyRequest.title must NOT have fewer than 10 characters. Given value: \"foo\"",
  "status": 400,
  "errors": [
    {
      "instancePath": "/title",
      "schemaPath": "#/properties/title/minLength",
      "keyword": "minLength",
      "params": {
        "limit": 10
      },
      "message": "must NOT have fewer than 10 characters",
      "data": "foo",
      "modelName": "MyRequest",
      "dataPath": ".title",
      "requestPath": "body"
    }
  ],
}

I tried a few different decorators. It looks like only the @Required decorator works.

To Reproduce

  1. Clone https://github.com/ktalebian/tsed-bug
  2. npm i
  3. npm start
  4. curl localhost:8083/body -XPOST -H "Content-Type: application/json" -d '{"prop1":"asd"}' | jq .

Expected behavior

Expected to see Title must be at least 10 characters long message.

Code snippets

No response

Repository URL example

https://github.com/ktalebian/tsed-bug

OS

macOS

Node version

Node 20

Library version

v7.*

Additional context

No response

@Romakita
Copy link
Collaborator

Romakita commented May 3, 2024

@ktalebian thanks a lot for the issue. I'll try to that asap ;)

Copy link

🎉 Are you happy?

If you appreciated the support, know that it is free and is carried out on personal time ;)

A support, even a little bit makes a difference for me and continues to bring you answers!

github opencollective

@Romakita
Copy link
Collaborator

🎉 This issue has been resolved in version 7.70.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

2 participants