Skip to content
Open
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -876,6 +876,7 @@ isBoolean(value);
| `@IsMobilePhone(locale: string)` | Checks if the string is a mobile phone number. |
| `@IsISO31661Alpha2()` | Checks if the string is a valid ISO 3166-1 alpha-2 officially assigned country code. |
| `@IsISO31661Alpha3()` | Checks if the string is a valid ISO 3166-1 alpha-3 officially assigned country code. |
| `@IsISO31661Numeric()` | Checks if the string is a valid ISO 3166-1 numeric officially assigned country code. |
| `@IsLocale()` | Checks if the string is a locale. |
| `@IsPhoneNumber(region: string)` | Checks if the string is a valid phone number using libphonenumber-js. |
| `@IsMongoId()` | Checks if the string is a valid hex-encoded representation of a MongoDB ObjectId. |
Expand Down
1 change: 1 addition & 0 deletions src/decorator/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export * from './string/IsLowercase';
export * from './string/IsMobilePhone';
export * from './string/IsISO31661Alpha2';
export * from './string/IsISO31661Alpha3';
export * from './string/IsISO31661Numeric';
export * from './string/IsMongoId';
export * from './string/IsMultibyte';
export * from './string/IsSurrogatePair';
Expand Down
31 changes: 31 additions & 0 deletions src/decorator/string/IsISO31661Numeric.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { ValidationOptions } from '../ValidationOptions';
import { buildMessage, ValidateBy } from '../common/ValidateBy';
import isISO31661NumericValidator from 'validator/lib/isISO31661Numeric';

export const IS_ISO31661_NUMERIC = 'isISO31661Numeric';

/**
* Check if the string is a valid [ISO 3166-1 numeric](https://en.wikipedia.org/wiki/ISO_3166-1_numeric) officially assigned country code.
*/
export function isISO31661Numeric(value: unknown): boolean {
return typeof value === 'string' && isISO31661NumericValidator(value);
}

/**
* Check if the string is a valid [ISO 3166-1 numeric](https://en.wikipedia.org/wiki/ISO_3166-1_numeric) officially assigned country code.
*/
export function IsISO31661Numeric(validationOptions?: ValidationOptions): PropertyDecorator {
return ValidateBy(
{
name: IS_ISO31661_NUMERIC,
validator: {
validate: (value, args): boolean => isISO31661Numeric(value),
defaultMessage: buildMessage(
eachPrefix => eachPrefix + '$property must be a valid ISO 3166-1 numeric country code',
validationOptions
),
},
},
validationOptions
);
}
18 changes: 18 additions & 0 deletions test/functional/validation-functions-and-decorators.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ import {
IsPhoneNumber,
IsISO31661Alpha2,
IsISO31661Alpha3,
IsISO31661Numeric,
IsHash,
IsMACAddress,
IsISSN,
Expand Down Expand Up @@ -4116,6 +4117,23 @@ describe('IsISO31661Alpha3', () => {
});
});

describe('IsISO31661Numeric', () => {
class MyClass {
@IsISO31661Numeric()
someProperty: string;
}

it('should not fail for a valid ISO 3166-1 numeric country code', () => {
const validValues = ['056', '208', '276', '528', '804'];
return checkValidValues(new MyClass(), validValues);
});

it('should fail for invalid values', () => {
const invalidValues = [undefined, null, '', 'NL', 'NLD', '42', '000', '999'];
return checkInvalidValues(new MyClass(), invalidValues);
});
});

describe('isHash', () => {
function testHash(algorithm: ValidatorJS.HashAlgorithm, validValues: any[], invalidValues: any[]): void {
class MyClass {
Expand Down