Skip to content

Commit

Permalink
Added validation on config
Browse files Browse the repository at this point in the history
  • Loading branch information
balajidharma committed Dec 21, 2024
1 parent ac326ba commit cb62699
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -242,11 +242,12 @@ Disable or enable the value validation based on data type.

```php
'data_types' => [
['type' => 'string', 'cast' => 'string'],
['type' => 'integer', 'cast' => 'integer'],
['type' => 'boolean', 'cast' => 'boolean'],
['type' => 'date', 'cast' => 'date'],
['type' => 'json', 'cast' => 'array'],
['type' => 'string', 'validation' => 'string', 'cast' => 'string'],
['type' => 'integer', 'validation' => 'integer', 'cast' => 'integer'],
['type' => 'float', 'validation' => 'numeric', 'cast' => 'float'],
['type' => 'boolean', 'validation' => 'boolean', 'cast' => 'boolean'],
['type' => 'date', 'validation' => 'date', 'cast' => 'date'],
['type' => 'json', 'validation' => 'json', 'cast' => 'array'],
],
```
Support all the Eloquent [Attribute Casting](https://laravel.com/docs/eloquent-mutators#attribute-casting)
Expand Down
11 changes: 6 additions & 5 deletions config/attributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@
'validate_value_before_save' => true,

'data_types' => [
['type' => 'string', 'cast' => 'string'],
['type' => 'integer', 'cast' => 'integer'],
['type' => 'boolean', 'cast' => 'boolean'],
['type' => 'date', 'cast' => 'date'],
['type' => 'json', 'cast' => 'array'],
['type' => 'string', 'validation' => 'string', 'cast' => 'string'],
['type' => 'integer', 'validation' => 'integer', 'cast' => 'integer'],
['type' => 'float', 'validation' => 'numeric', 'cast' => 'float'],
['type' => 'boolean', 'validation' => 'boolean', 'cast' => 'boolean'],
['type' => 'date', 'validation' => 'date', 'cast' => 'date'],
['type' => 'json', 'validation' => 'json', 'cast' => 'array'],
],
];
4 changes: 3 additions & 1 deletion src/Models/Attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,10 @@ public function setValueAttribute($value)
{
if(Config::get('attributes.validate_value_before_save'))
{
$dataType = collect(Config::get('attributes.data_types'))->firstWhere('type', $this->data_type);

$validator = Validator::make(['value' => $value], [
'value' => $this->data_type
'value' => $dataType['validation']
]);

if ($validator->fails()) {
Expand Down

0 comments on commit cb62699

Please sign in to comment.