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

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

star/validator
==============

Validator implementation lib

112[2 issues](https://github.com/yvoyer/validator/issues)PHP

Since Feb 14Pushed 11y ago1 watchersCompare

[ Source](https://github.com/yvoyer/validator)[ Packagist](https://packagist.org/packages/star/validator)[ RSS](/packages/star-validator/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

Validator
=========

[](#validator)

[![Build Status](https://camo.githubusercontent.com/c0f31d0ae7445825574f8da1ba24e9f328422c727ad5ff1c4921f5c950237137/68747470733a2f2f7472617669732d63692e6f72672f79766f7965722f76616c696461746f722e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/yvoyer/validator)

Validation tool that can be connected on any object you want to validate.

Usage
-----

[](#usage)

Validators
----------

[](#validators)

Validators are classes that will determines if the value was valid. In the following example, the `YourNonEmptyValidator` would make sure the name on the `SomeObject` cannot be empty.

```
// YourNonEmptyValidator.php
class YourNonEmptyValidator implements Validator
{
    /**
     * @var SomeObject
     */
    private $object;

    /**
     * @param SomeObject $object
     */
    public function __construct(SomeObject $object)
    {
        $this->object = $object;
    }

    /**
     * @param NotificationHandler $handler
     *
     * @return ValidationResult
     */
    public function validate(NotificationHandler $handler)
    {
        $name = $this->object->getName();

        if (empty($name)) {
            $handler->notifyError(new StringMessage('Name cannot be empty.'));
        }

        return $handler->createResult();
    }
}

```

The validators will return a `ValidationResult` to determine whether any constraints have failed.

NotificationHandler
-------------------

[](#notificationhandler)

The `NotificationHandler` are responsible to notify the user about an error.

The only supported strategy currently implemented are:

- ExceptionNotificationHandler: Will throw a `ValidationErrorException` on the first error (recommended for development).
- DeferredNotificationHandler: Will keep the trace of all errors for future use by the `ValidationResult`.

Example
-------

[](#example)

Given you created a class that needs to be validated:

```
// SomeObject.php
class SomeObject
{
    /**
     * @var string
     */
    private $name;

    public function setName($name)
    {
        $this->name = $name;
    }

    public function getName()
    {
        return $this->name;
    }
}

```

You will need to implement a validation method in which you will put all the validators to push in the `ValidationHandler`.

```
// SomeObject.php
...
/**
 * @param NotificationHandler $handler
 *
 * @return ValidationResult
 */
public function validate(NotificationHandler $handler)
{
    $validator = new YourNonEmptyValidator($this); // This is your custom validator implementing Validator interface

    return $validator->validate($handler);
}
...

```

Creating the validator for your object (or using built-in validators, you can get the result from the validation.

By using the structure, validating your code will look something like this.

```
// Using the Exception handler on a valid object
$validObject = new SomeObject();
$validObject->setName('non-empty');
$result = $validObject->validate(new ExceptionNotificationHandler());
$result->hasErrors(); // Returns false
$result->getErrors()); // Returns empty array(), since there was no errors

// Using the Exception handler on a invalid object
$invalidObject = new SomeObject();
$invalidObject->validate(new ExceptionNotificationHandler()); // Would throw an exception on the first error (because of the handler).

// Using the deferred handler on a invalid object
$result = $invalidObject->validate(new DeferredNotificationHandler());
$result->hasErrors(); // Returns true
$result->getErrors()); // Returns empty array('Name cannot be empty.')

```

###  Health Score

15

—

LowBetter than 3% of packages

Maintenance0

Infrequent updates — may be unmaintained

Popularity7

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity41

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.

### Community

Maintainers

![](https://www.gravatar.com/avatar/36116f724f1bd3ed808e54e2b2de6c17209c5c2c1954685fba18958ec8d055ee?d=identicon)[yvoyer](/maintainers/yvoyer)

---

Top Contributors

[![yvoyer](https://avatars.githubusercontent.com/u/1745744?v=4)](https://github.com/yvoyer "yvoyer (10 commits)")

### Embed Badge

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

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

###  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)
