Introducing Config Validation0:00
Let's talk about validation in this next video, but you're currently thinking about something else, because this is not about request of data validation. This is about validating your config files. This sounds strange, but it's very useful. So here's the repository for the Laravel config validator by Ash Elm, and let's see what it does. A Laravel package that allows you to validate your config values and environment. And the first time I read it, it sounded a bit strange to me. So why would I want to do this, and how does it help me? But now I find this really useful. So I'd say let's install it, and then let's take a look what it actually does. Here we go. That's the first step. All right, done. What's the next thing? There is a default rule set which we can publish, and I think it's a good idea to give us an overview of what this package can do. All right, so we now have a new directory config/validation. Let's take a look
Exploring Published Rules0:52
rule set which we can publish, and I think it's a good idea to give us an overview of what this package can do. All right, so we now have a new directory config/validation. Let's take a look here, and now on the config/validation. So next to the config folder, we now got a config/validation folder with a file for every config file. So all those files we have in config, we now have a validation rule file for those as well. And this is what the one for app, for example, looks like. So for typical values that I already know, like the app name, it should be a string. environment should be a string, and debug should be boolean. url should be URL. So I think you already see where we're going with this. Let's take a look at app again. So these are the values that are being set, and mostly they are loaded from the .env file. And here we're making sure that we don't put here anything strange outside our .env files, which would not work. So we're telling
Creating Custom Rules1:44
set, and mostly they are loaded from the .env file. And here we're making sure that we don't put here anything strange outside our environment files, which would not work. So we're telling Laravel exactly how we want our config values to be, and we're going to run those validation rules. But let's get rid of them for now, and let's create our own one. Let's see how this is done. So we now have this new comment here, php artisan make:config validation. And the one thing that I want to do now is I want to have this for my services file. Let's check this out here, config. Oops, it seems like I deleted the wrong files. So config files we need. These are the ones which I want to delete. Oops, please don't tell anyone. So back here, services config file is one which is very important to me because, for example, here are the values being set for our Twitter service. So in my application, I really want to make sure that those values
Running Config Validation2:39
is one which is very important to me because, for example, here are the values being set for our Twitter service. So in my application, I really want to make sure that those values also are being set. So let's try to create a new validation config validation for services. Here we go. Let's see. Config validation services. All right. So here we can now define how we want our services file to be. And for our case, we're just making sure that we have the right Twitter settings because this is what I'm most interested here. All right. So I want to make sure that for Twitter, I think the first one was consumerKey. This should be a string. And let's give this a run already just to make sure that it's working. We can run this with php artisan config:validate. And this is working. Okay, good. Let's see what happens if we say that this should be a Boolean. Run it again. This is now failing because this is not a Boolean. It is a string. And it also shows
And this is working. Okay, good. Let's see what happens if we say that this should be a Boolean. Run it again. This is now failing because this is not a Boolean. It is a string. And it also shows you the value right down here. But I have made sure that you can see this value because it's a real key of mine. All right. So let's get back in here. Let's say this is a string. And we got four of them. consumerKey. I think it was consumerSecret. Then I think it's accessToken and accessTokenSecret. All right. Let's run this again. And it's working. All of our validation rules passed. Okay, amazing. So I would use this in one of my packages, especially for onboarding, so that I make sure that new people, new developer on my team sets the right .env files. Because mostly they come only with the .env.example file when they install the application for the first time. And you want to make sure that they set the right keys. And this validation
Extending with Custom Checks4:29
Because mostly they come only with the .env example file when they install the application for the first time. And you want to make sure that they set the right keys. And this validation process will tell them if they probably are missing some keys, some credentials that they need to set. But actually there's also more that you could do. So you can take this even further because since this is running with Laravel validation process in the background, we could just create a new validation rule and call it something like TraderCredentialsWorkRule. And now we got here this class, this rule class with some validation. And here I could, for example, make a request to the Twitter API with my credentials and see if my credentials are working. Many APIs have some kind of health or credential checks where you can make sure that the credentials are correct. And with the right response,
if my credentials are working. Many APIs have some kind of health or credential checks where you can make sure that the credentials are correct. And with the right response, I could say, okay, this is correct. And I'll return true and the validation would pass. So I think this will also be very cool. And I'm pretty sure I'm going to try to implement this with one of my applications because I think this could be very useful. I imagine there are many more use cases for this package probably that only fit for your personal needs. So give this a look also back on the documentation, go through the things you can do here. There are a few more things that you can do. So I think this is also a good example where you make sure that the driver value is set with one of those values, which I think is also pretty useful because you only want to have one of those values being set. So this is really nice.
Features and Recap5:57
you make sure that the driver value is set with one of those values, which I think is also pretty useful because you only want to have one of those values being set. So this is really nice. Then, of course, you also got custom validation messages, error messages. You can specify that it only runs for specific environments, which could also be useful. So yeah, a lot of cool features for this package, which makes it very powerful. Of course, you can also run this through code itself, but I often prefer to do this with a command. Let's break down what we just learned. LayerboardConfigValidator is a package by Ash Allen that helps you to validate your config files. I find this extremely useful for bigger teams and when onboarding new people, just to make sure that the right credentials or config values are set. Thank you, Ash Allen.
make sure that the right credentials or config values are set. Thank you, Ash Allen.
