Skip to content

Commit

Permalink
[11.x] Remove deprecated interface Rule implementation with Validatio…
Browse files Browse the repository at this point in the history
…nRule in Password rule validation
  • Loading branch information
Sohel Rana committed Jan 29, 2024
1 parent 50c96bd commit 7899ac7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 52 deletions.
62 changes: 19 additions & 43 deletions src/Illuminate/Validation/Rules/Password.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@

namespace Illuminate\Validation\Rules;

use Closure;
use Illuminate\Container\Container;
use Illuminate\Contracts\Validation\DataAwareRule;
use Illuminate\Contracts\Validation\Rule;
use Illuminate\Contracts\Validation\UncompromisedVerifier;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Contracts\Validation\ValidatorAwareRule;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Traits\Conditionable;
use InvalidArgumentException;

class Password implements Rule, DataAwareRule, ValidatorAwareRule
class Password implements ValidationRule, DataAwareRule, ValidatorAwareRule
{
use Conditionable;

Expand Down Expand Up @@ -150,7 +151,7 @@ public static function default()
? call_user_func(static::$defaultCallback)
: static::$defaultCallback;

return $password instanceof Rule ? $password : static::min(8);
return $password instanceof ValidationRule ? $password : static::min(8);
}

/**
Expand Down Expand Up @@ -300,30 +301,30 @@ public function rules($rules)
}

/**
* Determine if the validation rule passes.
* Run the validation rule.
*
* @param string $attribute
* @param mixed $value
* @return bool
* @param \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail
* @return void
*/
public function passes($attribute, $value)
public function validate(string $attribute, mixed $value, Closure $fail): void
{
$this->messages = [];

$validator = Validator::make(
$this->data,
[$attribute => array_merge(['string', 'min:'.$this->min], $this->customRules)],
[$attribute => [
'string',
'min:'.$this->min,
...($this->max ? ['max:'.$this->max] : []),
...$this->customRules,
]],
$this->validator->customMessages,
$this->validator->customAttributes
)->after(function ($validator) use ($attribute, $value) {
if (! is_string($value)) {
return;
}

if ($this->max && mb_strlen($value) > $this->max) {
$validator->addFailure($attribute, 'max.string');
}

if ($this->mixedCase && ! preg_match('/(\p{Ll}+.*\p{Lu})|(\p{Lu}+.*\p{Ll})/u', $value)) {
$validator->addFailure($attribute, 'password.mixed');
}
Expand All @@ -342,41 +343,16 @@ public function passes($attribute, $value)
});

if ($validator->fails()) {
return $this->fail($validator->messages()->all());
foreach ($validator->messages()->all() as $message) {
$fail($message);
}
}

if ($this->uncompromised && ! Container::getInstance()->make(UncompromisedVerifier::class)->verify([
if (! $validator->fails() && $this->uncompromised && ! Container::getInstance()->make(UncompromisedVerifier::class)->verify([
'value' => $value,
'threshold' => $this->compromisedThreshold,
])) {
$validator->addFailure($attribute, 'password.uncompromised');

return $this->fail($validator->messages()->all());
$fail('validation.password.uncompromised')->translate();
}

return true;
}

/**
* Get the validation error message.
*
* @return array
*/
public function message()
{
return $this->messages;
}

/**
* Adds the given failures, and return false.
*
* @param array|string $messages
* @return bool
*/
protected function fail($messages)
{
$this->messages = array_merge($this->messages, Arr::wrap($messages));

return false;
}
}
16 changes: 7 additions & 9 deletions tests/Validation/ValidationPasswordRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

namespace Illuminate\Tests\Validation;

use Closure;
use Illuminate\Container\Container;
use Illuminate\Contracts\Validation\Rule as RuleContract;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Support\Facades\Facade;
use Illuminate\Translation\ArrayLoader;
use Illuminate\Translation\Translator;
Expand Down Expand Up @@ -312,16 +313,13 @@ public function testPassesWithCustomRules()
}
};

$ruleObject = new class implements RuleContract
$ruleObject = new class implements ValidationRule
{
public function passes($attribute, $value)
public function validate(string $attribute, mixed $value, Closure $fail): void
{
return $value === 'aa';
}

public function message()
{
return 'Custom rule object failed';
if ($value !== 'aa') {
$fail('Custom rule object failed');
}
}
};

Expand Down

0 comments on commit 7899ac7

Please sign in to comment.