Skip to content

Commit

Permalink
feat(isOctal): add new isOctal validator (#1153)
Browse files Browse the repository at this point in the history
  • Loading branch information
ayala-io authored and profnandaa committed Oct 13, 2019
1 parent d18a8b8 commit 4162ae7
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 0 deletions.
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

0 comments on commit 4162ae7

Please sign in to comment.