diff --git a/README.md b/README.md index becdc74de..0944a19a9 100644 --- a/README.md +++ b/README.md @@ -88,6 +88,7 @@ Validator | Description **isAlphanumeric(str [, locale])** | check if the string contains only letters and numbers.

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}`
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. diff --git a/src/index.js b/src/index.js index 502331db7..4b7f4ae49 100644 --- a/src/index.js +++ b/src/index.js @@ -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'; @@ -194,6 +195,7 @@ const validator = { isISO31661Alpha2, isISO31661Alpha3, isBase32, + isBase58, isBase64, isDataURI, isMagnetURI, diff --git a/src/lib/isBase58.js b/src/lib/isBase58.js new file mode 100644 index 000000000..05c46dc18 --- /dev/null +++ b/src/lib/isBase58.js @@ -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; +} diff --git a/test/validators.js b/test/validators.js index 700868b87..728520abb 100644 --- a/test/validators.js +++ b/test/validators.js @@ -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',