Routing-controllers: Should @BodyParam default to required?

Created on 14 Jun 2017  路  9Comments  路  Source: typestack/routing-controllers

Taking a stab at rebuilding a Ruby/Grape API with Koa2 and this library, extremely happy that it exists. Thank you for your work!

Something I noticed is that that Params specified in a request function are not required by default. Reading this signature, I'd expect it to raise an error if any fields are missing in my POST:

@Post('/buzz')
createSession(@BodyParam('foo') foo: string, @BodyParam('bar') bar: string, @BodyParam('baz') baz: string) { 
  // do something
}

I see I can add the { required: true } option, but I feel like the less surprising default would be a closer to match to what the TS function signature describes, which is "everything is required." As it stands, the default forces us to treat each param as optional, so the TS signature does not accurately describe the input. Changing the default would make things safer and more trustworthy by default, which is a better user experience, IMO.

All 9 comments

Exactly this's what I feel, e.g. The current user decorator also requires to put required: true. What if we could just use TS signature.

@BruceHem there's no way to use the reflection metadata to determine whether a parameter is required or not. That information is not emitted by the TS compiler.

Unfortunately as @marshall007 told we cannot use that information because typescript does not emit this information. We should ask typescript team to improve their reflection mechanizm and emit more data about code.

New default option allows paramOptions to be required by default

useExpressServer(app, {

    defaults    : {
        paramOptions: {
            required: true // this means that all parameters (@BodyParam, @Param etc in Actions) are required by default
        }
    }
}

Very good.

@IAMtheIAM, but it will be released in 0.7.3 so it cannot be used right now.

@NoNameProvided it is released in 0.7.2, I've checked it.

@NoNameProvided Yes confirmed, it is working in 0.7.2

Ahh ok, thanks for the correction, I must have mess up something during the update of my test project.

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

szabobar picture szabobar  路  4Comments

azaslonov picture azaslonov  路  5Comments

impzero picture impzero  路  4Comments

dcbartlett picture dcbartlett  路  3Comments

AleskiWeb picture AleskiWeb  路  4Comments