PHPackages                             krubio/perfect-validation - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Validation &amp; Sanitization](/categories/validation)
4. /
5. krubio/perfect-validation

ActiveLibrary[Validation &amp; Sanitization](/categories/validation)

krubio/perfect-validation
=========================

SOLID Compliant OOP Validation

v0.1.3(2y ago)034MITPHPPHP &gt;=8.0.0

Since Jun 24Pushed 2y ago1 watchersCompare

[ Source](https://github.com/benanamen/perfect-validation)[ Packagist](https://packagist.org/packages/krubio/perfect-validation)[ RSS](/packages/krubio-perfect-validation/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (4)Dependencies (5)Versions (5)Used By (0)

[![codecov](https://camo.githubusercontent.com/7eff3e07f0b508ba19a201a829fe23f77fabb486a2c4cbbe3c95647a73039f55/68747470733a2f2f636f6465636f762e696f2f67682f62656e616e616d656e2f706572666563742d76616c69646174696f6e2f6272616e63682f6d61737465722f67726170682f62616467652e7376673f746f6b656e3d7566657077515034304d)](https://codecov.io/gh/benanamen/perfect-validation)

[![Scrutinizer Code Quality](https://camo.githubusercontent.com/8d5b0e9d6dfedfb4aa61f6004e62b86cd8cc9705a3a064059ace4d386368e7ef/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f62656e616e616d656e2f706572666563742d76616c69646174696f6e2f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/benanamen/perfect-validation/?branch=master)[![Code Coverage](https://camo.githubusercontent.com/61f2b26da4ae80dcf9b89059bc54875d1798cf17bcbe8934f338e414faf3170a/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f62656e616e616d656e2f706572666563742d76616c69646174696f6e2f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/benanamen/perfect-validation/?branch=master)[![Build Status](https://camo.githubusercontent.com/2eaac6cbbf001d98aecf42a812a9c4b1241f3d9dac25e689dd343312957c3157/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f62656e616e616d656e2f706572666563742d76616c69646174696f6e2f6261646765732f6275696c642e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/benanamen/perfect-validation/build-status/master)[![Code Intelligence Status](https://camo.githubusercontent.com/328f1afca7ac277820288c4f7aef819064924b2c7e85ab4c37ff5681440ebe54/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f62656e616e616d656e2f706572666563742d76616c69646174696f6e2f6261646765732f636f64652d696e74656c6c6967656e63652e7376673f623d6d6173746572)](https://scrutinizer-ci.com/code-intelligence)

[![Coverage](https://camo.githubusercontent.com/a204733f49ad27ebaae994294e66ee82b2a47d5352bb68437b3bf641b5a0e6c1/68747470733a2f2f736f6e6172636c6f75642e696f2f6170692f70726f6a6563745f6261646765732f6d6561737572653f70726f6a6563743d62656e616e616d656e5f706572666563742d76616c69646174696f6e266d65747269633d636f766572616765)](https://sonarcloud.io/summary/new_code?id=benanamen_perfect-validation)[![Maintainability Rating](https://camo.githubusercontent.com/51f789ccc1f971a355043d00ff0c0b0c7514522165e6a0937df1096083f691c9/68747470733a2f2f736f6e6172636c6f75642e696f2f6170692f70726f6a6563745f6261646765732f6d6561737572653f70726f6a6563743d62656e616e616d656e5f706572666563742d76616c69646174696f6e266d65747269633d7371616c655f726174696e67)](https://sonarcloud.io/summary/new_code?id=benanamen_perfect-validation)[![Code Smells](https://camo.githubusercontent.com/177da7d7e35db2bb46f8fea0088e9739e49751d86c2ee8603a3051f97145af05/68747470733a2f2f736f6e6172636c6f75642e696f2f6170692f70726f6a6563745f6261646765732f6d6561737572653f70726f6a6563743d62656e616e616d656e5f706572666563742d76616c69646174696f6e266d65747269633d636f64655f736d656c6c73)](https://sonarcloud.io/summary/new_code?id=benanamen_perfect-validation)[![Technical Debt](https://camo.githubusercontent.com/7e6ff3478920653a4d3f03ad2fee6e1ed1729b91c802bad39a2a246258d289aa/68747470733a2f2f736f6e6172636c6f75642e696f2f6170692f70726f6a6563745f6261646765732f6d6561737572653f70726f6a6563743d62656e616e616d656e5f706572666563742d76616c69646174696f6e266d65747269633d7371616c655f696e646578)](https://sonarcloud.io/summary/new_code?id=benanamen_perfect-validation)[![Quality Gate Status](https://camo.githubusercontent.com/9e2fd54dc98003199ad92e3320c2182bc32bc5acbfbf05dacf4737db125dbc6e/68747470733a2f2f736f6e6172636c6f75642e696f2f6170692f70726f6a6563745f6261646765732f6d6561737572653f70726f6a6563743d62656e616e616d656e5f706572666563742d76616c69646174696f6e266d65747269633d616c6572745f737461747573)](https://sonarcloud.io/summary/new_code?id=benanamen_perfect-validation)[![Reliability Rating](https://camo.githubusercontent.com/3410a9d02a0b8a56581c3929b8ddf5b9ffe0029a3c637aece87952baf7195e8f/68747470733a2f2f736f6e6172636c6f75642e696f2f6170692f70726f6a6563745f6261646765732f6d6561737572653f70726f6a6563743d62656e616e616d656e5f706572666563742d76616c69646174696f6e266d65747269633d72656c696162696c6974795f726174696e67)](https://sonarcloud.io/summary/new_code?id=benanamen_perfect-validation)

[![Duplicated Lines (%)](https://camo.githubusercontent.com/72ba2700e22f033b9f0b769a55b0f4666e538f08096dd7df3c023503a0ff8b83/68747470733a2f2f736f6e6172636c6f75642e696f2f6170692f70726f6a6563745f6261646765732f6d6561737572653f70726f6a6563743d62656e616e616d656e5f706572666563742d76616c69646174696f6e266d65747269633d6475706c6963617465645f6c696e65735f64656e73697479)](https://sonarcloud.io/summary/new_code?id=benanamen_perfect-validation)[![Vulnerabilities](https://camo.githubusercontent.com/4cf3b8790ab52609a11b1c4e8d63d5887a96703ad7d98b2865789d8e2473fd24/68747470733a2f2f736f6e6172636c6f75642e696f2f6170692f70726f6a6563745f6261646765732f6d6561737572653f70726f6a6563743d62656e616e616d656e5f706572666563742d76616c69646174696f6e266d65747269633d76756c6e65726162696c6974696573)](https://sonarcloud.io/summary/new_code?id=benanamen_perfect-validation)[![Bugs](https://camo.githubusercontent.com/3205a4f9f867fce90a21a95a66a3f73c13a32736b50cd524cd44725ce9be1ee2/68747470733a2f2f736f6e6172636c6f75642e696f2f6170692f70726f6a6563745f6261646765732f6d6561737572653f70726f6a6563743d62656e616e616d656e5f706572666563742d76616c69646174696f6e266d65747269633d62756773)](https://sonarcloud.io/summary/new_code?id=benanamen_perfect-validation)[![Security Rating](https://camo.githubusercontent.com/9f21b023245a5fc625510df94a4558a0191590ee592d20b36ea8980ac5919fa1/68747470733a2f2f736f6e6172636c6f75642e696f2f6170692f70726f6a6563745f6261646765732f6d6561737572653f70726f6a6563743d62656e616e616d656e5f706572666563742d76616c69646174696f6e266d65747269633d73656375726974795f726174696e67)](https://sonarcloud.io/summary/new_code?id=benanamen_perfect-validation)

Validator Class Documentation
=============================

[](#validator-class-documentation)

The `Validator` class is responsible for validating data using different validation strategies. It allows you to set and switch between different validation strategies based on your specific requirements.

Class Signature
---------------

[](#class-signature)

```
class Validator {
    private ValidationStrategy $strategy;

    public function __construct(ValidationStrategy $strategy);
    public function setValidationStrategy(ValidationStrategy $strategy): void;
    public function validateData($data): mixed;
    public function validateDataWithErrors($data): array;
}
```

Constructor
-----------

[](#constructor)

### `__construct(ValidationStrategy $strategy)`

[](#__constructvalidationstrategy-strategy)

The constructor accepts a `ValidationStrategy` object as a parameter, which determines the specific validation strategy to use. The initial validation strategy is set during object creation.

Public Methods
--------------

[](#public-methods)

### `setValidationStrategy(ValidationStrategy $strategy): void`

[](#setvalidationstrategyvalidationstrategy-strategy-void)

This method allows you to set a new validation strategy by providing a `ValidationStrategy` object as a parameter. It replaces the existing validation strategy with the new one.

### `validateData($data): mixed`

[](#validatedatadata-mixed)

The `validateData` method is used to perform data validation using the current validation strategy. It accepts the data to be validated as a parameter and returns the validation result.

### `validateDataWithErrors($data): array`

[](#validatedatawitherrorsdata-array)

The `validateDataWithErrors` method performs data validation and returns an array containing the validation status and any errors, if applicable. If the strategy object has a `getErrors` method, the errors will be included in the array.

- Parameters:

    - `$data`: The data to be validated.
- Return:

    - Returns the validation result, which depends on the specific validation strategy being used. It can be a boolean value (`true` for a successful validation, `false` otherwise) or other data types based on the strategy implementation or an array with the validation status and errors, if any.

Usage Example
-------------

[](#usage-example)

Here's an example that demonstrates the basic usage of the `Validator` class:

```
// Create a validator with a specific validation strategy
$validator = new Validator($requiredFieldsStrategy);

// Set a new validation strategy if needed
$validator->setValidationStrategy($customValidationStrategy);

// Perform data validation
$data = $_POST; // Example data to be validated
$result = $validator->validateData($data);

// Perform data validation with errors
$resultWithErrors = $validator->validateDataWithErrors($data);

// Check the validation result
if ($result === true) {
    echo "Validation passed! The data is valid.";
} elseif (isset($resultWithErrors['errors'])) {
    echo "Validation failed! Errors: " . implode(', ', $resultWithErrors['errors']);
} else {
    echo "Validation failed! Please check the data for errors.";
}
```

In the example above:

1. The `Validator` class is instantiated with an initial validation strategy (`$requiredFieldsStrategy`).
2. If needed, you can use the `setValidationStrategy` method to switch to a different validation strategy.
3. The `validateData` and `validateDataWithErrors` methods are called with the data to be validated (`$data`).
4. The returned results are checked to determine if the validation passed or failed.
5. Based on the validation results, you can perform appropriate actions or display error messages.

Note: The specific validation strategies used (`$requiredFieldsStrategy` and `$customValidationStrategy`) are not shown in the example, as they depend on your specific implementation.

###  Health Score

21

—

LowBetter than 19% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity9

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity42

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~40 days

Total

4

Last Release

935d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/92efcd3e8d87753dc819d970a254160b7ea8a60de6a127e7f93acada576b49c5?d=identicon)[benanamen](/maintainers/benanamen)

---

Top Contributors

[![benanamen](https://avatars.githubusercontent.com/u/1039435?v=4)](https://github.com/benanamen "benanamen (5 commits)")

---

Tags

validaton

###  Code Quality

TestsCodeception

Static AnalysisPHPStan, Psalm

Type Coverage Yes

### Embed Badge

![Health badge](/badges/krubio-perfect-validation/health.svg)

```
[![Health](https://phpackages.com/badges/krubio-perfect-validation/health.svg)](https://phpackages.com/packages/krubio-perfect-validation)
```

###  Alternatives

[webmozart/assert

Assertions to validate method input/output with nice error messages.

7.6k894.0M1.2k](/packages/webmozart-assert)[bensampo/laravel-enum

Simple, extensible and powerful enumeration implementation for Laravel.

2.0k15.9M104](/packages/bensampo-laravel-enum)[swaggest/json-schema

High definition PHP structures with JSON-schema based validation

48612.5M73](/packages/swaggest-json-schema)[stevebauman/purify

An HTML Purifier / Sanitizer for Laravel

5325.6M19](/packages/stevebauman-purify)[ashallendesign/laravel-config-validator

A package for validating your Laravel app's config.

217905.3k5](/packages/ashallendesign-laravel-config-validator)[crazybooot/base64-validation

Laravel validators for base64 encoded files

1341.9M8](/packages/crazybooot-base64-validation)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
