Skip to content

Commit 510ac92

Browse files
committed
feat: Added a ISO639-1 decorator
This decorator is used to check if a string matches the ISO 639-1, that describes how country languages prefixes should look like. Fortunatelly, it just delegates the validation to an already existent function in the validator library
1 parent 9f26bad commit 510ac92

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

src/decorator/decorators.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ export * from './string/IsIP';
6767
export * from './string/IsPort';
6868
export * from './string/IsISBN';
6969
export * from './string/IsISIN';
70+
export * from './string/IsISO6391';
7071
export * from './string/IsISO8601';
7172
export * from './string/IsJSON';
7273
export * from './string/IsJWT';

src/decorator/string/IsISO6391.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { ValidationOptions } from '../ValidationOptions';
2+
import { buildMessage, ValidateBy } from '../common/ValidateBy';
3+
import isISO6391 from 'validator/lib/isISO6391';
4+
5+
export const IS_ISO6391 = 'isISO6391';
6+
7+
/**
8+
* Checks if the string is an ISO 639-1 (the country language code).
9+
* If given value is not a string, then it returns false.
10+
*/
11+
export function isIso6391(value: unknown): boolean {
12+
return typeof value === 'string' && isISO6391(value);
13+
}
14+
15+
/**
16+
* Checks if the string is an ISIN (stock/security identifier).
17+
* If given value is not a string, then it returns false.
18+
*/
19+
export function IsISO6391(validationOptions?: ValidationOptions): PropertyDecorator {
20+
return ValidateBy(
21+
{
22+
name: IS_ISO6391,
23+
validator: {
24+
validate: (value, args): boolean => isIso6391(value),
25+
defaultMessage: buildMessage(
26+
eachPrefix => eachPrefix + '$property must be an ISO 639-1 (the country language code)',
27+
validationOptions
28+
),
29+
},
30+
},
31+
validationOptions
32+
);
33+
}

0 commit comments

Comments
 (0)