diff --git a/js/validators/FileSizeValidator.js b/js/validators/FileSizeValidator.js index 0e8f542..0ee070c 100644 --- a/js/validators/FileSizeValidator.js +++ b/js/validators/FileSizeValidator.js @@ -19,7 +19,7 @@ export class FileSizeValidator extends Validator validate(value) { - if(value.hasOwnProperty('size') && value.size > (this._maxSize * 1024 * 1024)) + if('size' in value && value.size > (this._maxSize * 1024 * 1024)) { return ValidationResponse.error(['File upload cannot be more than ' + this._maxSize + 'mb in size']); } diff --git a/package.json b/package.json index 62dabf9..ea28e03 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@packaged/validate", - "version": "3.1.3", + "version": "3.1.4", "main": "index.js", "repository": "git@github.com:packaged/validate", "author": "Tom Kay ", diff --git a/src/Validators/FileSizeValidator.php b/src/Validators/FileSizeValidator.php index bc2fa4b..8f108f7 100644 --- a/src/Validators/FileSizeValidator.php +++ b/src/Validators/FileSizeValidator.php @@ -29,14 +29,14 @@ public static function deserialize($configuration): SerializableValidator public function serialize(): array { return [ - 'maxSize' => $this->_maxSize, + 'maxSize' => $this->getMaxSize(), ]; } protected function _doValidate($value): Generator { // Validation - if(array_key_exists('size', $value) && $value['size'] < ($this->_maxSize * 1024 * 1024)) + if(is_array($value) && array_key_exists('size', $value) && $value['size'] > ($this->_maxSize * 1024 * 1024)) { yield $this->_makeError("File upload cannot be more than " . $this->_maxSize . "mb in size"); } diff --git a/tests/FileSizeValidatorTest.php b/tests/FileSizeValidatorTest.php new file mode 100644 index 0000000..a2a8733 --- /dev/null +++ b/tests/FileSizeValidatorTest.php @@ -0,0 +1,19 @@ + 1159144]; + $validator = new FileSizeValidator(3); + + $largeUpload = ['size' => 5123224]; + + $this->assertTrue($validator->isValid($upload)); + $this->assertFalse($validator->isValid($largeUpload)); + } +}