Giter Club home page Giter Club logo

Comments (13)

serhiisol avatar serhiisol commented on June 20, 2024

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.

 avatar commented on June 20, 2024
{
  "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.

serhiisol avatar serhiisol commented on June 20, 2024

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.

 avatar commented on June 20, 2024

It should be allowed for the query params to be undefined when loading the page instead of throwing an error.

from node-decorators.

serhiisol avatar serhiisol commented on June 20, 2024

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.

 avatar commented on June 20, 2024

NestJS has a good implementation for parameter validation.

from node-decorators.

serhiisol avatar serhiisol commented on June 20, 2024

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.

 avatar commented on June 20, 2024

It could work if the validation function stops throwing on undefined

from node-decorators.

serhiisol avatar serhiisol commented on June 20, 2024

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.

 avatar commented on June 20, 2024
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.

serhiisol avatar serhiisol commented on June 20, 2024

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.

 avatar commented on June 20, 2024

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.

serhiisol avatar serhiisol commented on June 20, 2024

Yup, that's the same mechanism.

from node-decorators.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.