Skip to content

Commit

Permalink
feat: add isLatLong, isLatitude, isLongtitude validators (#427)
Browse files Browse the repository at this point in the history
Close #415
  • Loading branch information
rubiin authored and vlapo committed Oct 2, 2019
1 parent 5bb704e commit 3fd15c4
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 1 deletion.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -922,7 +922,10 @@ validator.isInstance(value, target); // Checks value is an instance of the targe
| `@IsISIN()` | Checks if the string is an ISIN (stock/security identifier). |
| `@IsISO8601()` | Checks if the string is a valid ISO 8601 date. |
| `@IsJSON()` | Checks if the string is valid JSON. |
| `@IsLowercase()` | Checks if the string is lowercase. |
| `@IsLowercase()` | Checks if the string is lowercase.
| `@IsLatLong()` | check if the string is a valid latitude-longitude coordinate in the format lat,long
| `@IsLatitude()` | check if the string or number is a valid latitude coordinate
| `@IsLongitude()` | check if the string or number is a valid longitude coordinate
| `@IsMobilePhone(locale: string)` | Checks if the string is a mobile phone number. |
| `@IsISO31661Alpha2()` | Check if the string is a valid [ISO 3166-1 alpha-2](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2) officially assigned country code. |
| `@IsISO31661Alpha3()` | Check if the string is a valid [ISO 3166-1 alpha-3](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-3) officially assigned country code. |
Expand Down
45 changes: 45 additions & 0 deletions src/decorator/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,51 @@ export function IsBoolean(validationOptions?: ValidationOptions) {
};
}

/**
* Checks if a value is a latitude,longitude.
*/
export function IsLatLong(validationOptions?: ValidationOptions) {
return function (object: Object, propertyName: string) {
const args: ValidationMetadataArgs = {
type: ValidationTypes.IS_LATLONG,
target: object.constructor,
propertyName: propertyName,
validationOptions: validationOptions
};
getFromContainer(MetadataStorage).addValidationMetadata(new ValidationMetadata(args));
};
}

/**
* Checks if a value is a latitude,longitude.
*/
export function IsLatitude(validationOptions?: ValidationOptions) {
return function (object: Object, propertyName: string) {
const args: ValidationMetadataArgs = {
type: ValidationTypes.IS_LONGITUDE,
target: object.constructor,
propertyName: propertyName,
validationOptions: validationOptions
};
getFromContainer(MetadataStorage).addValidationMetadata(new ValidationMetadata(args));
};
}

/**
* Checks if a value is a latitude,longitude.
*/
export function IsLongitude(validationOptions?: ValidationOptions) {
return function (object: Object, propertyName: string) {
const args: ValidationMetadataArgs = {
type: ValidationTypes.IS_LATITUDE,
target: object.constructor,
propertyName: propertyName,
validationOptions: validationOptions
};
getFromContainer(MetadataStorage).addValidationMetadata(new ValidationMetadata(args));
};
}

/**
* Checks if a value is a date.
*/
Expand Down
9 changes: 9 additions & 0 deletions src/validation/ValidationTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ export class ValidationTypes {
static IS_BOOLEAN = "isBoolean";
static IS_DATE = "isDate";
static IS_NUMBER = "isNumber";
static IS_LATLONG = "isLatLong";
static IS_LATITUDE = "isLatitude";
static IS_LONGITUDE = "isLongitude";
static IS_STRING = "isString";
static IS_DATE_STRING = "isDateString";
static IS_ARRAY = "isArray";
Expand Down Expand Up @@ -232,6 +235,12 @@ export class ValidationTypes {
return eachPrefix + "$property must be a valid ISO31661 Alpha2 code";
case this.IS_ISO31661_ALPHA_3:
return eachPrefix + "$property must be a valid ISO31661 Alpha3 code";
case this.IS_LATLONG:
return eachPrefix + "$property must be a latitude,longitude string";
case this.IS_LATITUDE:
return eachPrefix + "$property must be a latitude string or number";
case this.IS_LONGITUDE:
return eachPrefix + "$property must be a longitude string or number";
case this.IS_MONGO_ID:
return eachPrefix + "$property must be a mongodb id";
case this.IS_MULTIBYTE:
Expand Down
29 changes: 29 additions & 0 deletions src/validation/Validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,12 @@ export class Validator {
return this.isNotIn(value, metadata.constraints[0]);

/* type checkers */
case ValidationTypes.IS_LATLONG:
return this.isLatLong(value);
case ValidationTypes.IS_LATITUDE:
return this.isLatitude(value);
case ValidationTypes.IS_LONGITUDE:
return this.isLongitude(value);
case ValidationTypes.IS_BOOLEAN:
return this.isBoolean(value);
case ValidationTypes.IS_DATE:
Expand Down Expand Up @@ -332,6 +338,29 @@ export class Validator {
return value instanceof Boolean || typeof value === "boolean";
}


/**
* Checks if a given value is a latitude.
*/
isLatLong(value: any): boolean {

return this.validatorJs.isLatLong(value);
}

/**
* Checks if a given value is a latitude.
*/
isLatitude(value: any): boolean {
return (typeof value === "number" || this.isString(value)) && this.isLatLong(`0,${value}`);
}

/**
* Checks if a given value is a longitude.
*/
isLongitude(value: any): boolean {
return (typeof value === "number" || this.isString(value)) && this.isLatLong(`${value},0`);
}

/**
* Checks if a given value is a real date.
*/
Expand Down
65 changes: 65 additions & 0 deletions test/functional/validation-functions-and-decorators.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import {expect} from "chai";
import {
IsBooleanString,
IsPositive,
IsLatLong,
IsLongitude,
IsLatitude,
IsNegative,
Contains,
Equals,
Expand Down Expand Up @@ -441,6 +444,68 @@ describe("IsBoolean", function() {
checkReturnedError(new MyClass(), invalidValues, validationType, message, done);
});

});
// -------------------------------------------------------------------------
// Specifications: type check
// -------------------------------------------------------------------------

describe("IsLatLong", function () {

const validValues = ["27.6945311,85.3446311", "27.675509,85.2100893"];
const invalidValues = [ "276945311,853446311" , "asas,as.as12" ];

class MyClass {
@IsLatLong()
someProperty: any;
}

it("should not fail if validator.validate said that its valid", function (done) {
checkValidValues(new MyClass(), validValues, done);
});

it("should fail if validator.validate said that its invalid", function (done) {
checkInvalidValues(new MyClass(), invalidValues, done);
});

});
describe("IsLatitude", function () {

const validValues = ["27.6945311", "27.675509", 27.675509];
const invalidValues = ["276945311", "asas", 1234222, 5678921];

class MyClass {
@IsLatitude()
someProperty: any;
}

it("should not fail if validator.validate said that its valid", function (done) {
checkValidValues(new MyClass(), validValues, done);
});

it("should fail if validator.validate said that its invalid", function (done) {
checkInvalidValues(new MyClass(), invalidValues, done);
});

});

describe("IsLongitude", function () {

const validValues = ["85.3446311", "85.2100893", 85.2100893];
const invalidValues = ["853446311", "as.as12", 12345 , 737399];

class MyClass {
@IsLongitude()
someProperty: any;
}

it("should not fail if validator.validate said that its valid", function (done) {
checkValidValues(new MyClass(), validValues, done);
});

it("should fail if validator.validate said that its invalid", function (done) {
checkInvalidValues(new MyClass(), invalidValues, done);
});

});

describe("IsDate", function() {
Expand Down

0 comments on commit 3fd15c4

Please sign in to comment.