samchon / nestia

NestJS Helper Libraries + TypeScript OpenAPI generator
https://nestia.io/
MIT License
1.71k stars 87 forks source link

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

Open xxmichas opened 3 weeks ago

xxmichas commented 3 weeks ago

Summary

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

samchon commented 3 weeks ago

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.

xxmichas commented 3 weeks ago

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.