Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More validators #684

Merged
merged 6 commits into from
Aug 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ Passing anything other than a string is an error.
- **isIn(str, values)** - check if the string is in a array of allowed values.
- **isInt(str [, options])** - check if the string is an integer. `options` is an object which can contain the keys `min` and/or `max` to check the integer is within boundaries (e.g. `{ min: 10, max: 99 }`). `options` can also contain the key `allow_leading_zeroes`, which when set to false will disallow integer values with leading zeroes (e.g. `{ allow_leading_zeroes: false }`). Finally, `options` can contain the keys `gt` and/or `lt` which will enforce integers being greater than or less than, respectively, the value provided (e.g. `{gt: 1, lt: 4}` for a number between 1 and 4).
- **isJSON(str)** - check if the string is valid JSON (note: uses JSON.parse).
- **isLatLong(str)** - check if the string is a valid latitude-longitude coordinate.
- **isLength(str, options)** - check if the string's length falls in a range. `options` is an object which defaults to `{min:0, max: undefined}`. Note: this function takes into account surrogate pairs.
- **isLowercase(str)** - check if the string is lowercase.
- **isMACAddress(str)** - check if the string is a MAC address.
Expand All @@ -92,6 +93,7 @@ Passing anything other than a string is an error.
- **isMongoId(str)** - check if the string is a valid hex-encoded representation of a [MongoDB ObjectId][mongoid].
- **isMultibyte(str)** - check if the string contains one or more multibyte chars.
- **isNumeric(str)** - check if the string contains only numbers.
- **isPostalCode(str, locale)** - check if the string is a postal code, (locale is one of `[ 'AT','AU','BE','CA','CH','CZ','DE','DK','DZ','ES','FI','FR','GB','GR','IL','IN','IS','IT','JP','KE','LI','MX','NL','NO','PL','PT','RO','RU','SA','SE','TW','US','ZA','ZM' ]` OR 'any'. If 'any' is used, function will check if any of the locals match).
- **isSurrogatePair(str)** - check if the string contains any surrogate pairs chars.
- **isURL(str [, options])** - check if the string is an URL. `options` is an object which defaults to `{ protocols: ['http','https','ftp'], require_tld: true, require_protocol: false, require_host: true, require_valid_protocol: true, allow_underscores: false, host_whitelist: false, host_blacklist: false, allow_trailing_dot: false, allow_protocol_relative_urls: false }`.
- **isUUID(str [, version])** - check if the string is a UUID (version 3, 4 or 5).
Expand Down
10 changes: 10 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,14 @@ var _isDataURI = require('./lib/isDataURI');

var _isDataURI2 = _interopRequireDefault(_isDataURI);

var _isLatLong = require('./lib/isLatLong');

var _isLatLong2 = _interopRequireDefault(_isLatLong);

var _isPostalCode = require('./lib/isPostalCode');

var _isPostalCode2 = _interopRequireDefault(_isPostalCode);

var _ltrim = require('./lib/ltrim');

