diff --git a/src/Validator.php b/src/Validator.php index 007d066..9bf9a41 100644 --- a/src/Validator.php +++ b/src/Validator.php @@ -4,18 +4,18 @@ namespace PIN; class Validator { static $regexes; - + public static function validate(string $pin) { if(!self::$regexes) { self::$regexes = array_filter(file('regex.txt')); } foreach (self::$regexes as $regex) { - if (preg_match($regex, $pin) === 1) { + if (strlen($pin) === 6 and preg_match($regex, $pin) === 1) { return true; } } return false; } -} \ No newline at end of file +} diff --git a/src/index.js b/src/index.js index 27c6d44..704d223 100644 --- a/src/index.js +++ b/src/index.js @@ -1,12 +1,17 @@ const readline = require("readline"); const fs = require("fs"); -const regexes = fs.readFileSync(__dirname + '/../regex.txt', 'utf8') +const regexes = fs + .readFileSync(__dirname + "/../regex.txt", "utf8") .split("\n") // Remove empty lines - .filter(function(r) {return r.length > 1}) + .filter(function(r) { + return r.length > 1; + }) // Remove the opening and closing slashes - .map(function(r) {return new RegExp(r.slice(1,-1));}) + .map(function(r) { + return new RegExp("^" + r.slice(1, -1) + "$"); + }); module.exports = { validate: function(pin) { diff --git a/src/pincode/__init__.py b/src/pincode/__init__.py index 0b63fd8..8d1d282 100644 --- a/src/pincode/__init__.py +++ b/src/pincode/__init__.py @@ -1,12 +1,14 @@ +import os import re regex = list() -f = open('regex.txt') +dir_path = os.path.dirname(os.path.realpath(__file__)) +f = open(dir_path + '/../../regex.txt') lines = f.readlines() for line in lines: if (len(line) > 10): # Remove the \n at the end - regex.append(re.compile(line[1:-2])) + regex.append(re.compile('^' + line[1:-2] + '$')) class Pincode: @staticmethod diff --git a/tests/SimpleTest.php b/tests/SimpleTest.php index 7956aa9..ba99cba 100644 --- a/tests/SimpleTest.php +++ b/tests/SimpleTest.php @@ -6,7 +6,7 @@ use PHPUnit\Framework\TestCase; class SimpleTest extends TestCase { const PINS = ['244713', '560029', '560030', '110011']; - const INVALID_PINS = ['999999', '99999', '9999', '999', '99', '9', '111111']; + const INVALID_PINS = ['999999', '99999', '9999', '999', '99', '9', '111111', '2447131']; public function testSamplePins() { foreach(self::PINS as $pin) { diff --git a/tests/test.py b/tests/test.py index 1ed4970..b4ff64c 100644 --- a/tests/test.py +++ b/tests/test.py @@ -7,6 +7,6 @@ def test_valid_pincodes(): assert_equals(True, Pincode.validate(pin), pin + " should be valid") def test_invalid_pincodes(): - INVALID_PINS = ['999999', '99999', '9999', '999', '99', '9', '111111']; + INVALID_PINS = ['999999', '99999', '9999', '999', '99', '9', '111111', '2447131']; for pin in INVALID_PINS: assert_equals(False, Pincode.validate(pin), pin + " should be invalid") diff --git a/tests/validate.js b/tests/validate.js index 5ae47b4..3ccad7c 100644 --- a/tests/validate.js +++ b/tests/validate.js @@ -9,6 +9,7 @@ assert.strictEqual(pincode.validate('560029'), true, '560029'); assert.strictEqual(pincode.validate('560030'), true, '560030'); // Incorrect +assert.strictEqual(pincode.validate('1100111'), false, '1100111'); assert.strictEqual(pincode.validate('111111'), false, '111111'); assert.strictEqual(pincode.validate('999999'), false, '999999'); assert.strictEqual(pincode.validate('99999'), false, '99999');