Skip to content

Commit

Permalink
feat: add isPort decorator (#282)
Browse files Browse the repository at this point in the history
  • Loading branch information
henrikra authored and vlapo committed Jul 24, 2019
1 parent 5957730 commit 36684ec
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 11 deletions.
20 changes: 18 additions & 2 deletions src/decorator/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -770,7 +770,7 @@ export function IsHexadecimal(validationOptions?: ValidationOptions) {
/**
* Checks if the string is an IP (version 4 or 6).
*/
export function IsIP(version?: "4"|"6", validationOptions?: ValidationOptions) {
export function IsIP(version?: number, validationOptions?: ValidationOptions) {
return function (object: Object, propertyName: string) {
const args: ValidationMetadataArgs = {
type: ValidationTypes.IS_IP,
Expand All @@ -783,10 +783,26 @@ export function IsIP(version?: "4"|"6", validationOptions?: ValidationOptions) {
};
}


/**
* Check if the string is a valid port number.
*/
export function IsPort(validationOptions?: ValidationOptions) {
return function (object: Object, propertyName: string) {
const args: ValidationMetadataArgs = {
type: ValidationTypes.IS_PORT,
target: object.constructor,
propertyName: propertyName,
validationOptions: validationOptions
};
getFromContainer(MetadataStorage).addValidationMetadata(new ValidationMetadata(args));
};
}

/**
* Checks if the string is an ISBN (version 10 or 13).
*/
export function IsISBN(version?: "10"|"13", validationOptions?: ValidationOptions) {
export function IsISBN(version?: number, validationOptions?: ValidationOptions) {
return function (object: Object, propertyName: string) {
const args: ValidationMetadataArgs = {
type: ValidationTypes.IS_ISBN,
Expand Down
1 change: 1 addition & 0 deletions src/validation/ValidationTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export class ValidationTypes {
static IS_HEX_COLOR = "isHexColor";
static IS_HEXADECIMAL = "isHexadecimal";
static IS_IP = "isIp";
static IS_PORT = "isPort";
static IS_ISBN = "isIsbn";
static IS_ISIN = "isIsin";
static IS_ISO8601 = "isIso8601";
Expand Down
16 changes: 13 additions & 3 deletions src/validation/Validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {IsNumberOptions} from "./ValidationTypeOptions";
import {ValidatorOptions} from "./ValidatorOptions";
import {ValidationExecutor} from "./ValidationExecutor";
import {ValidationOptions} from "../decorator/ValidationOptions";
import * as validator from "validator";

/**
* Validator performs validation of the given object based on its metadata.
Expand All @@ -15,7 +16,7 @@ export class Validator {
// Private Properties
// -------------------------------------------------------------------------

private validatorJs = require("validator");
private validatorJs = validator;
private libPhoneNumber = {
phoneUtil: require("google-libphonenumber").PhoneNumberUtil.getInstance(),
};
Expand Down Expand Up @@ -204,6 +205,8 @@ export class Validator {
return this.isHexadecimal(value);
case ValidationTypes.IS_IP:
return this.isIP(value, metadata.constraints[0]);
case ValidationTypes.IS_PORT:
return this.isPort(value);
case ValidationTypes.IS_ISBN:
return this.isISBN(value, metadata.constraints[0]);
case ValidationTypes.IS_ISIN:
Expand Down Expand Up @@ -613,15 +616,22 @@ export class Validator {
* Checks if the string is an IP (version 4 or 6).
* If given value is not a string, then it returns false.
*/
isIP(value: string, version?: "4"|"6"): boolean {
isIP(value: string, version?: number): boolean {
return typeof value === "string" && this.validatorJs.isIP(value, version);
}

/**
* Check if the string is a valid port number.
*/
isPort(value: string): boolean {
return this.validatorJs.isPort(value);
}

/**
* Checks if the string is an ISBN (version 10 or 13).
* If given value is not a string, then it returns false.
*/
isISBN(value: string, version?: "10"|"13"): boolean {
isISBN(value: string, version?: number): boolean {
return typeof value === "string" && this.validatorJs.isISBN(value, version);
}

Expand Down
12 changes: 6 additions & 6 deletions test/functional/validation-functions-and-decorators.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2027,7 +2027,7 @@ describe("IsISBN version 10", function() {
];

class MyClass {
@IsISBN("10")
@IsISBN(10)
someProperty: string;
}

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

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

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

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

class MyClass {
@IsISBN("13")
@IsISBN(13)
someProperty: string;
}

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

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

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

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

0 comments on commit 36684ec

Please sign in to comment.