Skip to content

Commit 36684ec

Browse files
henrikravlapo
authored andcommitted
feat: add isPort decorator (#282)
1 parent 5957730 commit 36684ec

File tree

4 files changed

+38
-11
lines changed

4 files changed

+38
-11
lines changed

Diff for: src/decorator/decorators.ts

+18-2
Original file line numberDiff line numberDiff line change
@@ -770,7 +770,7 @@ export function IsHexadecimal(validationOptions?: ValidationOptions) {
770770
/**
771771
* Checks if the string is an IP (version 4 or 6).
772772
*/
773-
export function IsIP(version?: "4"|"6", validationOptions?: ValidationOptions) {
773+
export function IsIP(version?: number, validationOptions?: ValidationOptions) {
774774
return function (object: Object, propertyName: string) {
775775
const args: ValidationMetadataArgs = {
776776
type: ValidationTypes.IS_IP,
@@ -783,10 +783,26 @@ export function IsIP(version?: "4"|"6", validationOptions?: ValidationOptions) {
783783
};
784784
}
785785

786+
787+
/**
788+
* Check if the string is a valid port number.
789+
*/
790+
export function IsPort(validationOptions?: ValidationOptions) {
791+
return function (object: Object, propertyName: string) {
792+
const args: ValidationMetadataArgs = {
793+
type: ValidationTypes.IS_PORT,
794+
target: object.constructor,
795+
propertyName: propertyName,
796+
validationOptions: validationOptions
797+
};
798+
getFromContainer(MetadataStorage).addValidationMetadata(new ValidationMetadata(args));
799+
};
800+
}
801+
786802
/**
787803
* Checks if the string is an ISBN (version 10 or 13).
788804
*/
789-
export function IsISBN(version?: "10"|"13", validationOptions?: ValidationOptions) {
805+
export function IsISBN(version?: number, validationOptions?: ValidationOptions) {
790806
return function (object: Object, propertyName: string) {
791807
const args: ValidationMetadataArgs = {
792808
type: ValidationTypes.IS_ISBN,

Diff for: src/validation/ValidationTypes.ts

+1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ export class ValidationTypes {
6464
static IS_HEX_COLOR = "isHexColor";
6565
static IS_HEXADECIMAL = "isHexadecimal";
6666
static IS_IP = "isIp";
67+
static IS_PORT = "isPort";
6768
static IS_ISBN = "isIsbn";
6869
static IS_ISIN = "isIsin";
6970
static IS_ISO8601 = "isIso8601";

Diff for: src/validation/Validator.ts

+13-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {IsNumberOptions} from "./ValidationTypeOptions";
55
import {ValidatorOptions} from "./ValidatorOptions";
66
import {ValidationExecutor} from "./ValidationExecutor";
77
import {ValidationOptions} from "../decorator/ValidationOptions";
8+
import * as validator from "validator";
89

910
/**
1011
* Validator performs validation of the given object based on its metadata.
@@ -15,7 +16,7 @@ export class Validator {
1516
// Private Properties
1617
// -------------------------------------------------------------------------
1718

18-
private validatorJs = require("validator");
19+
private validatorJs = validator;
1920
private libPhoneNumber = {
2021
phoneUtil: require("google-libphonenumber").PhoneNumberUtil.getInstance(),
2122
};
@@ -204,6 +205,8 @@ export class Validator {
204205
return this.isHexadecimal(value);
205206
case ValidationTypes.IS_IP:
206207
return this.isIP(value, metadata.constraints[0]);
208+
case ValidationTypes.IS_PORT:
209+
return this.isPort(value);
207210
case ValidationTypes.IS_ISBN:
208211
return this.isISBN(value, metadata.constraints[0]);
209212
case ValidationTypes.IS_ISIN:
@@ -613,15 +616,22 @@ export class Validator {
613616
* Checks if the string is an IP (version 4 or 6).
614617
* If given value is not a string, then it returns false.
615618
*/
616-
isIP(value: string, version?: "4"|"6"): boolean {
619+
isIP(value: string, version?: number): boolean {
617620
return typeof value === "string" && this.validatorJs.isIP(value, version);
618621
}
619622

623+
/**
624+
* Check if the string is a valid port number.
625+
*/
626+
isPort(value: string): boolean {
627+
return this.validatorJs.isPort(value);
628+
}
629+
620630
/**
621631
* Checks if the string is an ISBN (version 10 or 13).
622632
* If given value is not a string, then it returns false.
623633
*/
624-
isISBN(value: string, version?: "10"|"13"): boolean {
634+
isISBN(value: string, version?: number): boolean {
625635
return typeof value === "string" && this.validatorJs.isISBN(value, version);
626636
}
627637

Diff for: test/functional/validation-functions-and-decorators.spec.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -2027,7 +2027,7 @@ describe("IsISBN version 10", function() {
20272027
];
20282028

20292029
class MyClass {
2030-
@IsISBN("10")
2030+
@IsISBN(10)
20312031
someProperty: string;
20322032
}
20332033

@@ -2040,11 +2040,11 @@ describe("IsISBN version 10", function() {
20402040
});
20412041

20422042
it("should not fail if method in validator said that its valid", function() {
2043-
validValues.forEach(value => validator.isISBN(value, "10").should.be.true);
2043+
validValues.forEach(value => validator.isISBN(value, 10).should.be.true);
20442044
});
20452045

20462046
it("should fail if method in validator said that its invalid", function() {
2047-
invalidValues.forEach(value => validator.isISBN(value, "10").should.be.false);
2047+
invalidValues.forEach(value => validator.isISBN(value, 10).should.be.false);
20482048
});
20492049

20502050
it("should return error object with proper data", function(done) {
@@ -2069,7 +2069,7 @@ describe("IsISBN version 13", function() {
20692069
];
20702070

20712071
class MyClass {
2072-
@IsISBN("13")
2072+
@IsISBN(13)
20732073
someProperty: string;
20742074
}
20752075

@@ -2082,11 +2082,11 @@ describe("IsISBN version 13", function() {
20822082
});
20832083

20842084
it("should not fail if method in validator said that its valid", function() {
2085-
validValues.forEach(value => validator.isISBN(value, "13").should.be.true);
2085+
validValues.forEach(value => validator.isISBN(value, 13).should.be.true);
20862086
});
20872087

20882088
it("should fail if method in validator said that its invalid", function() {
2089-
invalidValues.forEach(value => validator.isISBN(value, "13").should.be.false);
2089+
invalidValues.forEach(value => validator.isISBN(value, 13).should.be.false);
20902090
});
20912091

20922092
it("should return error object with proper data", function(done) {

0 commit comments

Comments
 (0)