1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-12 14:12:44 +01:00

Convest rule to validationrule

This commit is contained in:
David Bomba 2023-12-26 13:54:27 +11:00
parent 3e4cf6f261
commit a88b4d5d8d
2 changed files with 42 additions and 18 deletions

View File

@ -11,32 +11,26 @@
namespace App\Http\ValidationRules\Account;
use Illuminate\Contracts\Validation\Rule;
use Closure;
use Illuminate\Contracts\Validation\ValidationRule;
/**
* Class EmailBlackListRule.
*/
class EmailBlackListRule implements Rule
class EmailBlackListRule implements ValidationRule
{
public array $blacklist = [
'noddy@invoiceninja.com',
];
/**
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value)
public function validate(string $attribute, mixed $value, Closure $fail): void
{
return ! in_array($value, $this->blacklist);
if (in_array($value, $this->blacklist)) {
$fail('This email address is blacklisted, if you think this is in error, please email contact@invoiceninja.com');
}
}
/**
* @return string
*/
public function message()
{
return 'This email address is blacklisted, if you think this is in error, please email contact@invoiceninja.com';
}
}

View File

@ -11,8 +11,9 @@
namespace Tests\Unit\ValidationRules;
use App\Http\ValidationRules\Account\BlackListRule;
use Tests\TestCase;
use App\Http\ValidationRules\Account\BlackListRule;
use App\Http\ValidationRules\Account\EmailBlackListRule;
/**
* @test
@ -25,6 +26,35 @@ class BlacklistValidationTest extends TestCase
parent::setUp();
}
public function testValidEmailRule3()
{
$rules = [
'email' => [new EmailBlackListRule],
];
$data = [
'email' => 'contact@invoiceninja.com',
];
$v = $this->app['validator']->make($data, $rules);
$this->assertTrue($v->passes());
}
public function testValidEmailRule2()
{
$rules = [
'email' => [new EmailBlackListRule],
];
$data = [
'email' => 'noddy@invoiceninja.com',
];
$v = $this->app['validator']->make($data, $rules);
$this->assertFalse($v->passes());
}
public function testValidEmailRule()
{
$rules = [