Routing-controllers: QueryParams does not include ParamOptions as like as the documentation says?

Created on 24 May 2017  路  13Comments  路  Source: typestack/routing-controllers

I can not find ParamOptions while using the newly added feature QueryParams?

fix

Most helpful comment

Actually @QueryParams validation works right now. I even created a test case for this:

describe.only("should validate @QueryParams input", () => {

    class UserQuery {

        @MinLength(5)
        author: string;

        @MinLength(5)
        contributor: string;

        hello() {
            return "Hello world!";
        }
    }

    let requestQuery: UserQuery;
    beforeEach(() => {
        requestQuery = undefined;
    });

    before(() => {
        getMetadataArgsStorage().reset();

        @JsonController()
        class UserController {

            @Get("/github")
            getUsers(@QueryParams() query: UserQuery): any {
                requestQuery = query;
                return query.hello();
            }

        }
    });

    const options: RoutingControllersOptions = {
        validation: true
    };

    let expressApp: any, koaApp: any;
    before(done => expressApp = createExpressServer(options).listen(3001, done));
    after(done => expressApp.close(done));
    before(done => koaApp = createKoaServer(options).listen(3002, done));
    after(done => koaApp.close(done));

    assertRequest([3001], "get", "github?author=Umed&contributor=19majkel94&somethingPrivate=blablabla", response => {
        console.log(response.body.errors);
        expect(response).to.have.status(400);
        expect(response.body.errors).to.have.lengthOf(1);
        expect(response.body.errors[0].value).to.eql("Umed");
    });
});

But correct errors are returned in response only in express - in koa I get this in errors property:

