Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add isPort decorator #282

Merged
merged 9 commits into from
Jul 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions src/decorator/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -754,7 +754,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 @@ -767,10 +767,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 @@ -63,6 +63,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;
vlapo marked this conversation as resolved.
Show resolved Hide resolved
private libPhoneNumber = {
phoneUtil: require("google-libphonenumber").PhoneNumberUtil.getInstance(),
};
Expand Down Expand Up @@ -202,6 +203,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 @@ -602,15 +605,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 @@ -1957,7 +1957,7 @@ describe("IsISBN version 10", function() {
];

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

Expand All @@ -1970,11 +1970,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 @@ -1999,7 +1999,7 @@ describe("IsISBN version 13", function() {
];

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

Expand All @@ -2012,11 +2012,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