-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Validation system for settings data
- Loading branch information
Showing
18 changed files
with
163 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
language: php | ||
|
||
php: | ||
- 7.2 | ||
- 7.3 | ||
- 7.4 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,7 +41,7 @@ | |
<a title="PHP Version" href="#"><img src="https://img.shields.io/packagist/php-v/glorand/laravel-model-settings" alt="PHP Version" /></a> | ||
</p> | ||
|
||
The package requires PHP 7.2+ and follows the FIG standards PSR-1, PSR-2 and PSR-4 | ||
The package requires PHP 7.3+ and follows the FIG standards PSR-1, PSR-2, PSR-4 and PSR-12 | ||
to ensure a high level of interoperability between shared PHP. | ||
|
||
Bug reports, feature requests, and pull requests can be submitted by following our [Contribution Guide](CONTRIBUTING.md). | ||
|
@@ -62,7 +62,8 @@ Bug reports, feature requests, and pull requests can be submitted by following o | |
- [Check if the model has a specific setting](#check) | ||
- [Remove a setting from a model](#remove) | ||
- [Persistence](#persistence) | ||
- [Using another method name other than settings()](#invokeSettingsBy) | ||
- [Using another method name other than `settings()`](#invokeSettingsBy) | ||
- [Validation system for settings data](#validation) | ||
- [Changelog](#changelog) | ||
- [Contributing](#contributing) | ||
- [License](#license) | ||
|
@@ -265,6 +266,40 @@ If you prefer to use another name other than `settings` , | |
you can do so by defining a `$invokeSettingsBy` property. | ||
This forward calls (such as `configurations()`) to the `settings()` method. | ||
|
||
### Validation system for settings data <a name="validation></a> | ||
When you're using the set() or apply()|update() methods thrown an exception when you break a rule. | ||
You can define rules on model using `$settingsRules` public property, and the rules array definition is identical with | ||
the Laravel default validation rules. ([see Laravel rules](https://laravel.com/docs/8.x/validation#available-validation-rules)) | ||
```php | ||
class User extends Model | ||
{ | ||
use HasSettingsTable; | ||
|
||
public array $defaultSettings = [ | ||
'user' => [ | ||
'name' => 'Test User', | ||
'email' => '[email protected]' | ||
'age' => 27, | ||
], | ||
'language' => 'en', | ||
'max_size' => 12, | ||
]; | ||
|
||
// settings rules | ||
public array $settingsRules = [ | ||
'user' => 'array', | ||
'user.email' => [ | ||
'string', | ||
'email', | ||
], | ||
'user.age' => 'integer', | ||
'language' => 'string|in:en,es,it|max:2', | ||
'max_size' => 'int|min:5|max:15', | ||
]; | ||
} | ||
|
||
``` | ||
|
||
## Changelog <a name="changelog"></a> | ||
Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently. | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
use Glorand\Model\Settings\Traits\HasSettingsRedis; | ||
use Glorand\Model\Settings\Traits\HasSettingsTable; | ||
use Illuminate\Support\Facades\Redis; | ||
use Illuminate\Validation\ValidationException; | ||
use Lunaweb\RedisMock\MockPredisConnection; | ||
|
||
class CommonFunctionalityTest extends TestCase | ||
|
@@ -19,13 +20,14 @@ class CommonFunctionalityTest extends TestCase | |
]; | ||
/** @var \string[][] */ | ||
protected $testArray = [ | ||
'user' => [ | ||
'user' => [ | ||
'first_name' => "John", | ||
'last_name' => "Doe", | ||
'email' => "[email protected]", | ||
'last_name' => "Doe", | ||
'email' => "[email protected]", | ||
'age' => 27, | ||
], | ||
'project' => [ | ||
'name' => 'Project One', | ||
'name' => 'Project One', | ||
'description' => 'Test Description', | ||
], | ||
]; | ||
|
@@ -34,10 +36,22 @@ class CommonFunctionalityTest extends TestCase | |
protected $defaultSettingsTestArray = [ | ||
'config' => [ | ||
'email' => 'gmail', | ||
'file' => 'aws', | ||
'file' => 'aws', | ||
], | ||
]; | ||
|
||
/** @var string[] */ | ||
protected $rules = [ | ||
'user' => [ | ||
'array', | ||
], | ||
'user.email' => [ | ||
'string', | ||
'email', | ||
], | ||
'user.age' => 'integer', | ||
]; | ||
|
||
public function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
@@ -105,7 +119,7 @@ public function testHas(string $modelType) | |
); | ||
|
||
$this->assertTrue($model->settings()->has('user.first_name')); | ||
$this->assertFalse($model->settings()->has('user.age')); | ||
$this->assertFalse($model->settings()->has('user.role')); | ||
} | ||
|
||
/** | ||
|
@@ -153,7 +167,7 @@ public function testGetMultiple(string $modelType) | |
[ | ||
'user' => [ | ||
'first_name' => 'def_val', | ||
'last_name' => 'def_val', | ||
'last_name' => 'def_val', | ||
], | ||
], | ||
$values | ||
|
@@ -166,15 +180,16 @@ public function testGetMultiple(string $modelType) | |
); | ||
$this->assertEquals( | ||
[ | ||
'user' => [ | ||
'user' => [ | ||
'first_name' => 'John', | ||
'last_name' => 'Doe', | ||
'email' => '[email protected]', | ||
'last_name' => 'Doe', | ||
'email' => '[email protected]', | ||
'age' => 27, | ||
], | ||
'project' => [ | ||
'name' => 'Project One', | ||
], | ||
'date' => 'def_val', | ||
'date' => 'def_val', | ||
], | ||
$values | ||
); | ||
|
@@ -361,4 +376,32 @@ public function testDefaultValueFromConfig(string $modelType) | |
$model->settings()->all() | ||
); | ||
} | ||
|
||
/** | ||
* @dataProvider modelTypesProvider | ||
*/ | ||
public function testValidateData(string $modelType) | ||
{ | ||
$model = $this->getModelByType($modelType); | ||
$model->settingsRules = $this->rules; | ||
|
||
$model->settings()->clear(); | ||
$this->assertEquals([], $model->settings()->all()); | ||
|
||
$model->settings()->apply($this->testArray); | ||
|
||
$model->settings()->clear(); | ||
$this->assertEquals([], $model->settings()->all()); | ||
|
||
try { | ||
$model->settings()->set('user.age', 'string'); | ||
} catch (ValidationException $e) { | ||
$this->assertArrayHasKey('user.age', $e->errors()); | ||
} | ||
|
||
$testArray = $this->testArray; | ||
$testArray['user']['age'] = 'string'; | ||
$this->expectException(ValidationException::class); | ||
$model->settings()->apply($testArray); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.