TypeError: Cannot read property 'prototype' of undefined
       at E:\#Programowanie\#GitHub\routing-controllers\src\metadata\MetadataStorage.ts:79:36
       at Array.filter (native)
       at MetadataStorage.getTargetValidationMetadatas (E:\#Programowanie\#GitHub\routing-controllers\src\metadata\MetadataStorage.ts:75:61)
       at ValidationExecutor.execute (E:\#Programowanie\#GitHub\routing-controllers\src\validation\ValidationExecutor.ts:44:54)
       at Validator.coreValidate (E:\#Programowanie\#GitHub\routing-controllers\src\validation\Validator.ts:31:18)
       at Validator.<anonymous> (E:\#Programowanie\#GitHub\routing-controllers\src\validation\Validator.ts:73:35)
       at step (E:\#Programowanie\#GitHub\routing-controllers\node_modules\class-validator\validation\Validator.js:32:23)
       at Object.next (E:\#Programowanie\#GitHub\routing-controllers\node_modules\class-validator\validation\Validator.js:13:53)
       at E:\#Programowanie\#GitHub\routing-controllers\node_modules\class-validator\validation\Validator.js:7:71
       at Promise (<anonymous>) }

I will try to fix it and implement the ParamOptions feature 馃槈

All 13 comments

Yeah, looks like @pleerock forgot to add ParamOptions param to the @QueryParams() and other decorators like @HeaderParams, etc.:

export function QueryParams(): Function

So now we can't turn on/off validation or set class-transform options.

Thats a documentation issue. QueryParams do not accept param options because QueryParams is dummy - it simply take request.query and returns it. No any other operation applied to the extract data.

because QueryParams is dummy - it simply take request.query and returns it. No any other operation applied to the extract data.

@pleerock
It is really bad idea, if user want to validate the query object he can't do it because there is now no param option to turn on validation or pass validation settings. Without #122 we have to make a dirty hack with sending json in query param.
I know that other options like required makes no sense, but class-transformer and class-validator would be very useful on query object.

Type transformation wont work because we cant read data type of array, so classtransformation and validation wont work until explicit given type. Usually all params in query params are different which means array of different object types. How are we going to specify them to QueryParams() ? Via array of types? This wont look good and seems useless.

Hello @pleerock , I think it will be nice if we add this feature as @19majkel94 says, it will be nice if we have the ability to enable the validations for query params, example: if i'm sending an email format inside a query string and want to validate it..etc

because we cant read data type of array

How are we going to specify them to QueryParams() ? Via array of types?

Wait... what? What array? 馃槰

@Get("/test/:paramOne/:paramTwo")
public async test(@Req() req: any) {
    const query = req.query;
    const params = req.params;

    return {
        query,
        params,
    }
}

GET /test/pleerock/19majkel94?express=better&koa=poor

2017-05-25 21_42_16-users ts - backend - visual studio code

{
  "query": {
    "express": "better",
    "koa": "poor"
  },
  "params": {
    "paramOne": "pleerock",
    "paramTwo": "19majkel94"
  }
}

Both route params and query params are an OBJECT, not an ARRAY. So you can declare a class, decorate it with class-validator decorator and class-transformer will transform plain object to class instance and allow to validate.

ah right I forgot that its an object 馃槅 . But what class do you want to define - special for the whole query object?, e.g. @QueryParams() params: UserRouteParams ? If so, then its a good solution and probably we can implement it

special for the whole query object?, e.g. @QueryParams() params: UserRouteParams

Yes, like I already have for body validation. Until #122 it's the only solution for auto validating route params.

Will this be reopened? It would be really useful if @QueryParams worked just like @Body.

Actually @QueryParams validation works right now. I even created a test case for this:

describe.only("should validate @QueryParams input", () => {

    class UserQuery {

        @MinLength(5)
        author: string;

        @MinLength(5)
        contributor: string;

        hello() {
            return "Hello world!";
        }
    }

    let requestQuery: UserQuery;
    beforeEach(() => {
        requestQuery = undefined;
    });

    before(() => {
        getMetadataArgsStorage().reset();

        @JsonController()
        class UserController {

            @Get("/github")
            getUsers(@QueryParams() query: UserQuery): any {
                requestQuery = query;
                return query.hello();
            }

        }
    });

    const options: RoutingControllersOptions = {
        validation: true
    };

    let expressApp: any, koaApp: any;
    before(done => expressApp = createExpressServer(options).listen(3001, done));
    after(done => expressApp.close(done));
    before(done => koaApp = createKoaServer(options).listen(3002, done));
    after(done => koaApp.close(done));

    assertRequest([3001], "get", "github?author=Umed&contributor=19majkel94&somethingPrivate=blablabla", response => {
        console.log(response.body.errors);
        expect(response).to.have.status(400);
        expect(response.body.errors).to.have.lengthOf(1);
        expect(response.body.errors[0].value).to.eql("Umed");
    });
});

But correct errors are returned in response only in express - in koa I get this in errors property:

TypeError: Cannot read property 'prototype' of undefined
       at E:\#Programowanie\#GitHub\routing-controllers\src\metadata\MetadataStorage.ts:79:36
       at Array.filter (native)
       at MetadataStorage.getTargetValidationMetadatas (E:\#Programowanie\#GitHub\routing-controllers\src\metadata\MetadataStorage.ts:75:61)
       at ValidationExecutor.execute (E:\#Programowanie\#GitHub\routing-controllers\src\validation\ValidationExecutor.ts:44:54)
       at Validator.coreValidate (E:\#Programowanie\#GitHub\routing-controllers\src\validation\Validator.ts:31:18)
       at Validator.<anonymous> (E:\#Programowanie\#GitHub\routing-controllers\src\validation\Validator.ts:73:35)
       at step (E:\#Programowanie\#GitHub\routing-controllers\node_modules\class-validator\validation\Validator.js:32:23)
       at Object.next (E:\#Programowanie\#GitHub\routing-controllers\node_modules\class-validator\validation\Validator.js:13:53)
       at E:\#Programowanie\#GitHub\routing-controllers\node_modules\class-validator\validation\Validator.js:7:71
       at Promise (<anonymous>) }

I will try to fix it and implement the ParamOptions feature 馃槈

Waiting for this feature :)

Closing this via #289. Will be released in 0.8.0-beta.1 - routing-controllers@next, stay tuned! 馃槈

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

humbertowoody picture humbertowoody  路  4Comments

ghost picture ghost  路  5Comments

d-bechtel picture d-bechtel  路  6Comments

AleskiWeb picture AleskiWeb  路  4Comments

szabobar picture szabobar  路  4Comments