Skip to content

Commit e0eaa02

Browse files
feat: add isISO6391 decorator for language codes (#2626)
* Create isISO6391.ts * Document `@IsIso6391()` in `README.md` * Add unit tests * Add `isISO6391` to exported decorators --------- Co-authored-by: Brage Sekse Aarset <brage.aarset@gmail.com>
1 parent a8b2f6e commit e0eaa02

4 files changed

Lines changed: 51 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -918,6 +918,7 @@ isBoolean(value);
918918
| `@IsLatitude()` | Checks if the string or number is a valid latitude coordinate. |
919919
| `@IsLongitude()` | Checks if the string or number is a valid longitude coordinate. |
920920
| `@IsMobilePhone(locale: string)` | Checks if the string is a mobile phone number. |
921+
| `@IsISO6391()` | Checks if the string is a valid ISO 639-1 officially assigned language code. |
921922
| `@IsISO31661Alpha2()` | Checks if the string is a valid ISO 3166-1 alpha-2 officially assigned country code. |
922923
| `@IsISO31661Alpha3()` | Checks if the string is a valid ISO 3166-1 alpha-3 officially assigned country code. |
923924
| `@IsISO31661Numeric()` | Checks if the string is a valid ISO 3166-1 numeric officially assigned country code. |

src/decorator/decorators.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ export * from './string/IsTimeZone';
117117
export * from './string/IsBase58';
118118
export * from './string/is-tax-id';
119119
export * from './string/is-iso4217-currency-code';
120+
export * from './string/isISO6391';
120121

121122
// -------------------------------------------------------------------------
122123
// Type checkers

src/decorator/string/isISO6391.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { ValidationOptions } from '../ValidationOptions';
2+
import { buildMessage, ValidateBy } from '../common/ValidateBy';
3+
import isISO6391Validator from 'validator/lib/isISO6391';
4+
5+
export const IS_ISO6391 = 'isISO6391';
6+
7+
/**
8+
* Check if the string is a valid [ISO 639-1](https://en.wikipedia.org/wiki/ISO_639-1) officially assigned language code.
9+
*/
10+
export function isISO6391(value: unknown): boolean {
11+
return typeof value === 'string' && isISO6391Validator(value);
12+
}
13+
14+
/**
15+
* Check if the string is a valid [ISO 639-1](https://en.wikipedia.org/wiki/ISO_639-1) officially assigned language code.
16+
*/
17+
export function IsISO6391(validationOptions?: ValidationOptions): PropertyDecorator {
18+
return ValidateBy(
19+
{
20+
name: IS_ISO6391,
21+
validator: {
22+
validate: (value, args): boolean => isISO6391(value),
23+
defaultMessage: buildMessage(
24+
eachPrefix => eachPrefix + '$property must be a valid ISO 639-1 language code',
25+
validationOptions
26+
),
27+
},
28+
},
29+
validationOptions
30+
);
31+
}

test/functional/validation-functions-and-decorators.spec.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ import {
194194
isTaxId,
195195
IsTaxId,
196196
IsISO4217CurrencyCode,
197+
IsISO6391,
197198
} from '../../src/decorator/decorators';
198199
import { Validator } from '../../src/validation/Validator';
199200
import { ValidatorOptions } from '../../src/validation/ValidatorOptions';
@@ -5244,3 +5245,20 @@ describe('IsISO4217', () => {
52445245
return checkInvalidValues(new MyClass(), invalidValues);
52455246
});
52465247
});
5248+
5249+
describe('IsISO6391', () => {
5250+
class MyClass {
5251+
@IsISO6391()
5252+
someProperty: string;
5253+
}
5254+
5255+
it('should not fail for a valid ISO 639-1 language code', () => {
5256+
const validValues = ['de', 'en', 'eo', 'fy', 'nl'];
5257+
return checkValidValues(new MyClass(), validValues);
5258+
});
5259+
5260+
it('should fail for invalid values', () => {
5261+
const invalidValues = [undefined, null, '', 'FR', 'xx', 'tok'];
5262+
return checkInvalidValues(new MyClass(), invalidValues);
5263+
});
5264+
});

0 commit comments

Comments
 (0)