Skip to content

Commit

Permalink
feat(isBase58): add new isBase58 validator (#1445)
Browse files Browse the repository at this point in the history
  • Loading branch information
ezkemboi authored Oct 4, 2020
1 parent 26f3715 commit 7916dfd
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 0 deletions.
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', '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`.
**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 @@ -4818,6 +4818,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

0 comments on commit 7916dfd

Please sign in to comment.