Skip to content

Commit 3fd15c4

Browse files
rubiinvlapo
authored andcommitted
feat: add isLatLong, isLatitude, isLongtitude validators (#427)
Close #415
1 parent 5bb704e commit 3fd15c4

File tree

5 files changed

+152
-1
lines changed

5 files changed

+152
-1
lines changed

Diff for: README.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -922,7 +922,10 @@ validator.isInstance(value, target); // Checks value is an instance of the targe
922922
| `@IsISIN()` | Checks if the string is an ISIN (stock/security identifier). |
923923
| `@IsISO8601()` | Checks if the string is a valid ISO 8601 date. |
924924
| `@IsJSON()` | Checks if the string is valid JSON. |
925-
| `@IsLowercase()` | Checks if the string is lowercase. |
925+
| `@IsLowercase()` | Checks if the string is lowercase.
926+
| `@IsLatLong()` | check if the string is a valid latitude-longitude coordinate in the format lat,long
927+
| `@IsLatitude()` | check if the string or number is a valid latitude coordinate
928+
| `@IsLongitude()` | check if the string or number is a valid longitude coordinate
926929
| `@IsMobilePhone(locale: string)` | Checks if the string is a mobile phone number. |
927930
| `@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. |
928931
| `@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. |

Diff for: src/decorator/decorators.ts

+45
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,51 @@ export function IsBoolean(validationOptions?: ValidationOptions) {
260260
};
261261
}
262262

263+
/**
264+
* Checks if a value is a latitude,longitude.
265+
*/
266+
export function IsLatLong(validationOptions?: ValidationOptions) {
267+
return function (object: Object, propertyName: string) {
268+
const args: ValidationMetadataArgs = {
269+
type: ValidationTypes.IS_LATLONG,
270+
target: object.constructor,
271+
propertyName: propertyName,
272+
validationOptions: validationOptions
273+
};
274+
getFromContainer(MetadataStorage).addValidationMetadata(new ValidationMetadata(args));
275+
};
276+
}
277+
278+
/**
279+
* Checks if a value is a latitude,longitude.
280+
*/
281+
export function IsLatitude(validationOptions?: ValidationOptions) {
282+
return function (object: Object, propertyName: string) {
283+
const args: ValidationMetadataArgs = {
284+
type: ValidationTypes.IS_LONGITUDE,
285+
target: object.constructor,
286+
propertyName: propertyName,
287+
validationOptions: validationOptions
288+
};
289+
getFromContainer(MetadataStorage).addValidationMetadata(new ValidationMetadata(args));
290+
};
291+
}
292+
293+
/**
294+
* Checks if a value is a latitude,longitude.
295+
*/
296+
export function IsLongitude(validationOptions?: ValidationOptions) {
297+
return function (object: Object, propertyName: string) {
298+
const args: ValidationMetadataArgs = {
299+
type: ValidationTypes.IS_LATITUDE,
300+
target: object.constructor,
301+
propertyName: propertyName,
302+
validationOptions: validationOptions
303+
};
304+
getFromContainer(MetadataStorage).addValidationMetadata(new ValidationMetadata(args));
305+
};
306+
}
307+
263308
/**
264309
* Checks if a value is a date.
265310
*/

Diff for: src/validation/ValidationTypes.ts

