Skip to content

macocci7/purephp-validation

Repository files navigation

Purephp Validation

1. Features

Purephp Validation is a standalone library to use the Illuminate\Validation package outside the Laravel framework.

This library is based on jeffochoa/validator-factory,

This library is customized to use with static calls, like a Laravel Facade:

$validator = Validator::make(
    $data,
    $rules,
    $messages,
    $attributes,
);

It also supports Password rule object and File rule object.

$validator = Validator::make(
    data: $data,
    rules: [
        'password' => [
            'required',
            Password::min(8),
        ],
        'attachment' => [
            'required',
            File::image(),
        ],
    ];
);

Additionally and uniquely, Instance rule object is supported.

$validator = Validator::make(
    data: [
        'prop1' => new Instance([]),
        'prop2' => 'Instance',
        'prop3' => fn () => true,
    ],
    rules: [
        'prop1' => Instance::of(Instance::class),
        'prop2' => Instance::of([
            Instance::class,
            Validator::class,
            (fn () => true)::class,
        ]),
        'prop3' => Instance::of('Closure'),
    ],
);

2. Contents

3. Requirements

  • PHP 8.2 or later
  • Composer installed

4. Installation

composer require macocci7/purephp-validation

5. Usage

5.1. Basic Usage

First, import autoload.php into your code (in src/ folder) like this:

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

Then, create a new instance of the Illuminate\Validation\Validator as follows:

use Macocci7\PurephpValidation\ValidatorFactory as Validator;

$validator = Validator::make(
    data: [
        'name' => 'foo',
        'email' => '[email protected]',
        'password' => 'Passw0rd',
    ],
    rules: [
        'name' => 'required|string|min:3|max:40',
        'email' => 'required|email:rfc',
        'password' => 'required|string|min:8|max:16',
    ],
);

Now, you can check the validation results:

if ($validator->fails()) {
    var_dump($validator->errors()->message);
} else {
    echo "🎊 Passed 🎉" . PHP_EOL;
}

You can learn more about writing validation rules at the Laravel Official Documentation.

Here's also an example code for basic usage: BasicUsage.php

5.2. Setting Translations Root Path and Language

You'll probably want to place the lang folder somewhere else outside of vendor/.

You can set the Translations Root Path before creating an instance of Validator:

// Set Translations Root Path (optional)
// - The path must end with '/'.
// - 'lang/' folder must be placed under the path.
Validator::translationsRootPath(__DIR__ . '/');

You can also set the Language before creating an instance of Validator:

// Set lang: 'en' as default (optional)
Validator::lang('ja');

Here's an example code for setting Translations Root Path and Language: SetTranslationsRootPath.php

5.3. Using Password Rule Object

You can validate passwords using Laravel's Password rule object.

use Macocci7\PurephpValidation\Rules\PasswordWrapper as Password;

$validator = Validator::make(
    data: [ 'password' => 'pass' ],
    rules: [
        'password' => [
            'required',
            Password::min(8)
                ->max(16)
                // at least one letter
                ->letters()
                // at least one uppercase
                // and at least one lowercase letter
                ->mixedCase()
                // at least one number
                ->numbers()
                // at least one symbol
                ->symbols()
                // not in a data leak
                ->uncompromised(),
        ],
    ],
);

You can learn more about Laravel's Password rule object at the Laravel Official Document.

Here's an example code for using Password rule object: ValidatePassword.php

5.4. Using File Rule Object

You can validate files using Laravel's File rule object.

use Illuminate\Validation\Rule;
use Macocci7\PurephpValidation\Rules\FileWrapper as File;
use Symfony\Component\HttpFoundation\File\File as SymfonyFile;

$path = __DIR__ . '/../storage/uploaded/foo.jpg';

$validator = Validator::make(
    data: [
        'photo' => new SymfonyFile($path),
    ],
    rules: [
        'photo' => [
            'required',
            File::image()
                ->min(10)   // kilo bytes
                ->max(144)  // kilo bytes
                ->dimensions(
                    Rule::dimensions()
                        ->maxWidth(200)     // pix
                        ->maxHeight(300)    // pix
                ),
        ],
    ],
);

You can learn more about Laravel's File rule object at the Laravel Official Document.

Here's an example code for using Laravel's File rule object: ValidateFile.php

5.5. Using Instance Rule Object

You can validate objects using Instance rule object. (unique feature)

By using Instance::of($class) method as a rule, you can perform $value instanceof $class in the validation.

Instance::of() accepts class name(s) as an argument.

use Macocci7\PurephpValidation\Rules\Instance;

$validator = Validator::make(
    data: $data,
    rules: [
        'prop1' => Instance::of(Instance::class),
        'prop2' => Instance::of([
            // Macocci7\PurephpValidation\Rules\Instance
            Instance::class,
            // Macocci7\PurephpValidation\ValidatorFactory
            Validator::class,
            // Closure
            (fn () => true)::class,
        ]),
        'prop3' => Instance::of('Closure'),
    ],
);

Here's an example code for using Instance rule object: ValidateInstance.php

6. Examples

7. LICENSE

MIT


Copyright 2024 macocci7

About

illuminate/validation wrapper for pure php

Resources

License

Stars

Watchers

Forks

Packages

No packages published