Skip to content

Commit 01c17fc

Browse files
committed
Setup library config validation
1 parent 970d910 commit 01c17fc

File tree

5 files changed

+90
-7
lines changed

5 files changed

+90
-7
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
"nesbot/carbon": "^2.0|^3.0",
3232
"illuminate/container": "^10.0|^11.0",
3333
"illuminate/database": "^10.0|^11.0",
34-
"ramsey/uuid": "^4.0"
34+
"ramsey/uuid": "^4.0",
35+
"ashallendesign/laravel-config-validator": "^2.6.1"
3536
},
3637
"require-dev": {
3738
"mockery/mockery": "^1.0",

config/door-access.php

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@
3636
| Code format
3737
|--------------------------------------------------------------------------
3838
|
39-
| Define the format of the code. The options are 'numeric', 'alphanumeric' or 'mixed'.
39+
| Define the format of the code. The options are
40+
| 'numeric', 'alphanumeric' or 'mixed'.
4041
| The default for numeric format is '0123456789'.
4142
|
4243
*/
@@ -49,13 +50,27 @@
4950
|--------------------------------------------------------------------------
5051
|
5152
| Define the characters of the generated code.
52-
| The code generator dynamically selects the appropriate character set depending on the chosen code format.
53+
| The code generator dynamically selects the appropriate
54+
| character set depending on the chosen code format.
5355
|
5456
*/
5557

5658
'numeric_characters' => '0123456789', // Default for numeric format.
57-
'alphanumeric_characters' => '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', // Default for alphanumeric format.
58-
'mixed_characters' => '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()-_=+[]{};:<>,.?/', // Combined set.
59+
'alphanumeric_characters' => '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
60+
'mixed_characters' => '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()-_=+[]{};:<>,.?/',
61+
62+
/*
63+
|--------------------------------------------------------------------------
64+
| Config Validation
65+
|--------------------------------------------------------------------------
66+
|
67+
| Choose whether you want the config to be validated. This
68+
| can be useful for ensuring that your config values are
69+
| safe to use.
70+
|
71+
*/
72+
73+
'validate_config' => true,
5974

6075
/*
6176
|--------------------------------------------------------------------------

src/Classes/Validation.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Veeqtoh\DoorAccess\Classes;
6+
7+
use AshAllenDesign\ConfigValidator\Exceptions\InvalidConfigValueException;
8+
use AshAllenDesign\ConfigValidator\Services\ConfigValidator;
9+
use AshAllenDesign\ConfigValidator\Services\Rule;
10+
use Veeqtoh\DoorAccess\Exceptions\ValidationException;
11+
12+
class Validation
13+
{
14+
/**
15+
* Validate all the config related to the library.
16+
*
17+
* @throws ValidationException
18+
* @throws InvalidConfigValueException
19+
*/
20+
public function validateConfig(): bool
21+
{
22+
$validator = app(ConfigValidator::class);
23+
24+
$passes = $validator
25+
->throwExceptionOnFailure(false)
26+
->runInline([
27+
'door-access' => [
28+
Rule::make('code_length')->rules(['required', 'integer', 'min:6', 'max:19']),
29+
Rule::make('character_repeated_limit')->rules(['required', 'integer', 'min:3', 'max:19']),
30+
Rule::make('sequence_length_limit')->rules(['required', 'integer', 'min:3']),
31+
Rule::make('unique_characters_limit')->rules(['required', 'integer', 'min:3']),
32+
],
33+
]);
34+
35+
if (! $passes) {
36+
$validationMessage = $validator->errors()[array_key_first($validator->errors())][0];
37+
38+
throw new ValidationException($validationMessage);
39+
}
40+
41+
return $passes;
42+
}
43+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Veeqtoh\DoorAccess\Exceptions;
6+
7+
use Exception;
8+
9+
/**
10+
* class ValidationException
11+
* This class provides the exception for library config validations.
12+
*
13+
* @package Veeqtoh\DoorAccess\Exceptions
14+
*/
15+
class ValidationException extends Exception
16+
{
17+
//
18+
}

src/Providers/DoorAccessProvider.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
namespace Veeqtoh\DoorAccess\Providers;
55

66
use Illuminate\Support\ServiceProvider;
7+
use Veeqtoh\DoorAccess\Classes\Validation;
78

89
/**
910
* class DoorAccessProvider
@@ -27,16 +28,21 @@ public function register(): void
2728
*
2829
* @return void
2930
*/
30-
public function boot()
31+
public function boot(): void
3132
{
3233
// Publish the package configuration file to the Laravel application.
3334
$this->publishes([
3435
__DIR__ . '/../../config/door-access.php' => config_path('door-access.php'),
3536
], 'config');
3637

37-
// Migrate the package's database tables.
38+
// Publish the package's migrations.
3839
$this->publishes([
3940
__DIR__.'/../../database/migrations' => database_path('migrations'),
4041
], 'door-access-migrations');
42+
43+
// Validate the library configs or not.
44+
if (config('door-access') && config('door-access.validate_config')) {
45+
(new Validation())->validateConfig();
46+
}
4147
}
4248
}

0 commit comments

Comments
 (0)