Skip to content

Commit 7916dfd

Browse files
authored
feat(isBase58): add new isBase58 validator (#1445)
1 parent 26f3715 commit 7916dfd

File tree

4 files changed

+40
-0
lines changed

4 files changed

+40
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +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', 'az-AZ', '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`.
8989
**isAscii(str)** | check if the string contains ASCII chars only.
9090
**isBase32(str)** | check if a string is base32 encoded.
91+
**isBase58(str)** | check if a string is base58 encoded.
9192
**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)
9293
**isBefore(str [, date])** | check if the string is a date that's before the specified date.
9394
**isBIC(str)** | check if a string is a BIC (Bank Identification Code) or SWIFT code.

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ import isISO31661Alpha2 from './lib/isISO31661Alpha2';
9292
import isISO31661Alpha3 from './lib/isISO31661Alpha3';
9393

9494
import isBase32 from './lib/isBase32';
95+
import isBase58 from './lib/isBase58';
9596
import isBase64 from './lib/isBase64';
9697
import isDataURI from './lib/isDataURI';
9798
import isMagnetURI from './lib/isMagnetURI';
@@ -194,6 +195,7 @@ const validator = {
194195
isISO31661Alpha2,
195196
isISO31661Alpha3,
196197
isBase32,
198+
isBase58,
197199
isBase64,
198200
isDataURI,
199201
isMagnetURI,

src/lib/isBase58.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import assertString from './util/assertString';
2+
3+
// Accepted chars - 123456789ABCDEFGH JKLMN PQRSTUVWXYZabcdefghijk mnopqrstuvwxyz
4+
const base58Reg = /^[A-HJ-NP-Za-km-z1-9]*$/;
5+
6+
export default function isBase58(str) {
7+
assertString(str);
8+
if (base58Reg.test(str)) {
9+
return true;
10+
}
11+
return false;
12+
}

test/validators.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4818,6 +4818,31 @@ describe('Validators', () => {
48184818
});
48194819
});
48204820

4821+
it('should validate base58 strings', () => {
4822+
test({
4823+
validator: 'isBase58',
4824+
valid: [
4825+
'BukQL',
4826+
'3KMUV89zab',
4827+
'91GHkLMNtyo98',
4828+
'YyjKm3H',
4829+
'Mkhss145TRFg',
4830+
'7678765677',
4831+
'abcodpq',
4832+
'AAVHJKLPY',
4833+
],
4834+
invalid: [
4835+
'0OPLJH',
4836+
'IMKLP23',
4837+
'KLMOmk986',
4838+
'LL1l1985hG',
4839+
'*MP9K',
4840+
'Zm=8JBSWY3DP',
4841+
')()(=9292929MKL',
4842+
],
4843+
});
4844+
});
4845+
48214846
it('should validate base64 strings', () => {
48224847
test({
48234848
validator: 'isBase64',

0 commit comments

Comments
 (0)