var _ltrim2 = _interopRequireDefault(_ltrim);
Expand Down Expand Up @@ -300,10 +308,12 @@ var validator = {
isISBN: _isISBN2.default,
isISSN: _isISSN2.default,
isMobilePhone: _isMobilePhone2.default,
isPostalCode: _isPostalCode2.default,
isCurrency: _isCurrency2.default,
isISO8601: _isISO2.default,
isBase64: _isBase2.default,
isDataURI: _isDataURI2.default,
isLatLong: _isLatLong2.default,
ltrim: _ltrim2.default,
rtrim: _rtrim2.default,
trim: _trim2.default,
Expand Down
23 changes: 23 additions & 0 deletions lib/isLatLong.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict';

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

exports.default = function (str) {
(0, _assertString2.default)(str);
if (!str.includes(',')) return false;
var pair = str.split(',');
return lat.test(pair[0]) && long.test(pair[1]);
};

var _assertString = require('./util/assertString');

var _assertString2 = _interopRequireDefault(_assertString);

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

var lat = /^\(?[+-]?(90(\.0+)?|[1-8]?\d(\.\d+)?)$/;
var long = /^\s?[+-]?(180(\.0+)?|1[0-7]\d(\.\d+)?|\d{1,2}(\.\d+)?)\)?$/;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is an optional whitespace tolerated at the beginning of long, but not at the end of long or the beginning or end of lat?

Copy link
Contributor Author

@0xch4z 0xch4z Aug 22, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because it is pretty common to separate coordinates with a comma and a space, but the space should not be mandatory. It's not common however, to prefix with a space, or end with a space. That's needless and would look incredibly silly.


module.exports = exports['default'];
74 changes: 74 additions & 0 deletions lib/isPostalCode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
'use strict';

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

exports.default = function (str, locale) {
(0, _assertString2.default)(str);
if (locale in patterns) {
return patterns[locale].test(str);
} else if (locale === 'any') {
for (var key in patterns) {
if (patterns.hasOwnProperty(key)) {
var pattern = patterns[key];
if (pattern.test(str)) {
return true;
}
}
}
return false;
}
throw new Error('Invalid locale \'' + locale + '\'');
};

var _assertString = require('./util/assertString');

var _assertString2 = _interopRequireDefault(_assertString);

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

// common patterns
var threeDigit = /^\d{3}$/;
var fourDigit = /^\d{4}$/;
var fiveDigit = /^\d{5}$/;
var sixDigit = /^\d{6}$/;

var patterns = {
AT: fourDigit,
AU: sixDigit,
BE: fourDigit,
CA: /^[ABCEGHJKLMNPRSTVXY]\d[ABCEGHJ-NPRSTV-Z][\s\-]?\d[ABCEGHJ-NPRSTV-Z]\d$/i,
CH: fourDigit,
CZ: /^\d{3}\s?\d{2}$/,
DE: fiveDigit,
DK: fourDigit,
DZ: fiveDigit,
ES: fiveDigit,
FI: fiveDigit,
FR: /^\d{2}\s?\d{3}$/,
GB: /^(gir\s?0aa|[a-z]{1,2}\d[\da-z]?\s?(\d[a-z]{2})?)$/i,
GR: /^\d{3}\s?\d{2}$/,
IL: fiveDigit,
IN: sixDigit,
IS: threeDigit,
IT: fiveDigit,
JP: /^\d{3}\-\d{4}$/,
KE: fiveDigit,
LI: /^(948[5-9]|949[0-7])$/,
MX: fiveDigit,
NL: /^\d{4}\s?[a-z]{2}$/i,
NO: fourDigit,
PL: /^\d{2}\-\d{3}$/,
PT: /^\d{4}(\-\d{3})?$/,
RO: sixDigit,
RU: sixDigit,
SA: fiveDigit,
SE: /^\d{3}\s?\d{2}$/,
TW: /^\d{3}(\d{2})?$/,
US: /^\d{5}(-\d{4})?$/,
ZA: fourDigit,
ZM: fiveDigit
};

module.exports = exports['default'];
5 changes: 5 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ import isISO8601 from './lib/isISO8601';
import isBase64 from './lib/isBase64';
import isDataURI from './lib/isDataURI';

import isLatLong from './lib/isLatLong';
import isPostalCode from './lib/isPostalCode';

import ltrim from './lib/ltrim';
import rtrim from './lib/rtrim';
import trim from './lib/trim';
Expand Down Expand Up @@ -132,10 +135,12 @@ const validator = {
isISBN,
isISSN,
isMobilePhone,
isPostalCode,
isCurrency,
isISO8601,
isBase64,
isDataURI,
isLatLong,
ltrim,
rtrim,
trim,
Expand Down
11 changes: 11 additions & 0 deletions src/lib/isLatLong.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import assertString from './util/assertString';

const lat = /^\(?[+-]?(90(\.0+)?|[1-8]?\d(\.\d+)?)$/;
const long = /^\s?[+-]?(180(\.0+)?|1[0-7]\d(\.\d+)?|\d{1,2}(\.\d+)?)\)?$/;

export default function (str) {
assertString(str);
if (!str.includes(',')) return false;
const pair = str.split(',');
return lat.test(pair[0]) && long.test(pair[1]);
}
62 changes: 62 additions & 0 deletions src/lib/isPostalCode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import assertString from './util/assertString';

// common patterns
const threeDigit = /^\d{3}$/;
const fourDigit = /^\d{4}$/;
const fiveDigit = /^\d{5}$/;
const sixDigit = /^\d{6}$/;

const patterns = {
AT: fourDigit,
AU: sixDigit,
BE: fourDigit,
CA: /^[ABCEGHJKLMNPRSTVXY]\d[ABCEGHJ-NPRSTV-Z][\s\-]?\d[ABCEGHJ-NPRSTV-Z]\d$/i,
CH: fourDigit,
CZ: /^\d{3}\s?\d{2}$/,
DE: fiveDigit,
DK: fourDigit,
DZ: fiveDigit,
ES: fiveDigit,
FI: fiveDigit,
FR: /^\d{2}\s?\d{3}$/,
GB: /^(gir\s?0aa|[a-z]{1,2}\d[\da-z]?\s?(\d[a-z]{2})?)$/i,
GR: /^\d{3}\s?\d{2}$/,
IL: fiveDigit,
IN: sixDigit,
IS: threeDigit,
IT: fiveDigit,
JP: /^\d{3}\-\d{4}$/,
KE: fiveDigit,
LI: /^(948[5-9]|949[0-7])$/,
MX: fiveDigit,
NL: /^\d{4}\s?[a-z]{2}$/i,
NO: fourDigit,
PL: /^\d{2}\-\d{3}$/,
PT: /^\d{4}(\-\d{3})?$/,
RO: sixDigit,
RU: sixDigit,
SA: fiveDigit,
SE: /^\d{3}\s?\d{2}$/,
TW: /^\d{3}(\d{2})?$/,
US: /^\d{5}(-\d{4})?$/,
ZA: fourDigit,
ZM: fiveDigit,
};

export default function (str, locale) {
assertString(str);
if (locale in patterns) {
return patterns[locale].test(str);
} else if (locale === 'any') {
for (const key in patterns) {
if (patterns.hasOwnProperty(key)) {
const pattern = patterns[key];
if (pattern.test(str)) {
return true;
}
}
}
return false;
}
throw new Error(`Invalid locale '${locale}'`);
}
Loading