deepkit / deepkit-framework

A new full-featured and high-performance TypeScript framework
https://deepkit.io/
MIT License
3.2k stars 123 forks source link

[type] default values for HttpQuery causing ValidationError #529

Closed VayKekaz closed 9 months ago

VayKekaz commented 9 months ago

Providing default values for query parameters using myParam: HttpQuery<integer> = 0 syntax causes a bug where ValidationError is returned.

@http.controller()
export class Controller {

    @http.GET()
    endpoint(
        limit?: HttpQuery<integer & Positive & Maximum<100>>,
        offset: HttpQuery<integer & Positive> = 0,
    ) {
        // invoking this endpoint without query params causes error:
        const errorCaused = {
            "message": "Validation error:\noffset(type): Cannot convert undefined to HttpQuery<number>",
            "errors": [
                {
                    "path": "offset",
                    "code": "type",
                    "message": "Cannot convert undefined to HttpQuery<number>"
                }
            ]
        }
    }
}

Here is some workaround that i found is to use HttpQueries along with object deconstruction:

@http.controller()
export class Controller {

    @http.GET()
    endpoint(
        {
            limit = 10,
            offset = 0,
        }: HttpQueries<{
            limit?: integer & Positive & Maximum<100>
            offset?: integer & Positive
        }>,
    ) {
    }
}
marcj commented 9 months ago

Thanks for the report @VayKekaz , fixed in https://github.com/deepkit/deepkit-framework/commit/fa74d166d5421f8459f64c9b2339b9cf272a1b18