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

Fix required rule validation for missing fields #14

Merged
merged 1 commit into from
May 18, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Rules/RequiredRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ public function passes(string $field, $value, array $data): bool
{
// Set the field property for use in the message method.
$this->field = $field;

$data[$field] = isset($data[$field]) ? $data[$field] : '';
// Check if the field is set in the data and not empty.
return isset($data[$field]) && !empty($data[$field]);
return !empty($data[$field]);
}

/**
Expand Down
6 changes: 4 additions & 2 deletions src/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,9 @@ protected function validate()
*/
protected function checkPasses(mixed $validator, string $field, ?string $ruleName = null)
{
if (isset($this->data[$field]) && !$validator->passes($field, $this->data[$field], $this->data)) {
if(!isset($this->data[$field]) && $ruleName !== 'required') return ;

if (!$validator->passes($field, $this->data[$field], $this->data)) {

$assert = isset($ruleName) && isset($this->messages[$field][$ruleName]);

Expand All @@ -140,7 +142,7 @@ protected function checkPasses(mixed $validator, string $field, ?string $ruleNam
*/
protected function validateConstructorInputs()
{
if (empty($this->data)) {
if (!isset($this->data)) {
throw new ValidatorException(
LangManager::getTranslation('validation.empty_data')
);
Expand Down
6 changes: 6 additions & 0 deletions tests/Feature/RuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

$validator = new Validator(['field' => ''], ['field' => 'required']);
expect($validator->isValid())->toBeFalse();

$validator = new Validator([], ['field' => 'required']);
expect($validator->isValid())->toBeFalse();

expect($validator->getErrors()['field'][0])->toBe(
LangManager::getTranslation('validation.required_rule', ['attribute' => 'field'])
Expand All @@ -21,6 +24,9 @@

$validator = new Validator(['username' => 'value'], ['username' => 'max:5']);
expect($validator->isValid())->toBeTrue();

$validator = new Validator([], ['username' => 'max:5']);
expect($validator->isValid())->toBeTrue();

$validator = new Validator(['username' => 'value_long'], ['username' => 'max:5']);
expect($validator->isValid())->toBeFalse();
Expand Down
8 changes: 4 additions & 4 deletions tests/Feature/ValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
use BlakvGhost\PHPValidator\Validator;
use BlakvGhost\PHPValidator\ValidatorException;

it('throws exception if data is empty', function () {
expect(fn () => new Validator([], ['name' => 'string']))
->toThrow(ValidatorException::class, LangManager::getTranslation('validation.empty_data'));
});
// it('throws exception if data is empty', function () {
// expect(fn () => new Validator([], ['name' => 'string']))
// ->toThrow(ValidatorException::class, LangManager::getTranslation('validation.empty_data'));
// });

it('throws exception if rules are empty', function () {
expect(fn () => new Validator(['name' => 'John'], []))
Expand Down
Loading