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 #954
  • Loading branch information
ezkemboi committed Aug 18, 2019
1 parent 23fcd9c commit f41ac00
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,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'.
**isMD5(str)** | check if the string is a MD5 hash.
Expand Down
3 changes: 3 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ var _isFQDN = _interopRequireDefault(require("./lib/isFQDN"));

var _isBoolean = _interopRequireDefault(require("./lib/isBoolean"));

var _isLocale = _interopRequireDefault(require("./lib/isLocale"));

var _isAlpha = _interopRequireWildcard(require("./lib/isAlpha"));

var _isAlphanumeric = _interopRequireWildcard(require("./lib/isAlphanumeric"));
Expand Down Expand Up @@ -203,6 +205,7 @@ var validator = {
isJSON: _isJSON.default,
isEmpty: _isEmpty.default,
isLength: _isLength.default,
isLocale: _isLocale.default,
isByteLength: _isByteLength.default,
isUUID: _isUUID.default,
isMongoId: _isMongoId.default,
Expand Down
20 changes: 20 additions & 0 deletions lib/isLocale.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"use strict";

Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = isLocale;

var _assertString = _interopRequireDefault(require("./util/assertString"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

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

function isLocale(str) {
(0, _assertString.default)(str);
return localeReg.test(str);
}

module.exports = exports.default;
module.exports.default = exports.default;
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 @@ -144,6 +145,7 @@ const validator = {
isJSON,
isEmpty,
isLength,
isLocale,
isByteLength,
isUUID,
isMongoId,
Expand Down
8 changes: 8 additions & 0 deletions src/lib/isLocale.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
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);
return localeReg.test(str);
}
19 changes: 19 additions & 0 deletions test/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -2801,6 +2801,25 @@ describe('Validators', () => {
});
});

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

it('should validate strings by byte length', () => {
test({
validator: 'isByteLength',
Expand Down
7 changes: 7 additions & 0 deletions validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,12 @@ function isBoolean(str) {
return ['true', 'false', '1', '0'].indexOf(str) >= 0;
}

var localeReg = /^[A-z]{2,4}([_-]([A-z]{4}|[\d]{3}))?([_-]([A-z]{2}|[\d]{3}))?$/;
function isLocale(str) {
assertString(str);
return localeReg.test(str);
}

var alpha = {
'en-US': /^[A-Z]+$/i,
'bg-BG': /^[А-Я]+$/i,
Expand Down Expand Up @@ -2072,6 +2078,7 @@ var validator = {
isJSON: isJSON,
isEmpty: isEmpty,
isLength: isLength,
isLocale: isLocale,
isByteLength: isByteLength,
isUUID: isUUID,
isMongoId: isMongoId,
Expand Down
2 changes: 1 addition & 1 deletion validator.min.js

Large diffs are not rendered by default.

0 comments on commit f41ac00

Please sign in to comment.