Repository: validatorjs/validator.js
Pull request #1153 (2019-10-12 13:57:07)

Added isOctal validator including tests and updated README.
Added a validator `isOctal(str)` to validate strings intended to be octal numbers. It is intended for general octal numbers. It also accepts the common prefix of '0c' although it is not required to have the prefix to validate.

Changes:
* `README.md` has been updated to reflect the new isOctal addition
* `src/lib/isOctal.js` source of the validator function
* `src/index.js` modified to include `isOctal`
* `test/validators.js` added test cases to test `isOctal`



Patch:
diff --git a/README.md b/README.md
index 095a4d7..018f395 100644
--- a/README.md
+++ b/README.md
@@ -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.
diff --git a/src/index.js b/src/index.js
index 5f21fcf..f96d4c1 100644
--- a/src/index.js
+++ b/src/index.js
@@ -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';
@@ -135,6 +136,7 @@ const validator = {
   isFloatLocales,
   isDecimal,
   isHexadecimal,
+  isOctal,
   isDivisibleBy,
   isHexColor,
   isISRC,
diff --git a/src/lib/isOctal.js b/src/lib/isOctal.js
new file mode 100644
index 0000000..205b98a
--- /dev/null
+++ b/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);
+}
diff --git a/src/lib/isPostalCode.js b/src/lib/isPostalCode.js
index 296d590..4176d5a 100644
--- a/src/lib/isPostalCode.js
+++ b/src/lib/isPostalCode.js
@@ -30,7 +30,7 @@ const patterns = {
   ID: fiveDigit,
   IE: /^[A-z]\d[\d|w]\s\w{4}$/i,
   IL: fiveDigit,
-  IN: sixDigit,
+  IN: /^((?!10|29|35|54|55|65|66|86|87|88|89)[1-9][0-9]{5})$/,
   IS: threeDigit,
   IT: fiveDigit,
   JP: /^\d{3}\-\d{4}$/,


Test patch:
diff --git a/test/validators.js b/test/validators.js
index 09715fb..2043af8 100644
--- a/test/validators.js
+++ b/test/validators.js
@@ -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',
@@ -6743,6 +6761,29 @@ describe('Validators', () => {
           'A65T F12',
         ],
       },
+      {
+        locale: 'IN',
+        valid: [
+          '364240',
+          '360005',
+        ],
+        invalid: [
+          '123',
+          '012345',
+          '011111',
+          '101123',
+          '291123',
+          '351123',
+          '541123',
+          '551123',
+          '651123',
+          '661123',
+          '861123',
+          '871123',
+          '881123',
+          '891123',
+        ],
+      },
       {
         locale: 'BG',
         valid: [