diff --git a/README.md b/README.md index 0d2ed2bce..7386e1ca9 100644 --- a/README.md +++ b/README.md @@ -94,7 +94,7 @@ Validator | Description **isAlpha(str [, locale, options])** | check if the string contains only letters (a-zA-Z).

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', 'fa-IR', 'fi-FI', 'fr-CA', 'fr-FR', 'he', 'hi-IN', '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.isAlphaLocales`. options is an optional object that can be supplied with the following key(s): ignore which can either be a String or RegExp of characters to be ignored e.g. " -" will ignore spaces and -'s. **isAlphanumeric(str [, locale, options])** | check if the string contains only letters and numbers (a-zA-Z0-9).

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', 'fa-IR', 'fi-FI', 'fr-CA', 'fr-FR', 'he', 'hi-IN', '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`. options is an optional object that can be supplied with the following key(s): ignore which can either be a String or RegExp of characters to be ignored e.g. " -" will ignore spaces and -'s. **isAscii(str)** | check if the string contains ASCII chars only. -**isBase32(str)** | check if a string is base32 encoded. +**isBase32(str [, options])** | check if a string is base32 encoded. `options` is optional and defaults to `{crockford: false}`.
When `crockford` is true it tests the given base32 encoded string using [Crockford's base32 alternative](http://www.crockford.com/base32.html). **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. diff --git a/src/lib/isBase32.js b/src/lib/isBase32.js index b2e9c8a28..5e2969cbc 100644 --- a/src/lib/isBase32.js +++ b/src/lib/isBase32.js @@ -1,9 +1,21 @@ import assertString from './util/assertString'; +import merge from './util/merge'; const base32 = /^[A-Z2-7]+=*$/; +const crockfordBase32 = /^[A-HJKMNP-TV-Z0-9]+$/; -export default function isBase32(str) { +const defaultBase32Options = { + crockford: false, +}; + +export default function isBase32(str, options) { assertString(str); + options = merge(options, defaultBase32Options); + + if (options.crockford) { + return crockfordBase32.test(str); + } + const len = str.length; if (len % 8 === 0 && base32.test(str)) { return true; diff --git a/test/validators.js b/test/validators.js index 76ec3d95a..0c2fba2c0 100644 --- a/test/validators.js +++ b/test/validators.js @@ -5753,6 +5753,25 @@ describe('Validators', () => { }); }); + it('should validate base32 strings with crockford alternative', () => { + test({ + validator: 'isBase32', + args: [{ crockford: true }], + valid: [ + '91JPRV3F41BPYWKCCGGG', + '60', + '64', + 'B5QQA833C5Q20S3F41MQ8', + ], + invalid: [ + '91JPRV3F41BUPYWKCCGGG', + 'B5QQA833C5Q20S3F41MQ8L', + '60I', + 'B5QQA833OULIC5Q20S3F41MQ8', + ], + }); + }); + it('should validate base58 strings', () => { test({ validator: 'isBase58',