Skip to content

Commit 55c57b3

Browse files
neilimevlapo
authored andcommitted
feat: add @IsISO31661Alpha3 and @IsISO31661Alpha2 validators (#273)
1 parent 3a0f273 commit 55c57b3

File tree

5 files changed

+103
-1
lines changed

5 files changed

+103
-1
lines changed

Diff for: README.md

+4
Original file line numberDiff line numberDiff line change
@@ -766,6 +766,8 @@ validator.isISO8601(str); // Checks if the string is a valid ISO 8601 date.
766766
validator.isJSON(str); // Checks if the string is valid JSON (note: uses JSON.parse).
767767
validator.isLowercase(str); // Checks if the string is lowercase.
768768
validator.isMobilePhone(str, locale); // Checks if the string is a mobile phone number.
769+
validator.isISO31661Alpha2(str); // Check if the string is a valid ISO 3166-1 alpha-2
770+
validator.isISO31661Alpha3(str); // Check if the string is a valid ISO 3166-1 alpha-3
769771
validator.isPhoneNumber(str, region); // Checks if the string is a valid phone number.
770772
validator.isMongoId(str); // Checks if the string is a valid hex-encoded representation of a MongoDB ObjectId.
771773
validator.isMultibyte(str); // Checks if the string contains one or more multibyte chars.
@@ -849,6 +851,8 @@ validator.isInstance(value, target); // Checks value is an instance of the targe
849851
| `@IsJSON()` | Checks if the string is valid JSON. |
850852
| `@IsLowercase()` | Checks if the string is lowercase. |
851853
| `@IsMobilePhone(locale: string)` | Checks if the string is a mobile phone number. |
854+
| `@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. |
855+
| `@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. |
852856
| `@IsPhoneNumber(region: string)` | Checks if the string is a valid phone number. "region" accepts 2 characters uppercase country code (e.g. DE, US, CH).If users must enter the intl. prefix (e.g. +41), then you may pass "ZZ" or null as region. See [google-libphonenumber, metadata.js:countryCodeToRegionCodeMap on github](https://github.com/ruimarinho/google-libphonenumber/blob/1e46138878cff479aafe2ce62175c6c49cb58720/src/metadata.js#L33) |
853857
| `@IsMongoId()` | Checks if the string is a valid hex-encoded representation of a MongoDB ObjectId. |
854858
| `@IsMultibyte()` | Checks if the string contains one or more multibyte chars. |

Diff for: src/decorator/decorators.ts

+34
Original file line numberDiff line numberDiff line change
@@ -879,6 +879,40 @@ export function IsPhoneNumber(region: string, validationOptions?: ValidationOpti
879879
};
880880
}
881881

882+
/**
883+
* Check if the string is a valid ISO 3166-1 alpha-2.
884+
* See heck if [ISO 3166-1 alpha-2](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2) officially assigned country code.
885+
*/
886+
export function IsISO31661Alpha2(validationOptions?: ValidationOptions) {
887+
return function (object: Object, propertyName: string) {
888+
const args: ValidationMetadataArgs = {
889+
type: ValidationTypes.IS_ISO31661_ALPHA_2,
890+
target: object.constructor,
891+
propertyName: propertyName,
892+
constraints: [],
893+
validationOptions: validationOptions
894+
};
895+
getFromContainer(MetadataStorage).addValidationMetadata(new ValidationMetadata(args));
896+
};
897+
}
898+
899+
/**
900+
* Check if the string is a valid ISO 3166-1 alpha-3.
901+
* See heck if [ISO 3166-1 alpha-3](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-3) officially assigned country code.
902+
*/
903+
export function IsISO31661Alpha3(validationOptions?: ValidationOptions) {
904+
return function (object: Object, propertyName: string) {
905+
const args: ValidationMetadataArgs = {
906+
type: ValidationTypes.IS_ISO31661_ALPHA_3,
907+
target: object.constructor,
908+
propertyName: propertyName,
909+
constraints: [],
910+
validationOptions: validationOptions
911+
};
912+
getFromContainer(MetadataStorage).addValidationMetadata(new ValidationMetadata(args));
913+
};
914+
}
915+
882916
/**
883917
* Checks if the string is a valid hex-encoded representation of a MongoDB ObjectId.
884918
*/

Diff for: src/validation/ValidationTypes.ts

+6
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ export class ValidationTypes {
7070
static IS_LOWERCASE = "isLowercase";
7171
static IS_MOBILE_PHONE = "isMobilePhone";
7272
static IS_PHONE_NUMBER = "isPhoneNumber";
73+
static IS_ISO31661_ALPHA_2 = "isISO31661Alpha2";
74+
static IS_ISO31661_ALPHA_3 = "isISO31661Alpha3";
7375
static IS_MONGO_ID = "isMongoId";
7476
static IS_MULTIBYTE = "isMultibyte";
7577
static IS_SURROGATE_PAIR = "isSurrogatePair";
@@ -219,6 +221,10 @@ export class ValidationTypes {
219221
return eachPrefix + "$property must be a phone number";
220222
case this.IS_PHONE_NUMBER:
221223
return eachPrefix + "$property must be a valid phone number";
224+
case this.IS_ISO31661_ALPHA_2:
225+
return eachPrefix + "$property must be a valid ISO31661 Alpha2 code";
226+
case this.IS_ISO31661_ALPHA_3:
227+
return eachPrefix + "$property must be a valid ISO31661 Alpha3 code";
222228
case this.IS_MONGO_ID:
223229
return eachPrefix + "$property must be a mongodb id";
224230
case this.IS_MULTIBYTE:

Diff for: src/validation/Validator.ts

+18
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,10 @@ export class Validator {
216216
return this.isMobilePhone(value, metadata.constraints[0]);
217217
case ValidationTypes.IS_PHONE_NUMBER:
218218
return this.isPhoneNumber(value, metadata.constraints[0]);
219+
case ValidationTypes.IS_ISO31661_ALPHA_2:
220+
return this.isISO31661Alpha2(value);
221+
case ValidationTypes.IS_ISO31661_ALPHA_3:
222+
return this.isISO31661Alpha3(value);
219223
case ValidationTypes.IS_MONGO_ID:
220224
return this.isMongoId(value);
221225
case ValidationTypes.IS_MULTIBYTE:
@@ -668,6 +672,20 @@ export class Validator {
668672
}
669673
}
670674

675+
/**
676+
* 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.
677+
*/
678+
isISO31661Alpha2(value: string): boolean {
679+
return typeof value === "string" && this.validatorJs.isISO31661Alpha2(value);
680+
}
681+
682+
/**
683+
* 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.
684+
*/
685+
isISO31661Alpha3(value: string): boolean {
686+
return typeof value === "string" && this.validatorJs.isISO31661Alpha3(value);
687+
}
688+
671689
/**
672690
* Checks if the string is a valid hex-encoded representation of a MongoDB ObjectId.
673691
* If given value is not a string, then it returns false.

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

+41-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ import {
6464
IsArray,
6565
IsDateString,
6666
IsInstance,
67-
IsPhoneNumber
67+
IsPhoneNumber,
68+
IsISO31661Alpha2,
69+
IsISO31661Alpha3,
6870
} from "../../src/decorator/decorators";
6971
import {Validator} from "../../src/validation/Validator";
7072
import {ValidatorOptions} from "../../src/validation/ValidatorOptions";
@@ -2899,6 +2901,44 @@ describe("isPhoneNumber", function() {
28992901
});
29002902
});
29012903

2904+
describe("IsISO31661Alpha2", function() {
2905+
2906+
class MyClass {
2907+
@IsISO31661Alpha2()
2908+
someProperty: string;
2909+
}
2910+
2911+
it("should not fail for a valid ISO31661 Alpha2 code", function(done) {
2912+
const validValues = ["AD", "AE", "AF", "AG"];
2913+
checkValidValues(new MyClass(), validValues, done);
2914+
});
2915+
2916+
it("should fail for invalid values", function(done) {
2917+
const invalidValues = [undefined, null, "", "AFR", "aD"];
2918+
checkInvalidValues(new MyClass(), invalidValues, done);
2919+
});
2920+
2921+
});
2922+
2923+
describe("IsISO31661Alpha3", function() {
2924+
2925+
class MyClass {
2926+
@IsISO31661Alpha3()
2927+
someProperty: string;
2928+
}
2929+
2930+
it("should not fail for a valid ISO31661 Alpha3 code", function(done) {
2931+
const validValues = ["ABW", "HND", "KHM", "RWA"];
2932+
checkValidValues(new MyClass(), validValues, done);
2933+
});
2934+
2935+
it("should fail for invalid values", function(done) {
2936+
const invalidValues = [undefined, null, "", "FR", "fR", "GB", "PT", "CM", "JP", "PM", "ZW"];
2937+
checkInvalidValues(new MyClass(), invalidValues, done);
2938+
});
2939+
2940+
});
2941+
29022942

29032943
// -------------------------------------------------------------------------
29042944
// Specifications: array check

0 commit comments

Comments
 (0)