Skip to content

Commit a60034d

Browse files
authored
Merge pull request #9 from arifszn/feat/rule-EthereumAddress
feat: rule ethereum address
2 parents 751e0bf + e299e30 commit a60034d

File tree

4 files changed

+107
-0
lines changed

4 files changed

+107
-0
lines changed

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public function rules()
5050
- [`Credit Card`](#creditcard)
5151
- [`Data URI`](#datauri)
5252
- [`Divisible By`](#divisibleby)
53+
- [`Ethereum Address`](#ethereumaddress)
5354
- [`Float Number`](#floatnumber)
5455
- [`Image URL`](#imageurl)
5556
- [`Phone`](#phone)
@@ -176,6 +177,20 @@ public function rules()
176177
}
177178
```
178179

180+
### `EthereumAddress`
181+
The field under validation must be an [Ethereum](https://ethereum.org/en/) address. Does not validate address checksums.
182+
183+
```php
184+
use Arifszn\AdvancedValidation\Rules\EthereumAddress;
185+
186+
public function rules()
187+
{
188+
return [
189+
'foo' => [new EthereumAddress()],
190+
];
191+
}
192+
```
193+
179194
### `FloatNumber`
180195

181196
The field under validation must be a float number.
@@ -281,6 +296,11 @@ composer test
281296
Any contributors who want to make this project better can make contributions, which will be greatly appreciated. To contribute, clone this repo locally and commit your code to a new branch. Feel free to create an issue or make a pull request.
282297

283298

299+
## Credits
300+
301+
- [validator.js](https://github.com/validatorjs/validator.js)
302+
303+
284304
## Support
285305

286306
If you are using this package and happy with it or just want to encourage me to continue creating stuff, you can do it by just starring and sharing the project.

resources/lang/en/validation.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
'credit_card' => 'The :attribute must be a valid credit card number.',
1111
'data_uri' => 'The :attribute must have data uri format.',
1212
'divisible_by' => 'The :attribute must be divisible by :number.',
13+
'ethereum_address' => 'The :attribute must be an Ethereum address.',
1314
'float_number' => 'The :attribute must be a float number',
1415
'image_url' => 'The :attribute must be a valid image URL.',
1516
'phone' => 'The :attribute must be a valid phone number.',

src/Rules/EthereumAddress.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace Arifszn\AdvancedValidation\Rules;
4+
5+
use Illuminate\Contracts\Validation\Rule;
6+
7+
/**
8+
* The field under validation must be an Ethereum address. Does not validate address checksums.
9+
*
10+
* @package Arifszn\AdvancedValidation\Rules
11+
*/
12+
class EthereumAddress implements Rule
13+
{
14+
/**
15+
* @var string
16+
*/
17+
private $errorMessage;
18+
19+
/**
20+
* Create a new rule instance.
21+
*
22+
* @param string|null $errorMessage Custom error message.
23+
* @return void
24+
*/
25+
public function __construct(string $errorMessage = null)
26+
{
27+
$this->errorMessage = $errorMessage;
28+
}
29+
30+
/**
31+
* Determine if the validation rule passes.
32+
*
33+
* @param string $attribute
34+
* @param mixed $value
35+
* @return bool
36+
*/
37+
public function passes($attribute, $value)
38+
{
39+
return preg_match('/^(0x)[0-9a-f]{40}$/i', $value);
40+
}
41+
42+
/**
43+
* Get the validation error message.
44+
*
45+
* @return string
46+
*/
47+
public function message()
48+
{
49+
return $this->errorMessage ? $this->errorMessage : trans('advancedValidation::validation.ethereum_address');
50+
}
51+
}

tests/Rules/EthereumAddressTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Arifszn\AdvancedValidation\Tests\Rules;
4+
5+
use Arifszn\AdvancedValidation\Rules\EthereumAddress;
6+
use Arifszn\AdvancedValidation\Tests\TestCase;
7+
8+
class EthereumAddressTest extends TestCase
9+
{
10+
/**
11+
* @dataProvider provider
12+
*/
13+
public function testValidation($result, $value)
14+
{
15+
$this->assertEquals($result, (new EthereumAddress())->passes('foo', $value));
16+
}
17+
18+
public function provider()
19+
{
20+
return [
21+
[true, '0x0000000000000000000000000000000000000001'],
22+
[true, '0x683E07492fBDfDA84457C16546ac3f433BFaa128'],
23+
[true, '0x88dA6B6a8D3590e88E0FcadD5CEC56A7C9478319'],
24+
[true, '0x8a718a84ee7B1621E63E680371e0C03C417cCaF6'],
25+
[true, '0xFCb5AFB808b5679b4911230Aa41FfCD0cd335b42'],
26+
27+
[false, '0xGHIJK05pwm37asdf5555QWERZCXV2345AoEuIdHt'],
28+
[false, '0xFCb5AFB808b5679b4911230Aa41FfCD0cd335b422222'],
29+
[false, '0xFCb5AFB808b5679b4911230Aa41FfCD0cd33'],
30+
[false, '0b0110100001100101011011000110110001101111'],
31+
[false, '683E07492fBDfDA84457C16546ac3f433BFaa128'],
32+
[false, '1C6o5CDkLxjsVpnLSuqRs1UBFozXLEwYvU'],
33+
];
34+
}
35+
}

0 commit comments

Comments
 (0)