Skip to content

Commit 027d092

Browse files
feat(isBase64): add urlSafe support (#1277)
* added support for isBase64URL * updated lib for isBase64URL * update readme * refactor * conflict resolve * cleanup files
1 parent f2ffa59 commit 027d092

File tree

7 files changed

+59
-4
lines changed

7 files changed

+59
-4
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ Validator | Description
8888
**isAlphanumeric(str [, locale])** | check if the string contains only letters and numbers.<br/><br/>Locale is one of `['ar', 'ar-AE', 'ar-BH', 'ar-DZ', 'ar-EG', 'ar-IQ', 'ar-JO', 'ar-KW', 'ar-LB', 'ar-LY', 'ar-MA', 'ar-QA', 'ar-QM', 'ar-SA', 'ar-SD', 'ar-SY', 'ar-TN', 'ar-YE', 'bg-BG', 'cs-CZ', 'da-DK', 'de-DE', 'el-GR', 'en-AU', 'en-GB', 'en-HK', 'en-IN', 'en-NZ', 'en-US', 'en-ZA', 'en-ZM', 'es-ES', 'fr-FR', 'fa-IR', 'he', 'hu-HU', 'it-IT', 'ku-IQ', 'nb-NO', 'nl-NL', 'nn-NO', 'pl-PL', 'pt-BR', 'pt-PT', 'ru-RU', 'sl-SI', 'sk-SK', 'sr-RS', 'sr-RS@latin', 'sv-SE', 'tr-TR', 'uk-UA']`) and defaults to `en-US`. Locale list is `validator.isAlphanumericLocales`.
8989
**isAscii(str)** | check if the string contains ASCII chars only.
9090
**isBase32(str)** | check if a string is base32 encoded.
91-
**isBase64(str)** | check if a string is base64 encoded.
91+
**isBase64(str, [, options])** | check if a string is base64 encoded. options is optional and defaults to `{urlSafe: false}`<br/> when `urlSafe` is true it tests the given base64 encoded string is [url safe](https://base64.guru/standards/base64url)
9292
**isBefore(str [, date])** | check if the string is a date that's before the specified date.
9393
**isIBAN(str)** | check if a string is a IBAN (International Bank Account Number).
9494
**isBIC(str)** | check if a string is a BIC (Bank Identification Code) or SWIFT code.

es/lib/isBase64.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
import assertString from './util/assertString';
22
var notBase64 = /[^A-Z0-9+\/=]/i;
33
export default function isBase64(str) {
4+
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
45
assertString(str);
6+
7+
if (options.urlSafe) {
8+
return /^[A-Za-z0-9_-]+$/.test(str);
9+
}
10+
511
var len = str.length;
612

713
if (!len || len % 4 !== 0 || notBase64.test(str)) {

lib/isBase64.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,13 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
1212
var notBase64 = /[^A-Z0-9+\/=]/i;
1313

1414
function isBase64(str) {
15+
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
1516
(0, _assertString.default)(str);
17+
18+
if (options.urlSafe) {
19+
return /^[A-Za-z0-9_-]+$/.test(str);
20+
}
21+
1622
var len = str.length;
1723

1824
if (!len || len % 4 !== 0 || notBase64.test(str)) {

src/lib/isBase64.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,13 @@ import assertString from './util/assertString';
22

33
const notBase64 = /[^A-Z0-9+\/=]/i;
44

5-
export default function isBase64(str) {
5+
export default function isBase64(str, options = {}) {
66
assertString(str);
7+
8+
if (options.urlSafe) {
9+
return /^[A-Za-z0-9_-]+$/.test(str);
10+
}
11+
712
const len = str.length;
813
if (!len || len % 4 !== 0 || notBase64.test(str)) {
914
return false;

test/validators.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8212,6 +8212,36 @@ describe('Validators', () => {
82128212
});
82138213
});
82148214

8215+
it('should validate base64URL', () => {
8216+
test({
8217+
validator: 'isBase64',
8218+
args: [{ urlSafe: true }],
8219+
valid: [
8220+
'bGFkaWVzIGFuZCBnZW50bGVtZW4sIHdlIGFyZSBmbG9hdGluZyBpbiBzcGFjZQ',
8221+
'1234',
8222+
'bXVtLW5ldmVyLXByb3Vk',
8223+
'PDw_Pz8-Pg',
8224+
'VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw',
8225+
],
8226+
invalid: [
8227+
' AA',
8228+
'\tAA',
8229+
'\rAA',
8230+
'\nAA',
8231+
'123=',
8232+
'This+isa/bad+base64Url==',
8233+
'0K3RgtC+INC30LDQutC+0LTQuNGA0L7QstCw0L3QvdCw0Y8g0YHRgtGA0L7QutCw',
8234+
],
8235+
error: [
8236+
null,
8237+
undefined,
8238+
{},
8239+
[],
8240+
42,
8241+
],
8242+
});
8243+
});
8244+
82158245
it('should validate date', () => {
82168246
test({
82178247
validator: 'isDate',

validator.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2196,7 +2196,9 @@ function currencyRegex(options) {
21962196
options.digits_after_decimal.forEach(function (digit, index) {
21972197
if (index !== 0) decimal_digits = "".concat(decimal_digits, "|\\d{").concat(digit, "}");
21982198
});
2199-
var symbol = "(\\".concat(options.symbol.replace(/\./g, '\\.'), ")").concat(options.require_symbol ? '' : '?'),
2199+
var symbol = "(".concat(options.symbol.replace(/\W/, function (m) {
2200+
return "\\".concat(m);
2201+
}), ")").concat(options.require_symbol ? '' : '?'),
22002202
negative = '-?',
22012203
whole_dollar_amount_without_sep = '[1-9]\\d*',
22022204
whole_dollar_amount_with_sep = "[1-9]\\d{0,2}(\\".concat(options.thousands_separator, "\\d{3})*"),
@@ -2361,7 +2363,13 @@ function isBase32(str) {
23612363

23622364
var notBase64 = /[^A-Z0-9+\/=]/i;
23632365
function isBase64(str) {
2366+
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
23642367
assertString(str);
2368+
2369+
if (options.urlSafe) {
2370+
return /^[A-Za-z0-9_-]+$/.test(str);
2371+
}
2372+
23652373
var len = str.length;
23662374

23672375
if (!len || len % 4 !== 0 || notBase64.test(str)) {

validator.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)