diff --git a/README.md b/README.md index 6cef21536..01989849e 100644 --- a/README.md +++ b/README.md @@ -918,6 +918,7 @@ isBoolean(value); | `@IsLatitude()` | Checks if the string or number is a valid latitude coordinate. | | `@IsLongitude()` | Checks if the string or number is a valid longitude coordinate. | | `@IsMobilePhone(locale: string)` | Checks if the string is a mobile phone number. | +| `@IsISO6391()` | Checks if the string is a valid ISO 639-1 officially assigned language code. | | `@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. | diff --git a/src/decorator/decorators.ts b/src/decorator/decorators.ts index 7a756fa44..3662e973b 100644 --- a/src/decorator/decorators.ts +++ b/src/decorator/decorators.ts @@ -117,6 +117,7 @@ export * from './string/IsTimeZone'; export * from './string/IsBase58'; export * from './string/is-tax-id'; export * from './string/is-iso4217-currency-code'; +export * from './string/isISO6391'; // ------------------------------------------------------------------------- // Type checkers diff --git a/src/decorator/string/isISO6391.ts b/src/decorator/string/isISO6391.ts new file mode 100644 index 000000000..514e5443f --- /dev/null +++ b/src/decorator/string/isISO6391.ts @@ -0,0 +1,31 @@ +import { ValidationOptions } from '../ValidationOptions'; +import { buildMessage, ValidateBy } from '../common/ValidateBy'; +import isISO6391Validator from 'validator/lib/isISO6391'; + +export const IS_ISO6391 = 'isISO6391'; + +/** + * Check if the string is a valid [ISO 639-1](https://en.wikipedia.org/wiki/ISO_639-1) officially assigned language code. + */ +export function isISO6391(value: unknown): boolean { + return typeof value === 'string' && isISO6391Validator(value); +} + +/** + * Check if the string is a valid [ISO 639-1](https://en.wikipedia.org/wiki/ISO_639-1) officially assigned language code. + */ +export function IsISO6391(validationOptions?: ValidationOptions): PropertyDecorator { + return ValidateBy( + { + name: IS_ISO6391, + validator: { + validate: (value, args): boolean => isISO6391(value), + defaultMessage: buildMessage( + eachPrefix => eachPrefix + '$property must be a valid ISO 639-1 language code', + validationOptions + ), + }, + }, + validationOptions + ); +} diff --git a/test/functional/validation-functions-and-decorators.spec.ts b/test/functional/validation-functions-and-decorators.spec.ts index 390d1a4fa..4c266f02e 100644 --- a/test/functional/validation-functions-and-decorators.spec.ts +++ b/test/functional/validation-functions-and-decorators.spec.ts @@ -194,6 +194,7 @@ import { isTaxId, IsTaxId, IsISO4217CurrencyCode, + IsISO6391, } from '../../src/decorator/decorators'; import { Validator } from '../../src/validation/Validator'; import { ValidatorOptions } from '../../src/validation/ValidatorOptions'; @@ -5244,3 +5245,20 @@ describe('IsISO4217', () => { return checkInvalidValues(new MyClass(), invalidValues); }); }); + +describe('IsISO6391', () => { + class MyClass { + @IsISO6391() + someProperty: string; + } + + it('should not fail for a valid ISO 639-1 language code', () => { + const validValues = ['de', 'en', 'eo', 'fy', 'nl']; + return checkValidValues(new MyClass(), validValues); + }); + + it('should fail for invalid values', () => { + const invalidValues = [undefined, null, '', 'FR', 'xx', 'tok']; + return checkInvalidValues(new MyClass(), invalidValues); + }); +});