Laravel Config Validator is a Laravel package that allows you to validate your config values and environment. The package has been developed and tested to work with the following minimum requirements:
You can install the package via Composer:
composer require ashallendesign/laravel-config-validator
To get you started with validating your app’s config, Laravel Config Validator comes with some default rulesets. To start using these rulesets, you can publish them using the following command:
php artisan vendor:publish --tag=config-validator-defaults
The above command will copy the validation files and place them in a
config-validation folder in your project’s root. These rules are just to get you started, so there are likely going to rule in the files that don’t apply to your app. So, once you’ve published them, feel free to delete them or edit them as much as you’d like.
Using the Generator Command
This package comes with a command that you can use to quickly create a validation file to get you started right away. Let’s say that you wanted to create a validation file for validating the config in the config/app.php file. To do this, you could use the following command:
php artisan make:config-validation app
Running the above command would create a file in
config-validation/app.php ready for you to start adding your config validation.
To validate your application’s config, you need to define the validation rules first. You can do this by placing them inside files in a config-validation folder with names that match the config file you’re validating. As an example, to validate the
config/app.php config file, you would create a new file at config-
validation/app.php that would hold the rules.
Adding Rules to a Ruleset
Once you have your ruleset file created in the
config-validation folder, you can start adding your validation rules.
Under the hood, Laravel Config Validator uses the built-in
Validator class, so it should seem pretty familiar to work with. To check out the available Laravel validation rules that can be used, click here.
As an example, we might want to add a config validation rule to ensure that the
driver field in the
app/mail.php file is a supported field. To do this, we could create a file at
config-validation/mail.php with the following:
Custom Validation Error Messages
There may be times when you want to override the error message for a specific validation rule. This can be done by passing in an array containing the messages to the
->messages() method for a Rule. This array should follow the same pattern that would be used in a standard Laravel Validator object.
As an example, we might want to add a config validation rule to ensure that the driver field in the
app/mail.php file is a supported field and also use a custom error message. To do this, we could update our validation file to the following:
->messages(['in' => 'The mail driver is invalid']),
This package has a lot of amazing features with code examples. If you want to dig more you can visit its complete documentation on Github.
Published at : 08-04-2022
I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.