Comments (13)
With example app for @Query('q') q: string | undefined
I receive the following:
{
"message": "Invalid param “q”. “String” expected, “undefined” received"
}
Could you show your tsconfig.json?
from node-decorators.
{
"compilerOptions": {
"allowJs": true,
"allowSyntheticDefaultImports": true,
"baseUrl": ".",
"emitDecoratorMetadata": true,
"esModuleInterop": true,
"experimentalDecorators": true,
"forceConsistentCasingInFileNames": true,
"importsNotUsedAsValues": "error",
"lib": ["ESNext", "DOM"],
"module": "CommonJS",
"moduleResolution": "Node16",
"noFallthroughCasesInSwitch": true,
"noImplicitAny": true,
"noImplicitThis": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"outDir": "./dist",
"removeComments": true,
"resolveJsonModule": true,
"skipLibCheck": true,
"strict": true,
"target": "ESNext",
"useDefineForClassFields": true,
"strictPropertyInitialization": false
},
"include": ["src/*.ts", "src/**/*.ts"],
"ts-node": {
"files": true
}
}
from node-decorators.
With your setup I managed to get expected result:
{
"message": "Invalid param “q”. “String” expected, “undefined” received"
}
The only thing I've changed is strict: false
and noImplicitAny: false
from node-decorators.
It should be allowed for the query params to be undefined when loading the page instead of throwing an error.
from node-decorators.
Agree, I thought Object
was the problem. I wonder if there's a way to detect "optionals". TS returns String
for combination of string | undefined
. Probably I'll remove simple validation since I don't have enough info to perform proper validation.
from node-decorators.
NestJS has a good implementation for parameter validation.
from node-decorators.
It uses class-validator under the hood as well. I've added also simple validation of standard types. But it feels like it won't work for complex scenarios such as string | undefined
or Promise<string ....>
. Reflect for string | undefined
returns String
as design:paramtypes
. And system couldn't verify the param.
from node-decorators.
It could work if the validation function stops throwing on undefined
from node-decorators.
I thought to add a custom validator for param decorators, for instance:
@Query('q', String) query: string | undefined
or
const qValidator = (q) => typeof q === string || q === undefined;
...
@Query('q', qValidator) query: string | undefined,
from node-decorators.
const qValidator = (q) => typeof q === string || q === undefined;
...
@Query('q', qValidator) query: string | undefined,
This is a good option since it's more flexible.
from node-decorators.
I think these options will be supported:
@Body() user: User
- if User is a class and has metadata (for class-validator)@Query('q', Type) query: string | undefined
- Type can be both standard and custom (for class-validator)@Query('q', validatorFunction) query: string | undefined
- for very custom-like validation
from node-decorators.
Looks good. The issue still remains for @Params()
. Currently I have @Get('/:id/:subpage?')
and I get the same error because of the validation, so it should be considered too.
from node-decorators.
Yup, that's the same mechanism.
from node-decorators.
Related Issues (20)
- Decorators with inheritance HOT 4
- Error while inject property in middelwares HOT 6
- Error injection HOT 18
- Where is attachMiddleware o how it change? HOT 2
- Where did the flexibility go?! HOT 11
- @Controller & Rest methods no longer work HOT 1
- Can't attach a controller without an error middleware HOT 17
- HttpModule is not exported HOT 21
- express-session is causing ERR_HTTP_HEADERS_SENT HOT 18
- Decorator error handling
- Missing data, EJS, express-ejs-layouts
- Implement OpenAPI nullable schema property
- Which container instance should I use ? HOT 2
- elysia Js HOT 3
- [BUG] Inject() and Injectable() not working as expected - upgrade 2.x -> 3.x HOT 4
- [BUG] dependency injection constructor service initialize fail HOT 1
- Middleware called from another controller [BUG]
- [BUG] Error: ENOENT: no such file or directory
- [FEAT] Enable auth token globally in swagger - @decorators/server
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from node-decorators.