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

Add isBase64URL support #1277

Merged
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: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ Validator | Description
**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`.
**isAscii(str)** | check if the string contains ASCII chars only.
**isBase32(str)** | check if a string is base32 encoded.
**isBase64(str)** | check if a string is base64 encoded.
**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)
**isBefore(str [, date])** | check if the string is a date that's before the specified date.
**isIBAN(str)** | check if a string is a IBAN (International Bank Account Number).
**isBIC(str)** | check if a string is a BIC (Bank Identification Code) or SWIFT code.
Expand Down
6 changes: 6 additions & 0 deletions es/lib/isBase64.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import assertString from './util/assertString';
var notBase64 = /[^A-Z0-9+\/=]/i;
export default function isBase64(str) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
assertString(str);

if (options.urlSafe) {
return /^[A-Za-z0-9_-]+$/.test(str);
}

var len = str.length;

if (!len || len % 4 !== 0 || notBase64.test(str)) {
Expand Down
6 changes: 6 additions & 0 deletions lib/isBase64.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
var notBase64 = /[^A-Z0-9+\/=]/i;

function isBase64(str) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
(0, _assertString.default)(str);

if (options.urlSafe) {
return /^[A-Za-z0-9_-]+$/.test(str);
}

var len = str.length;

if (!len || len % 4 !== 0 || notBase64.test(str)) {
Expand Down
7 changes: 6 additions & 1 deletion src/lib/isBase64.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@ import assertString from './util/assertString';

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

export default function isBase64(str) {
export default function isBase64(str, options = {}) {
assertString(str);

if (options.urlSafe) {
return /^[A-Za-z0-9_-]+$/.test(str);
}

const len = str.length;
if (!len || len % 4 !== 0 || notBase64.test(str)) {
return false;
Expand Down
30 changes: 30 additions & 0 deletions test/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -8196,6 +8196,36 @@ describe('Validators', () => {
});
});

it('should validate base64URL', () => {
test({
validator: 'isBase64',
args: [{ urlSafe: true }],
valid: [
'bGFkaWVzIGFuZCBnZW50bGVtZW4sIHdlIGFyZSBmbG9hdGluZyBpbiBzcGFjZQ',
'1234',
'bXVtLW5ldmVyLXByb3Vk',
'PDw_Pz8-Pg',
'VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw',
],
invalid: [
' AA',
'\tAA',
'\rAA',
'\nAA',
'123=',
'This+isa/bad+base64Url==',
'0K3RgtC+INC30LDQutC+0LTQuNGA0L7QstCw0L3QvdCw0Y8g0YHRgtGA0L7QutCw',
],
error: [
null,
undefined,
{},
[],
42,
],
});
});

it('should validate date', () => {
test({
validator: 'isDate',
Expand Down
10 changes: 9 additions & 1 deletion validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -2196,7 +2196,9 @@ function currencyRegex(options) {
options.digits_after_decimal.forEach(function (digit, index) {
if (index !== 0) decimal_digits = "".concat(decimal_digits, "|\\d{").concat(digit, "}");
});
var symbol = "(\\".concat(options.symbol.replace(/\./g, '\\.'), ")").concat(options.require_symbol ? '' : '?'),
var symbol = "(".concat(options.symbol.replace(/\W/, function (m) {
return "\\".concat(m);
}), ")").concat(options.require_symbol ? '' : '?'),
negative = '-?',
whole_dollar_amount_without_sep = '[1-9]\\d*',
whole_dollar_amount_with_sep = "[1-9]\\d{0,2}(\\".concat(options.thousands_separator, "\\d{3})*"),
Expand Down Expand Up @@ -2361,7 +2363,13 @@ function isBase32(str) {

var notBase64 = /[^A-Z0-9+\/=]/i;
function isBase64(str) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
assertString(str);

if (options.urlSafe) {
return /^[A-Za-z0-9_-]+$/.test(str);
}

var len = str.length;

if (!len || len % 4 !== 0 || notBase64.test(str)) {
Expand Down
2 changes: 1 addition & 1 deletion validator.min.js

Large diffs are not rendered by default.