PHPackages                             kariricode/validator - 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. kariricode/validator

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

kariricode/validator
====================

A powerful and flexible validation component for PHP, part of the KaririCode Framework. It provides configurable processors for validating data attributes in entities using a streamlined and efficient pipeline system.

1.0.0(1y ago)06MITPHPPHP ^8.3

Since Oct 23Pushed 1y agoCompare

[ Source](https://github.com/KaririCode-Framework/kariricode-validator)[ Packagist](https://packagist.org/packages/kariricode/validator)[ Docs](https://kariricode.org)[ RSS](/packages/kariricode-validator/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (3)Dependencies (9)Versions (3)Used By (0)

KaririCode Framework: Validator Component
=========================================

[](#kariricode-framework-validator-component)

A powerful and flexible data validation component for PHP, part of the KaririCode Framework. It uses attribute-based validation with configurable processors to ensure data integrity and validation in your applications.

Table of Contents
-----------------

[](#table-of-contents)

- [Features](#features)
- [Installation](#installation)
- [Usage](#usage)
    - [Basic Usage](#basic-usage)
    - [Advanced Usage: User Registration](#advanced-usage-user-registration)
- [Available Validators](#available-validators)
    - [Input Validators](#input-validators)
    - [Numeric Validators](#numeric-validators)
    - [Logic Validators](#logic-validators)
    - [Date Validators](#date-validators)
- [Configuration](#configuration)
- [Integration with Other KaririCode Components](#integration-with-other-kariricode-components)
- [Development and Testing](#development-and-testing)
- [Contributing](#contributing)
- [License](#license)
- [Support and Community](#support-and-community)

Features
--------

[](#features)

- Attribute-based validation for object properties
- Comprehensive set of built-in validators for common use cases
- Easy integration with other KaririCode components
- Configurable processors for customized validation logic
- Support for custom error messages
- Extensible architecture allowing custom validators
- Robust error handling and reporting
- Chainable validation pipelines for complex data validation
- Built-in support for multiple validation scenarios
- Type-safe validation with PHP 8.3 features

Installation
------------

[](#installation)

You can install the Validator component via Composer:

```
composer require kariricode/validator
```

### Requirements

[](#requirements)

- PHP 8.3 or higher
- Composer
- Extensions: `ext-mbstring`, `ext-filter`

Usage
-----

[](#usage)

### Basic Usage

[](#basic-usage)

1. Define your data class with validation attributes:

```
use KaririCode\Validator\Attribute\Validate;

class UserProfile
{
    #[Validate(
        processors: [
            'required',
            'length' => ['minLength' => 3, 'maxLength' => 20],
        ],
        messages: [
            'required' => 'Username is required',
            'length' => 'Username must be between 3 and 20 characters',
        ]
    )]
    private string $username = '';

    #[Validate(
        processors: ['required', 'email'],
        messages: [
            'required' => 'Email is required',
            'email' => 'Invalid email format',
        ]
    )]
    private string $email = '';

    // Getters and setters...
}
```

2. Set up the validator and use it:

```
use KaririCode\ProcessorPipeline\ProcessorRegistry;
use KaririCode\Validator\Validator;
use KaririCode\Validator\Processor\Logic\RequiredValidator;
use KaririCode\Validator\Processor\Input\LengthValidator;
use KaririCode\Validator\Processor\Input\EmailValidator;

$registry = new ProcessorRegistry();
$registry->register('validator', 'required', new RequiredValidator());
$registry->register('validator', 'length', new LengthValidator());
$registry->register('validator', 'email', new EmailValidator());

$validator = new Validator($registry);

$userProfile = new UserProfile();
$userProfile->setUsername('wa');  // Too short
$userProfile->setEmail('invalid-email');  // Invalid format

$result = $validator->validate($userProfile);

if ($result->hasErrors()) {
    foreach ($result->getErrors() as $property => $errors) {
        foreach ($errors as $error) {
            echo "$property: {$error['message']}\n";
        }
    }
}
```

### Advanced Usage: User Registration

[](#advanced-usage-user-registration)

Here's an example of how to use the KaririCode Validator in a real-world scenario, such as validating user registration data:

```
use KaririCode\Validator\Attribute\Validate;

class UserRegistration
{
    #[Validate(
        processors: [
            'required',
            'length' => ['minLength' => 3, 'maxLength' => 20],
        ],
        messages: [
            'required' => 'Username is required',
            'length' => 'Username must be between 3 and 20 characters',
        ]
    )]
    private string $username = '';

    #[Validate(
        processors: ['required', 'email'],
        messages: [
            'required' => 'Email is required',
            'email' => 'Invalid email format',
        ]
    )]
    private string $email = '';

    #[Validate(
        processors: [
            'required',
            'length' => ['minLength' => 8],
        ],
        messages: [
            'required' => 'Password is required',
            'length' => 'Password must be at least 8 characters long',
        ]
    )]
    private string $password = '';

    #[Validate(
        processors: [
            'required',
            'integer',
            'range' => ['min' => 18, 'max' => 120],
        ],
        messages: [
            'required' => 'Age is required',
            'integer' => 'Age must be a whole number',
            'range' => 'Age must be between 18 and 120',
        ]
    )]
    private int $age = 0;

    // Getters and setters...
}

// Usage example
$registration = new UserRegistration();
$registration->setUsername('wm');  // Too short
$registration->setEmail('invalid');  // Invalid format
$registration->setPassword('weak');  // Too short
$registration->setAge(15);  // Too young

$result = $validator->validate($registration);

// Process validation results
if ($result->hasErrors()) {
    $errors = $result->getErrors();
    // Handle validation errors
} else {
    $validatedData = $result->getValidatedData();
    // Process valid registration
}
```

Available Validators
--------------------

[](#available-validators)

### Input Validators

[](#input-validators)

- **EmailValidator**: Validates email addresses using PHP's filter\_var function.

    - Error Keys:
        - `invalidType`: Input is not a string
        - `invalidFormat`: Invalid email format
- **LengthValidator**: Validates string length within specified bounds.

    - **Configuration Options**:
        - `minLength`: Minimum allowed length
        - `maxLength`: Maximum allowed length
    - Error Keys:
        - `invalidType`: Input is not a string
        - `tooShort`: String is shorter than minLength
        - `tooLong`: String is longer than maxLength
- **UrlValidator**: Validates URLs using PHP's filter\_var function.

    - Error Keys:
        - `invalidType`: Input is not a string
        - `invalidFormat`: Invalid URL format

### Numeric Validators

[](#numeric-validators)

- **IntegerValidator**: Ensures the input is a valid integer.

    - Error Keys:
        - `notAnInteger`: Input is not a valid integer
- **RangeValidator**: Validates numeric values within a specified range.

    - **Configuration Options**:
        - `min`: Minimum allowed value
        - `max`: Maximum allowed value
    - Error Keys:
        - `notNumeric`: Input is not a number
        - `outOfRange`: Value is outside specified range

### Logic Validators

[](#logic-validators)

- **RequiredValidator**: Ensures a value is not empty.
    - Error Keys:
        - `missingValue`: Required value is missing or empty

### Date Validators

[](#date-validators)

- **DateFormatValidator**: Validates dates against a specified format.

    - **Configuration Options**:
        - `format`: Date format string (default: 'Y-m-d')
    - Error Keys:
        - `invalidType`: Input is not a string
        - `invalidFormat`: Date doesn't match specified format
- **DateRangeValidator**: Validates dates within a specified range.

    - **Configuration Options**:
        - `minDate`: Minimum allowed date
        - `maxDate`: Maximum allowed date
        - `format`: Date format string (default: 'Y-m-d')
    - Error Keys:
        - `invalidType`: Input is not a string
        - `invalidDate`: Invalid date format
        - `outOfRange`: Date is outside specified range

Configuration
-------------

[](#configuration)

The Validator component can be configured globally or per-validator basis. Here's an example of how to configure the `LengthValidator`:

```
use KaririCode\Validator\Processor\Input\LengthValidator;

$lengthValidator = new LengthValidator();
$lengthValidator->configure([
    'minLength' => 3,
    'maxLength' => 20,
]);

$registry->register('validator', 'length', $lengthValidator);
```

Integration with Other KaririCode Components
--------------------------------------------

[](#integration-with-other-kariricode-components)

The Validator component is designed to work seamlessly with other KaririCode components:

- **KaririCode\\Contract**: Provides interfaces and contracts for consistent component integration.
- **KaririCode\\ProcessorPipeline**: Utilized for building and executing validation pipelines.
- **KaririCode\\PropertyInspector**: Used for analyzing and processing object properties with validation attributes.

Registry Explanation
--------------------

[](#registry-explanation)

The registry is a central component for managing validators. Here's how to set up a complete registry:

```
// Create and configure the registry
$registry = new ProcessorRegistry();

// Register all required validators
$registry->register('validator', 'required', new RequiredValidator());
$registry->register('validator', 'email', new EmailValidator());
$registry->register('validator', 'length', new LengthValidator());
$registry->register('validator', 'integer', new IntegerValidator());
$registry->register('validator', 'range', new RangeValidator());
$registry->register('validator', 'url', new UrlValidator());
$registry->register('validator', 'dateFormat', new DateFormatValidator());
$registry->register('validator', 'dateRange', new DateRangeValidator());
```

Development and Testing
-----------------------

[](#development-and-testing)

For development and testing purposes, this package uses Docker and Docker Compose to ensure consistency across different environments. A Makefile is provided for convenience.

### Prerequisites

[](#prerequisites)

- Docker
- Docker Compose
- Make (optional, but recommended for easier command execution)

### Development Setup

[](#development-setup)

1. Clone the repository:

    ```
    git clone https://github.com/KaririCode-Framework/kariricode-validator.git
    cd kariricode-validator
    ```
2. Set up the environment:

    ```
    make setup-env
    ```
3. Start the Docker containers:

    ```
    make up
    ```
4. Install dependencies:

    ```
    make composer-install
    ```

### Available Make Commands

[](#available-make-commands)

- `make up`: Start all services in the background
- `make down`: Stop and remove all containers
- `make build`: Build Docker images
- `make shell`: Access the PHP container shell
- `make test`: Run tests
- `make coverage`: Run test coverage with visual formatting
- `make cs-fix`: Run PHP CS Fixer to fix code style
- `make quality`: Run all quality commands (cs-check, test, security-check)

Contributing
------------

[](#contributing)

We welcome contributions to the KaririCode Validator component! Here's how you can contribute:

1. Fork the repository
2. Create a new branch for your feature or bug fix
3. Write tests for your changes
4. Implement your changes
5. Run the test suite and ensure all tests pass
6. Submit a pull request with a clear description of your changes

Please read our [Contributing Guide](CONTRIBUTING.md) for more details on our code of conduct and development process.

License
-------

[](#license)

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.

Support and Community
---------------------

[](#support-and-community)

- **Documentation**:
- **Issue Tracker**: [GitHub Issues](https://github.com/KaririCode-Framework/kariricode-validator/issues)
- **Community Forum**: [KaririCode Club Community](https://kariricode.club)
- **Stack Overflow**: Tag your questions with `kariricode-validator`

---

Built with ❤️ by the KaririCode team. Empowering developers to create more secure and robust PHP applications.

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance37

Infrequent updates — may be unmaintained

Popularity4

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity55

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

Unknown

Total

1

Last Release

572d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/e42da33070ae9e744b18f4be67303b8935f6e14488a88328e7c121d909b4f2db?d=identicon)[growthcodeoficial](/maintainers/growthcodeoficial)

---

Top Contributors

[![walmir-silva](https://avatars.githubusercontent.com/u/142913591?v=4)](https://github.com/walmir-silva "walmir-silva (21 commits)")

---

Tags

phpvalidatorvalidationdataentityprocessorpipelineattributeKaririCode

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/kariricode-validator/health.svg)

```
[![Health](https://phpackages.com/badges/kariricode-validator/health.svg)](https://phpackages.com/packages/kariricode-validator)
```

###  Alternatives

[nilportugues/validator

An efficient data validator for PHP

131.5k](/packages/nilportugues-validator)

PHPackages © 2026

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