Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Validator Laravel Package

memio/validator

A small PHP validation library built around a clear rule/constraint API. Define validators, combine rules, and validate values with detailed violation information. Lightweight and framework-agnostic, suitable for simple input checks and reusable domain validation.

View on GitHub
Deep Wiki
Context7

Memio's Validator

A validator library for Memio: allows to define Constraints to check if the built Memio models are valid (e.g. Method cannot be both abstract and final).

Note: This package is part of Memio, a highly opinionated PHP code generator. Have a look at the main repository.

Installation

Install it using Composer:

$ composer require memio/validator:^3.0

Example

Let's say we want to check that Arguments aren't scalar. In order to do so, the first thing we'll need to do is to write a Constraint:

<?php

require __DIR__.'/vendor/autoload.php';

use Memio\Validator\Constraint;
use Memio\Validator\Violation\NoneViolation;
use Memio\Validator\Violation\SomeViolation;

class ArgumentCannotBeScalar implements Constraint
{
    public function validate($model)
    {
        $type = $model->getType();
        if (in_array($type, ['array', 'bool', 'callable', 'double', 'int', 'mixed', 'null', 'resource', 'string'], true)) {
            return new SomeViolation(sprintf('Argument "%s" cannot be scalar', $model->getName()));
        }

        return new NoneViolation();
    }
}

Note: In Memio, all Constraints are named after their error message. This isn't a hard coded rule, they can have any name.

We then need to register our rule in an ArgumentValidator:

// ...

$argumentValidator = new ArgumentValidator();
$argumentValidator->add(new ArgumentCannotBeScalar());

ArgumentValidator is a ModelValidator called by Validator if the given model is an Argument. However, if the given model is a Method we'd like Validator to check our Constraint against its Arguments. To do so, we need to assemble ModelValidators as follow:

// ...

$collectionValidator = new CollectionValidator();
$methodValidator = new MethodValidator($argumentValidator, $collectionValidator);
$contractValidator = new ContractValidator($collectionValidator, $methodValidator);
$objectValidator = new ObjectValidator($collectionValidator, $methodValidator);
$fileValidator = new FileValidator($contractValidator, $objectValidator);

Finally, we need to create a validator and register our ModelValidators in it:

// ...

$myValidator = new Validator();
$myValidator->add($argumentValidator);
$myValidator->add($collectionValidator);
$myValidator->add($methodValidator);
$myValidator->add($contractValidator);
$myValidator->add($objectValidator);
$myValidator->add($fileValidator);

This way we can build specialized validators: one that'd check syntax errors, one that'd check business rules, etc... Possibilities are endless!

Have a look at the main respository to discover the full power of Medio.

Want to know more?

Memio uses phpspec, which means the tests also provide the documentation. Not convinced? Then clone this repository and run the following commands:

make lib-init                        # Set up Docker environment
make phpspec arg='--format pretty'   # Run the specifications

Note: Run make or make help to see all available commands.

You can see the current and past versions using one of the following:

And finally some meta documentation:

Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
codifyo/ts-generator-bundle
andydefer/laravel-cluster
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky
spatie/mailcoach-vapor