question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

feat: IsPort decorator should accept number value

See original GitHub issue

I was trying to…

class Config {
  @IsPort()
  port: number;
}
const c = new Config();
c.port = 3000;
console.log(validateSync(c)) // error occurred

The problem: validateSync returns an error in the above snippet. The error doesn’t appear when port type is string. Is this behavior intended? https://github.com/typestack/class-validator/blob/60f72a829da5b7669b852b96e4764ed525d772c3/src/decorator/string/IsPort.ts#L11

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:15 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
hungtcscommented, Sep 9, 2022

I added such a custom decorator to my project:

import { buildMessage, isPort, ValidateBy, ValidationOptions } from 'class-validator';

export const IS_PORT_NUMBER = 'isPortNumber';

export function isPortNumber(value: unknown): boolean {
  return typeof value === 'number' && isPort(`${ value }`);
}

export function IsPortNumber(validationOptions?: ValidationOptions): PropertyDecorator {
  return ValidateBy(
    {
      name: IS_PORT_NUMBER,
      constraints: [],
      validator: {
        validate: (value): boolean => {
          return isPortNumber(value);
        },
        defaultMessage: buildMessage(eachPrefix => eachPrefix + '$property must be a valid port number', validationOptions),
      },
    },
    validationOptions
  );
}
2reactions
NoNameProvidedcommented, Feb 16, 2021

I am down for discussion, but still afraid of multiple types. How about accepting only numbers then, does that makes more sense? I am not totally firm on not allowing multiple types, but I would like to see some more feedback on this, before going down this path.

Read more comments on GitHub >

github_iconTop Results From Across the Web

feat: IsPort decorator should accept number value #826 - GitHub
So we would check if the received value is a number and convert it to a string and then pass it to the...
Read more >
feat: IsPort decorator should accept number value -
I was trying to... class Config { @IsPort() port: number; } const c = new Config(); c.port = 3000; console.log(validateSync(c)) // error ...
Read more >
What is Feature Engineering — Importance, Tools and ...
This blog post will explain how feature scaling works and why it's important as well as some tips for getting started with feature...
Read more >
Apple Watch SE: The ultimate combination of design, function ...
Apple Watch SE packs the essential features of Apple Watch into a modern design customers love at an affordable price. Design and Performance....
Read more >
Xbox Design Lab | Xbox
Design your own next-gen Xbox Wireless Controller in the Xbox Design Lab. Choose from a combination of colors, patterns, textured triggers, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found