Search by

denosyscore / validation

deondazy

Validation engine and rules

Package info

github.com/denosyscore/validation

pkg:composer/denosyscore/validation

Statistics

Installs: 933

Dependents: 1

Suggesters: 0

Stars: 0

Open Issues: 0


README

Read-only split. This package is developed in denosyscore/framework under packages/validation. Open issues and pull requests there.

Validation engine and rules

Installation

composer require denosyscore/validation

Quick start

Validate arrays against rule strings. The built-in rules work without a container or service provider.

<?php

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

use Denosys\Validation\Validator;

$input = ['email' => 'ada@example', 'age' => '15', 'plan' => 'pro'];

$validator = Validator::make($input, [
    'email' => 'required|email',
    'age'   => 'required|integer|min:18',
    'plan'  => 'required|in:free,pro',
    'name'  => 'nullable|string|max:80',
]);

if ($validator->fails()) {
    echo $validator->errors()->first('email'), "\n";  // the email error message
    echo $validator->errors()->first('age'), "\n";    // the minimum-age message
    var_dump($validator->errors()->has('plan'));      // bool(false)
}

$valid = Validator::make(['email' => 'ada@example.com'], ['email' => 'required|email']);
print_r($valid->validated());  // ['email' => 'ada@example.com']

Included Modules

  • src/Validation/*

Database-backed rules

The unique and exists rules resolve the database package's Denosys\Database\Connection\Connection binding lazily when a rule first runs. The legacy db binding remains a fallback. When both bindings are present, the typed connection takes precedence. Register the database provider before validating with these rules.

The date, before, and after rules use Carbon. Composer installs the declared Carbon runtime dependency with validation; consumers do not need to install it separately.

Direct field errors

When a field-specific failure is discovered after ordinary rule validation, raise it without constructing an empty validator in the caller:

throw \Denosys\Validation\ValidationException::withMessages([
    'email' => 'The email address is unavailable.',
]);

Values may be a string or a non-empty list of strings. The existing ValidationException(Validator $validator) constructor remains supported.

The unique:accounts,email,1 rule checks the email column in accounts while excluding the row with id = 1. Its third positional parameter is named ignore_id by Unique::parameterNames().

HTTP exception responses

Validation itself does not depend on HTTP. Install denosyscore/http v0.3.2 or later for Denosys\Http\Exceptions\ValidationExceptionHandler. HTTP also provides the former Denosys\Validation\Handlers\ValidationExceptionHandler name as an alias for existing integrations. Composer prevents pairing this validation release with an older HTTP version that lacks the alias.

HTTP-uploaded files

The file rule accepts valid PSR-7 uploads from any source. Add http_uploaded when a browser-facing form must require a file received by PHP's HTTP upload handling:

$validator = Validator::make(
    ['document' => $uploadedFile],
    ['document' => 'required|file|http_uploaded|mimes:jpg,png,pdf'],
);

The rule checks a successful, nonempty upload and calls is_uploaded_file() on its stream path. It is intended for validation before moving the upload.

Development

composer validate --strict
find src tests -type f -name '*.php' -print0 | xargs -0 -n1 php -l
composer test
composer analyse

RuleInterface::validate() and message() receive named parameters from the validator. Parameter values may include numbers, null, or lists; rules that support direct positional calls may also receive integer keys. The static analysis fixture checks this public parameter contract.

Releases

CI tests against database v0.2.3, v0.3.0, and v0.4.0. After CI passes on main, create an annotated semantic version tag from that reviewed commit and push it. The Release workflow publishes a GitHub Release. Confirm the matching version is available in the Composer package index before updating consumers.