Download the PHP package moe-mizrak/validator-guard without Composer

On this page you can find all versions of the php package moe-mizrak/validator-guard. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package validator-guard

Validator Guard


Latest Version on Packagist

reboosty

ValidatorGuard enables attribute-driven validation to control Laravel method behavior.


Attribute validation is a powerful way to validate method parameters, method results, and method behavior in a declarative way.

If the transaction amount is not between 100 and 10,000, the exception will be thrown/logged (based on throwing or logging enabled in config).

Table of Contents

🤖 Requirements

🏁 Get Started

You can install the package via composer:

You can publish the validator-guard config file with:

This is the contents of the published config file:

🧩 Configuration

And the details of validator-guard config file options as follows:

You can also set the throw_exceptions, log_exceptions, and log_channel options in the .env file as follows:

🎨 Usage

There are two ways to use Validator Guard, either by using the valguard helper or service container bindings.

[!IMPORTANT] Service container bindings is not recommended for classes that cannot be resolved by the container, such as facades or helpers, or for classes requiring parameters like runtime-specific data.

It is also unsuitable for objects that are short-lived, require complex setup, and so on.

Using valguard Helper

Helper method valguard offers a simple way to use Validator Guard for classes that cannot be resolved by the container or require runtime-specific data. For example, if you have a class named UserService, and method named getTransactionAmount that you want to use for attribute validation:

You can use the valguard helper as follows:

(You can check the details of the IntervalGuard attribute in the Attributes section.)

[!NOTE] For valguard helper method, you do NOT need to add the classes that you use for attribute validation to the class_list in the configuration file. And classes do NOT have to be resolved by the container.

Using Service Container Bindings

By using service container bindings, you need to add the classes that you use for attribute validation to the class_list in the configuration file. For the classes that you add to the class_list, the package will bind them to the ValidatorGuardCore in the service provider. So whenever these classes are resolved by the container, the package will initiate the ValidatorGuardCore to mimic the classes as a wrapper and handle validation.

For example, if you have a class named UserService that you want to use for attribute validation, you need to add the class to the class_list in the configuration file as follows:

And let's say you have a method named getTransactionAmount in the UserService class that you want to validate the attributes. You can add the attributes that you want to validate to the method as follows:

In this example, the getTransactionAmount method will be validated by the IntervalGuard attribute after the method execution. (You can check the details of the IntervalGuard attribute in the Attributes section.)

And whenever UserService is resolved by the container (e.g. Dependency Injection, app() helper etc.), the package will initiate the ValidatorGuardCore to mimic the UserService as a wrapper and handle validation:

Attributes

More attributes will be added in the future. You can also create/add your custom attributes as explained in the Create Your Own Attribute section. We will cover the following attributes in this section:

[!TIP] Attribute flags as follows:

  • TARGET_METHOD : Marks that attribute declaration is allowed only in class methods.
  • IS_REPEATABLE : Attribute declaration in the same place is allowed multiple times.
  • TARGET_PARAMETER : Marks that attribute declaration is allowed only in function or method parameters.

IntervalGuard

The IntervalGuard attribute is used to validate the method result within a specified interval. Attribute flags for the IntervalGuard: TARGET_METHOD, IS_REPEATABLE

IntervalGuard is listed in the after array in the configuration file attributes option because it validates the method result after the method execution.

Sample usage:

And when the getTransactionAmount method is called, the result will be validated by the IntervalGuard attribute after the method execution.

InntervalGuard attribute parameters:

Basically it checks: lowerBound < result < upperBound or lowerBound <= result <= upperBound based on the operator and son on.

IntervalGuard attribute is repeatable, so you can add multiple IntervalGuard attributes to the same method. For instance:

In this example, the getTransactionAmount method result will be validated by the IntervalGuard attribute twice after the method execution. The first validation will check if the transaction amount is bigger than 10, and the second validation will check if the transaction amount is less than or equal to 30.

There can be many other use cases for the IntervalGuard attribute. You can use it for any method that requires interval validation for the method result.

DateGuard

The DateGuard attribute is used to validate whether the given date parameter is in the future, past, weekdays, weekends, today, tomorrow or between two dates and so on. Attribute flag for the DateGuard: TARGET_PARAMETER

DateGuard is listed in the before array in the configuration file attributes option because it validates the method parameter before the method execution, which benefits the performance by avoiding unnecessary method execution.

Sample usage:

And when the createEvent method is called, the eventDate parameter will be validated by the DateGuard attribute before the method execution.

DateGuard attribute parameters:

There can be many other use cases for the DateGuard attribute. You can use it for any method that requires date validation for the method parameter.

AllowedValuesGuard

The AllowedValuesGuard attribute is used to validate whether the given parameter is one of the allowed values. Attribute flag for the AllowedValuesGuard: TARGET_PARAMETER

AllowedValuesGuard is listed in the before array in the configuration file attributes option because it validates the method parameter before the method execution.

Sample usage:

And when the createEvent method is called, the eventType parameter will be validated by the AllowedValuesGuard attribute before the method execution.

AllowedValuesGuard attribute parameters:

There can be many other use cases for the AllowedValuesGuard attribute. You can use it for any method that requires allowed values validation for the method parameter.

CallbackGuard

The CallbackGuard attribute is used to invoke a specified class method with given parameters and validate its result against the expected value. Attribute flag for the CallbackGuard: TARGET_METHOD, IS_REPEATABLE

CallbackGuard is listed in the before array in the configuration file attributes option because it validates the method parameter before the method execution.

Sample usage:

And when the processPayment method is called, the isPaymentMethodSupported method of the PaymentGateway class will be invoked with the given parameters, and the result of isPaymentMethodSupported method will be validated by the CallbackGuard attribute before the processPayment method execution.

CallbackGuard attribute parameters:

There can be many other use cases for the CallbackGuard attribute. You can use it for any method that requires callback method validation before the method execution.

ArrayKeysExistGuard

The ArrayKeysExistGuard attribute is used to validate whether array key exists in the method array result or array parameter. Attribute flag for the ArrayKeysExistGuard: TARGET_METHOD, IS_REPEATABLE

ArrayKeysExistGuard is listed in the after array in the configuration file attributes option because it validates the method result after the method execution, along with method parameters.

Sample usage:

And when the getUserData method is called, the result array will be validated by the ArrayKeysExistGuard attribute after the method execution.

ArrayKeysExistGuard attribute parameters:

There can be many other use cases for the ArrayKeysExistGuard attribute. You can use it for any method that requires array key validation for the method result or method parameter.

Create Your Own Attribute

You can create your custom attribute quite easily and use it for attribute validation. For this purpose, follow the steps below:

Sample attribute:
Config file:
Usage example:

💫 Contributing

Your contributions are welcome! f you'd like to improve this package, simply create a pull request with your changes. Your efforts help enhance its functionality and documentation.

If you find this package useful, please consider ⭐ it to show your support!

📜 License

Validator Guard is an open-sourced software licensed under the MIT license.


All versions of validator-guard with dependencies

PHP Build Version
Package Version
Requires php Version ^8.2
moe-mizrak/validator-guard-core Version ^1.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package moe-mizrak/validator-guard contains the following files

Loading the files please wait ....