+9
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ export class ValidationTypes {
2525
static IS_BOOLEAN = "isBoolean";
2626
static IS_DATE = "isDate";
2727
static IS_NUMBER = "isNumber";
28+
static IS_LATLONG = "isLatLong";
29+
static IS_LATITUDE = "isLatitude";
30+
static IS_LONGITUDE = "isLongitude";
2831
static IS_STRING = "isString";
2932
static IS_DATE_STRING = "isDateString";
3033
static IS_ARRAY = "isArray";
@@ -232,6 +235,12 @@ export class ValidationTypes {
232235
return eachPrefix + "$property must be a valid ISO31661 Alpha2 code";
233236
case this.IS_ISO31661_ALPHA_3:
234237
return eachPrefix + "$property must be a valid ISO31661 Alpha3 code";
238+
case this.IS_LATLONG:
239+
return eachPrefix + "$property must be a latitude,longitude string";
240+
case this.IS_LATITUDE:
241+
return eachPrefix + "$property must be a latitude string or number";
242+
case this.IS_LONGITUDE:
243+
return eachPrefix + "$property must be a longitude string or number";
235244
case this.IS_MONGO_ID:
236245
return eachPrefix + "$property must be a mongodb id";
237246
case this.IS_MULTIBYTE:

Diff for: src/validation/Validator.ts

+29
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,12 @@ export class Validator {
127127
return this.isNotIn(value, metadata.constraints[0]);
128128

129129
/* type checkers */
130+
case ValidationTypes.IS_LATLONG:
131+
return this.isLatLong(value);
132+
case ValidationTypes.IS_LATITUDE:
133+
return this.isLatitude(value);
134+
case ValidationTypes.IS_LONGITUDE:
135+
return this.isLongitude(value);
130136
case ValidationTypes.IS_BOOLEAN:
131137
return this.isBoolean(value);
132138
case ValidationTypes.IS_DATE:
@@ -332,6 +338,29 @@ export class Validator {
332338
return value instanceof Boolean || typeof value === "boolean";
333339
}
334340

341+
342+
/**
343+
* Checks if a given value is a latitude.
344+
*/
345+
isLatLong(value: any): boolean {
346+
347+
return this.validatorJs.isLatLong(value);
348+
}
349+
350+
/**
351+
* Checks if a given value is a latitude.
352+
*/
353+
isLatitude(value: any): boolean {
354+
return (typeof value === "number" || this.isString(value)) && this.isLatLong(`0,${value}`);
355+
}
356+
357+
/**
358+
* Checks if a given value is a longitude.
359+
*/
360+
isLongitude(value: any): boolean {
361+
return (typeof value === "number" || this.isString(value)) && this.isLatLong(`${value},0`);
362+
}
363+
335364
/**
336365
* Checks if a given value is a real date.
337366
*/

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

+65
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ import {expect} from "chai";
33
import {
44
IsBooleanString,
55
IsPositive,
6+
IsLatLong,
7+
IsLongitude,
8+
IsLatitude,
69
IsNegative,
710
Contains,
811
Equals,
@@ -441,6 +444,68 @@ describe("IsBoolean", function() {
441444
checkReturnedError(new MyClass(), invalidValues, validationType, message, done);
442445
});
443446

447+
});
448+
// -------------------------------------------------------------------------
449+
// Specifications: type check
450+
// -------------------------------------------------------------------------
451+
452+
describe("IsLatLong", function () {
453+
454+
const validValues = ["27.6945311,85.3446311", "27.675509,85.2100893"];
455+
const invalidValues = [ "276945311,853446311" , "asas,as.as12" ];
456+
457+
class MyClass {
458+
@IsLatLong()
459+
someProperty: any;
460+
}
461+
462+
it("should not fail if validator.validate said that its valid", function (done) {
463+
checkValidValues(new MyClass(), validValues, done);
464+
});
465+
466+
it("should fail if validator.validate said that its invalid", function (done) {
467+
checkInvalidValues(new MyClass(), invalidValues, done);
468+
});
469+
470+
});
471+
describe("IsLatitude", function () {
472+
473+
const validValues = ["27.6945311", "27.675509", 27.675509];
474+
const invalidValues = ["276945311", "asas", 1234222, 5678921];
475+
476+
class MyClass {
477+
@IsLatitude()
478+
someProperty: any;
479+
}
480+
481+
it("should not fail if validator.validate said that its valid", function (done) {
482+
checkValidValues(new MyClass(), validValues, done);
483+
});
484+
485+
it("should fail if validator.validate said that its invalid", function (done) {
486+
checkInvalidValues(new MyClass(), invalidValues, done);
487+
});
488+
489+
});
490+
491+
describe("IsLongitude", function () {
492+
493+
const validValues = ["85.3446311", "85.2100893", 85.2100893];
494+
const invalidValues = ["853446311", "as.as12", 12345 , 737399];
495+
496+
class MyClass {
497+
@IsLongitude()
498+
someProperty: any;
499+
}
500+
501+
it("should not fail if validator.validate said that its valid", function (done) {
502+
checkValidValues(new MyClass(), validValues, done);
503+
});
504+
505+
it("should fail if validator.validate said that its invalid", function (done) {
506+
checkInvalidValues(new MyClass(), invalidValues, done);
507+
});
508+
444509
});
445510

446511
describe("IsDate", function() {

0 commit comments

Comments
 (0)