Skip to content

Commit

Permalink
Merge pull request #14 from arifszn/dev
Browse files Browse the repository at this point in the history
Merge dev into main(1.0.1)
  • Loading branch information
arifszn authored Mar 3, 2022
2 parents a60034d + 74dedef commit 25ce5a0
Show file tree
Hide file tree
Showing 10 changed files with 620 additions and 4 deletions.
147 changes: 145 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ php artisan vendor:publish --provider="Arifszn\AdvancedValidation\ServiceProvide
You can specify the error message on the fly when declaring the rules. Simple pass the error message parameter.

```php
use Arifszn\AdvancedValidation\Rules\Base64Image;
use Arifszn\AdvancedValidation\Rules\Username;

public function rules()
{
return [
'avatar' => [new Base64Image('Your custom error message')],
'foo' => [new Username('Your custom error message')],
];
}
```
Expand All @@ -52,15 +52,23 @@ public function rules()
- [`Divisible By`](#divisibleby)
- [`Ethereum Address`](#ethereumaddress)
- [`Float Number`](#floatnumber)
- [`Hash`](#hash)
- [`Image URL`](#imageurl)
- [`JWT`](#jwt)
- [`Name`](#name)
- [`Phone`](#phone)
- [`Username`](#username)
- [`Without Spaces`](#withoutspaces)


### `Ascii`

The field under validation must contain ASCII chars only.

```
public Arifszn\AdvancedValidation\Rules\Ascii::__construct(string $errorMessage = null)
```

```php
use Arifszn\AdvancedValidation\Rules\Ascii;

Expand All @@ -76,6 +84,10 @@ public function rules()

The field under validation must be a Base64 encoded image.

```
public Arifszn\AdvancedValidation\Rules\Base64Image::__construct(string $errorMessage = null)
```

```php
use Arifszn\AdvancedValidation\Rules\Base64Image;

Expand All @@ -91,6 +103,10 @@ public function rules()

The field under validation must be a Base64 encoded string.

```
public Arifszn\AdvancedValidation\Rules\Base64String::__construct(string $errorMessage = null)
```

```php
use Arifszn\AdvancedValidation\Rules\Base64String;

Expand All @@ -106,6 +122,10 @@ public function rules()

The field under validation must be a BIC([Business Identifier Code](https://en.wikipedia.org/wiki/ISO_9362)) or SWIFT code.

```
public Arifszn\AdvancedValidation\Rules\BIC::__construct(string $errorMessage = null)
```

```php
use Arifszn\AdvancedValidation\Rules\BIC;

Expand All @@ -121,6 +141,10 @@ public function rules()

The field under validation must be a valid BTC address.

```
public Arifszn\AdvancedValidation\Rules\BtcAddress::__construct(string $errorMessage = null)
```

```php
use Arifszn\AdvancedValidation\Rules\BtcAddress;

Expand All @@ -136,6 +160,10 @@ public function rules()

The field under validation must be a valid credit card number.

```
public Arifszn\AdvancedValidation\Rules\CreditCard::__construct(string $errorMessage = null)
```

```php
use Arifszn\AdvancedValidation\Rules\CreditCard;

Expand All @@ -151,6 +179,10 @@ public function rules()

The field under validation must have [data uri format](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs).

```
public Arifszn\AdvancedValidation\Rules\DataURI::__construct(string $errorMessage = null)
```

```php
use Arifszn\AdvancedValidation\Rules\DataURI;

Expand All @@ -166,6 +198,10 @@ public function rules()

The field under validation must be divisible by the given number.

```
public Arifszn\AdvancedValidation\Rules\DivisibleBy::__construct(int $number, string $errorMessage = null)
```

```php
use Arifszn\AdvancedValidation\Rules\DivisibleBy;

Expand All @@ -180,6 +216,10 @@ public function rules()
### `EthereumAddress`
The field under validation must be an [Ethereum](https://ethereum.org/en/) address. Does not validate address checksums.

```
public Arifszn\AdvancedValidation\Rules\EthereumAddress::__construct(string $errorMessage = null)
```

```php
use Arifszn\AdvancedValidation\Rules\EthereumAddress;

Expand All @@ -195,6 +235,10 @@ public function rules()

The field under validation must be a float number.

```
public Arifszn\AdvancedValidation\Rules\FloatNumber::__construct(string $errorMessage = null)
```

```php
use Arifszn\AdvancedValidation\Rules\FloatNumber;

Expand All @@ -206,13 +250,38 @@ public function rules()
}
```

### `Hash`

The field under validation must be a hash of type algorithm.

Algorithm is one of `'md4', 'md5', 'sha1', 'sha256', 'sha384', 'sha512', 'ripemd128', 'ripemd160', 'tiger128', 'tiger160', 'tiger192', 'crc32', 'crc32b'`.

```
public Arifszn\AdvancedValidation\Rules\Hash::__construct(string $algorithm, string $errorMessage = null)
```

```php
use Arifszn\AdvancedValidation\Rules\Hash;

public function rules()
{
return [
'foo' => [new Hash('md4')],
];
}
```

### `ImageURL`

The field under validation must be a valid image URL.

https://www.php.net/images/logos/php-logo.png \
https://imaginarysite123.com/invalid.png

```
public Arifszn\AdvancedValidation\Rules\ImageURL::__construct(string $errorMessage = null)
```

```php
use Arifszn\AdvancedValidation\Rules\ImageURL;

Expand All @@ -224,6 +293,48 @@ public function rules()
}
```

### `JWT`

The field under validation must have a valid format of JWT ([JSON Web Token](https://en.wikipedia.org/wiki/JSON_Web_Token)).

```
public Arifszn\AdvancedValidation\Rules\Jwt::__construct(string $errorMessage = null)
```

```php
use Arifszn\AdvancedValidation\Rules\Jwt;

public function rules()
{
return [
'foo' => [new Jwt()],
];
}
```

### `Name`

The field under validation must be a valid name.

- no emoji
- no number (if $allowNumber flag is true, it will accept numbers, default is false)
- special characters are allowed (restricting special characters will cause false-negative for names like `Martin Luther King, Jr.` or `李小龍`)

```
public Arifszn\AdvancedValidation\Rules\Name::__construct(bool $allowNumber = false, string $errorMessage = null)
```

```php
use Arifszn\AdvancedValidation\Rules\Name;

public function rules()
{
return [
'name' => [new Name()],
];
}
```

### `Phone`

The field under validation must be a valid phone number.
Expand All @@ -233,6 +344,10 @@ The field under validation must be a valid phone number.
✓ (xxx) xxx-xxxx \
✓ xxxxxxxxxx

```
public Arifszn\AdvancedValidation\Rules\Phone::__construct(string $errorMessage = null)
```

```php
use Arifszn\AdvancedValidation\Rules\Phone;

Expand All @@ -244,10 +359,38 @@ public function rules()
}
```

### `Username`

The field under validation must be a valid username.

- starts with a letter (alpha)
- only alpha-numeric (a-z, A-Z, 0-9), underscore, minus and dot
- multiple underscores, minus and are not allowed (-- or __ or ..)
- underscores, minus and dot are not allowed at the beginning or end

```
public Arifszn\AdvancedValidation\Rules\Username::__construct(string $errorMessage = null)
```

```php
use Arifszn\AdvancedValidation\Rules\Username;

public function rules()
{
return [
'username' => [new Username()],
];
}
```

### `WithoutSpaces`

The field under validation must not contain spaces.

```
public Arifszn\AdvancedValidation\Rules\WithoutSpaces::__construct(string $errorMessage = null)
```

```php
use Arifszn\AdvancedValidation\Rules\WithoutSpaces;

Expand Down
7 changes: 5 additions & 2 deletions resources/lang/en/validation.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

<?php

return [
Expand All @@ -11,8 +10,12 @@
'data_uri' => 'The :attribute must have data uri format.',
'divisible_by' => 'The :attribute must be divisible by :number.',
'ethereum_address' => 'The :attribute must be an Ethereum address.',
'float_number' => 'The :attribute must be a float number',
'float_number' => 'The :attribute must be a float number.',
'hash' => 'The :attribute must be a hash of :algorithm algorithm.',
'image_url' => 'The :attribute must be a valid image URL.',
'jwt' => 'The :attribute must have a valid format of JWT.',
'name' => 'The :attribute must be a valid name.',
'phone' => 'The :attribute must be a valid phone number.',
'username' => 'The :attribute must be a valid username.',
'without_spaces' => 'The :attribute must not contain spaces.',
];
103 changes: 103 additions & 0 deletions src/Rules/Hash.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php

namespace Arifszn\AdvancedValidation\Rules;

use Illuminate\Contracts\Validation\Rule;

/**
* The field under validation must be a hash of type algorithm. Algorithm is one of
* ['md4', 'md5', 'sha1', 'sha256', 'sha384', 'sha512', 'ripemd128', 'ripemd160',
* 'tiger128', 'tiger160', 'tiger192', 'crc32', 'crc32b']
*
* @package Arifszn\AdvancedValidation\Rules
*/
class Hash implements Rule
{
/**
* @var string
*/
private $algorithm;

/**
* @var string
*/
private $errorMessage;

/**
* @var string
*/
private $attribute;

/**
* Create a new rule instance.
*
* @param string $algorithm 'md4' | 'md5' | 'sha1' | 'sha256' | 'sha384' | 'sha512' | 'ripemd128' | 'ripemd160' | 'tiger128' | 'tiger160' | 'tiger192' | 'crc32' | 'crc32b'
* @param string|null $errorMessage Custom error message.
* @return void
*/
public function __construct(string $algorithm, string $errorMessage = null)
{
$this->algorithm = $algorithm;
$this->errorMessage = $errorMessage;
}

/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value)
{
$this->attribute = $attribute;

try {
$hash = '/^[a-fA-F0-9]{' . $this->getLength($this->algorithm) . '}$/';

return preg_match($hash, $value);
} catch (\Throwable $th) {
return false;
}
}

/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
return $this->errorMessage ? $this->errorMessage : trans('advancedValidation::validation.hash', [
'attribute' => $this->attribute,
'algorithm' => $this->algorithm
]);
}

/**
* Get length.
*
* @param string $algorithm
* @return int
*/
private function getLength(string $algorithm)
{
$lengths = [
'md5' => 32,
'md4' => 32,
'sha1' => 40,
'sha256' => 64,
'sha384' => 96,
'sha512' => 128,
'ripemd128' => 32,
'ripemd160' => 40,
'tiger128' => 32,
'tiger160' => 40,
'tiger192' => 48,
'crc32' => 8,
'crc32b' => 8,
];

return $lengths[$algorithm];
}
}
Loading

0 comments on commit 25ce5a0

Please sign in to comment.