From 8eb1a1c1b3ec88403c774f020c714392528f8bd8 Mon Sep 17 00:00:00 2001 From: Oswaldo Date: Tue, 21 Dec 2021 21:31:38 -0600 Subject: [PATCH 1/3] feat(crockforbase32): Add option for Crockford's base 32 alternative to isBase32 method --- README.md | 2 +- src/lib/isBase32.js | 14 +++++++++++++- test/validators.js | 17 +++++++++++++++++ 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 0d2ed2bce..d82db8c2a 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. **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..1eb4235f2 100644 --- a/test/validators.js +++ b/test/validators.js @@ -5751,6 +5751,23 @@ describe('Validators', () => { 'Zm9vYm/y====', ], }); + + test({ + validator: 'isBase32', + args: [{ crockford: true }], + valid: [ + '91JPRV3F41BPYWKCCGGG', + '60', + '64', + 'B5QQA833C5Q20S3F41MQ8', + ], + invalid: [ + '91JPRV3F41BUPYWKCCGGG', + 'B5QQA833C5Q20S3F41MQ8L', + '60I', + 'B5QQA833OULIC5Q20S3F41MQ8', + ], + }); }); it('should validate base58 strings', () => { From 14aa576bf613497f9740327186ef17523b6a9166 Mon Sep 17 00:00:00 2001 From: Oswaldo Date: Fri, 7 Jan 2022 11:52:29 -0600 Subject: [PATCH 2/3] test move out to its own it() statement --- test/validators.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/validators.js b/test/validators.js index 1eb4235f2..0c2fba2c0 100644 --- a/test/validators.js +++ b/test/validators.js @@ -5751,7 +5751,9 @@ describe('Validators', () => { 'Zm9vYm/y====', ], }); + }); + it('should validate base32 strings with crockford alternative', () => { test({ validator: 'isBase32', args: [{ crockford: true }], From 177be3df2bfe7faaaaf969f61098c5cdadb9cd72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oswaldo=20V=C3=A1zquez?= Date: Mon, 10 Jan 2022 12:00:43 -0600 Subject: [PATCH 3/3] Update README.md Co-authored-by: Brage --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index d82db8c2a..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 [, 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. +**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.