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 isBase58 functionality #1445

Merged
merged 1 commit into from
Oct 4, 2020
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +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', 'fa-AF', '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', 'vi-VN']`) 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.
**isBase58(str)** | check if a string is base58 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.
**isBIC(str)** | check if a string is a BIC (Bank Identification Code) or SWIFT code.
Expand Down
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ import isISO31661Alpha2 from './lib/isISO31661Alpha2';
import isISO31661Alpha3 from './lib/isISO31661Alpha3';

import isBase32 from './lib/isBase32';
import isBase58 from './lib/isBase58';
import isBase64 from './lib/isBase64';
import isDataURI from './lib/isDataURI';
import isMagnetURI from './lib/isMagnetURI';
Expand Down Expand Up @@ -194,6 +195,7 @@ const validator = {
isISO31661Alpha2,
isISO31661Alpha3,
isBase32,
isBase58,
isBase64,
isDataURI,
isMagnetURI,
Expand Down
12 changes: 12 additions & 0 deletions src/lib/isBase58.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import assertString from './util/assertString';

// Accepted chars - 123456789ABCDEFGH JKLMN PQRSTUVWXYZabcdefghijk mnopqrstuvwxyz
const base58Reg = /^[A-HJ-NP-Za-km-z1-9]*$/;

export default function isBase58(str) {
assertString(str);
if (base58Reg.test(str)) {
return true;
}
return false;
}
25 changes: 25 additions & 0 deletions test/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -4774,6 +4774,31 @@ describe('Validators', () => {
});
});

it('should validate base58 strings', () => {
test({
validator: 'isBase58',
valid: [
'BukQL',
'3KMUV89zab',
'91GHkLMNtyo98',
'YyjKm3H',
'Mkhss145TRFg',
'7678765677',
'abcodpq',
'AAVHJKLPY',
],
invalid: [
'0OPLJH',
'IMKLP23',
'KLMOmk986',
'LL1l1985hG',
'*MP9K',
'Zm=8JBSWY3DP',
')()(=9292929MKL',
],
});
});

it('should validate base64 strings', () => {
test({
validator: 'isBase64',
Expand Down