Skip to content

Commit 32c8eb5

Browse files
Create isISO6391.ts
1 parent fb7b254 commit 32c8eb5

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

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+
}

0 commit comments

Comments
 (0)