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

Added isOctal validator including tests and updated README. #1153

Merged
merged 1 commit into from Oct 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -126,6 +126,7 @@ Validator | Description
**isMongoId(str)** | check if the string is a valid hex-encoded representation of a [MongoDB ObjectId][mongoid].
**isMultibyte(str)** | check if the string contains one or more multibyte chars.
**isNumeric(str [, options])** | check if the string contains only numbers.<br/><br/>`options` is an object which defaults to `{no_symbols: false}`. If `no_symbols` is true, the validator will reject numeric strings that feature a symbol (e.g. `+`, `-`, or `.`).
**isOctal(str)** | check if the string is a valid octal number.
**isPort(str)** | check if the string is a valid port number.
**isPostalCode(str, locale)** | check if the string is a postal code,<br/><br/>(locale is one of `[ 'AD', 'AT', 'AU', 'BE', 'BG', 'BR', 'CA', 'CH', 'CZ', 'DE', 'DK', 'DZ', 'EE', 'ES', 'FI', 'FR', 'GB', 'GR', 'HR', 'HU', 'ID', 'IE' 'IL', 'IN', 'IS', 'IT', 'JP', 'KE', 'LI', 'LT', 'LU', 'LV', 'MT', 'MX', 'NL', 'NO', 'NZ', 'PL', 'PR', 'PT', 'RO', 'RU', 'SA', 'SE', 'SI', 'TN', 'TW', 'UA', 'US', 'ZA', 'ZM' ]` OR 'any'. If 'any' is used, function will check if any of the locals match. Locale list is `validator.isPostalCodeLocales`.).
**isSurrogatePair(str)** | check if the string contains any surrogate pairs chars.
Expand Down
2 changes: 2 additions & 0 deletions src/index.js
Expand Up @@ -33,6 +33,7 @@ import isInt from './lib/isInt';
import isFloat, { locales as isFloatLocales } from './lib/isFloat';
import isDecimal from './lib/isDecimal';
import isHexadecimal from './lib/isHexadecimal';
import isOctal from './lib/isOctal';
import isDivisibleBy from './lib/isDivisibleBy';

import isHexColor from './lib/isHexColor';
Expand Down Expand Up @@ -135,6 +136,7 @@ const validator = {
isFloatLocales,
isDecimal,
isHexadecimal,
isOctal,
isDivisibleBy,
isHexColor,
isISRC,
Expand Down
8 changes: 8 additions & 0 deletions src/lib/isOctal.js
@@ -0,0 +1,8 @@
import assertString from './util/assertString';

const octal = /^(0o)?[0-7]+$/i;

export default function isOctal(str) {
assertString(str);
return octal.test(str);
}
18 changes: 18 additions & 0 deletions test/validators.js
Expand Up @@ -2504,6 +2504,24 @@ describe('Validators', () => {
});
});

it('should validate octal strings', () => {
test({
validator: 'isOctal',
valid: [
'076543210',
'0o01234567',
],
invalid: [
'abcdefg',
'012345678',
'012345670c',
'00c12345670c',
'',
'..',
],
});
});

it('should validate hexadecimal color strings', () => {
test({
validator: 'isHexColor',
Expand Down