nestia icon indicating copy to clipboard operation
nestia copied to clipboard

`TypedParam` should use validator configured in tsconfig just like `TypedQuery` / `TypedBody`

Open xxmichas opened this issue 1 year ago • 2 comments

Summary

  • nestia: 3.2.1
  • typia: 6.0.6
  • Expected behavior: I would expect TypedParam error to follow the same interface as TypedQuery and TypedBody.
  • Actual behavior: TypedParam uses assert regardless of value in tsconfig

tsconfig:

{ 
    "transform": "@nestia/core/lib/transform",
    "validate": "validate"
}

Code:

@core.TypedRoute.Get(":id")
public test(
  @core.TypedParam("id") id: number,
  @core.TypedQuery() query: { x: number },
): void {}

TypedParam error (bug):

{
  "path": "$input",
  "reason": "Error on typia.http.parameter(): invalid type on $input, expect to be number",
  "expected": "number",
  "value": "d",
  "message": "Invalid URL parameter value on \"id\"."
}

TypedParam error (expected):

{
  "errors": [
    {
      "path": "$input",
      "expected": "number",
      "value": "d"
    }
  ],
  "message": "Invalid URL parameter value on \"id\"."
}

This change would unify their interfaces and make it easier to consume apis created with nestia.

Related code: TypedParam: https://github.com/samchon/nestia/blob/master/packages/core/src/decorators/TypedParam.ts#L50 TypedQuery: https://github.com/samchon/nestia/blob/master/packages/core/src/decorators/TypedQuery.ts#L49 TypedBody: https://github.com/samchon/nestia/blob/master/packages/core/src/decorators/TypedBody.ts#L30

xxmichas avatar Jun 10 '24 05:06 xxmichas

As @TypedParam() decorated parameter has only one atomic type, I just used assert function only.

If you want the IValidation typed error message format, it is not hard thing, but have to connsider about when configured the validation function to be is() case. How it would be? Also, as it is a break change on the API level, if accept your suggestion, it would adapted in the next major version.

samchon avatar Jun 10 '24 05:06 samchon

Thanks for quick response.

When is() is used, both TypedQuery and TypedBody return:

interface _ {
  message: string;
  error: "Bad Request";
  statusCode: 400;
}

I think TypedParam should follow that interface too.

xxmichas avatar Jun 10 '24 05:06 xxmichas

Your opinion is correct. I'll update it as you want.

samchon avatar Dec 16 '24 05:12 samchon