Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(isBase32): Add option for Crockford's base 32 alternative #1888

Merged
merged 3 commits into from
Apr 22, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ Validator | Description
**isAlpha(str [, locale, options])** | check if the string contains only letters (a-zA-Z).<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', '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).<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', '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}`<br/> when `crockford` is true it tests the given base32 encoded string using Crockford's Base32 alternative.
BigOsvaap marked this conversation as resolved.
Show resolved Hide resolved
**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.
Expand Down
14 changes: 13 additions & 1 deletion src/lib/isBase32.js
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
17 changes: 17 additions & 0 deletions test/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -5751,6 +5751,23 @@ describe('Validators', () => {
'Zm9vYm/y====',
],
});

test({
validator: 'isBase32',
args: [{ crockford: true }],
valid: [
'91JPRV3F41BPYWKCCGGG',
'60',
'64',
'B5QQA833C5Q20S3F41MQ8',
],
invalid: [
'91JPRV3F41BUPYWKCCGGG',
'B5QQA833C5Q20S3F41MQ8L',
'60I',
'B5QQA833OULIC5Q20S3F41MQ8',
],
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest you move this test out to its own it() statement so it is more clear precisely what is being tested. At least many other tests are split up like this. See 'should validate JSON' and 'should validate JSON with primitives'.

Examples to the contrary are found as well ('should validate ISSNs' has everything in one it() statement), so I suppose a clarification of the convention here would be nice, but that's a bigger topic.

});

it('should validate base58 strings', () => {
Expand Down