Skip to content

Commit

Permalink
feat(isLocale): validate isLocale
Browse files Browse the repository at this point in the history
- it validates different languages for different geographical location
- Closes validatorjs#954
  • Loading branch information
ezkemboi committed Feb 14, 2020
1 parent 91c2591 commit ca06b5d
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ Validator | Description
**isJWT(str)** | check if the string is valid JWT token.
**isLatLong(str)**                     | check if the string is a valid latitude-longitude coordinate in the format `lat,long` or `lat, long`.
**isLength(str [, options])** | check if the string's length falls in a range.<br/><br/>`options` is an object which defaults to `{min:0, max: undefined}`. Note: this function takes into account surrogate pairs.
**isLocale(str)** | check if the string is a locale
**isLowercase(str)** | check if the string is lowercase.
**isMACAddress(str)** | check if the string is a MAC address.<br/><br/>`options` is an object which defaults to `{no_colons: false}`. If `no_colons` is true, the validator will allow MAC addresses without the colons. Also, it allows the use of hyphens or spaces e.g '01 02 03 04 05 ab' or '01-02-03-04-05-ab'.
**isMagnetURI(str)** | check if the string is a [magnet uri format](https://en.wikipedia.org/wiki/Magnet_URI_scheme).
Expand Down
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import isIPRange from './lib/isIPRange';
import isFQDN from './lib/isFQDN';

import isBoolean from './lib/isBoolean';
import isLocale from './lib/isLocale';

import isAlpha, { locales as isAlphaLocales } from './lib/isAlpha';
import isAlphanumeric, { locales as isAlphanumericLocales } from './lib/isAlphanumeric';
Expand Down Expand Up @@ -157,6 +158,7 @@ const validator = {
isJSON,
isEmpty,
isLength,
isLocale,
isByteLength,
isUUID,
isMongoId,
Expand Down
11 changes: 11 additions & 0 deletions src/lib/isLocale.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import assertString from './util/assertString';

const localeReg = /^[A-z]{2,4}([_-]([A-z]{4}|[\d]{3}))?([_-]([A-z]{2}|[\d]{3}))?$/;

export default function isLocale(str) {
assertString(str);
if (str === 'en_US_POSIX' || str === 'ca_ES_VALENCIA') {
return true;
}
return localeReg.test(str);
}
21 changes: 21 additions & 0 deletions test/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -2939,6 +2939,27 @@ describe('Validators', () => {
});
});

it('should validate isLocale codes', () => {
test({
validator: 'isLocale',
valid: [
'uz_Latn_UZ',
'en',
'gsw',
'es_ES',
'sw_KE',
'am_ET',
'ca_ES_VALENCIA',
'en_US_POSIX',
],
invalid: [
'lo_POP',
'12',
'12_DD',
],
});
});

it('should validate strings by byte length (deprecated api)', () => {
test({
validator: 'isByteLength',
Expand Down

0 comments on commit ca06b5d

Please sign in to comment.