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 @@ -874,6 +874,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. |
| `@IsLocale()` | Checks if the string is a locale. |
Expand Down
31 changes: 31 additions & 0 deletions src/decorator/string/isISO6391.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 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
);
}
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 @@ -193,6 +193,7 @@ import {
isTaxId,
IsTaxId,
IsISO4217CurrencyCode,
IsISO6391,
} from '../../src/decorator/decorators';
import { Validator } from '../../src/validation/Validator';
import { ValidatorOptions } from '../../src/validation/ValidatorOptions';
Expand Down Expand Up @@ -4789,3 +4790,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);
});
});