PHP code example of tezmanian / utils-validate
1. Go to this page and download the library: Download tezmanian/utils-validate library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
tezmanian / utils-validate example snippets
use Tezmanian\Utils\Validate\Guard;
// Basic assertions
$name = Guard::notNull($request->get('name'));
$email = Guard::notEmpty($request->get('email'));
$score = Guard::inRange($input, min: 0, max: 100);
$user = Guard::instanceOf($entity, User::class);
// Boolean assertions
Guard::isTrue($user->isActive());
Guard::isFalse($user->isBanned());
// Numeric string
Guard::isNumeric($request->get('amount'));
// Custom predicate
Guard::that($price > 0, fn() => new DomainException('Price must be positive'));
Guard::notNull($value, \InvalidArgumentException::class);
Guard::notNull($value, new \DomainException('Value is
use Tezmanian\Utils\Validate\Rules;
Rules::isEmail('[email protected] '); // true
Rules::isUrl('https://example.com'); // true
Rules::isUrl('http://...', v6('::1'); // true
Rules::minLength('hello', 3); // true
Rules::maxLength('hello', 10); // true
Rules::matches('abc123', '/^[a-z0-9]+$/'); // true
Rules::between(5, 1, 10); // true
Rules::isPositive(1); // true
Rules::isNegative(-1); // true
Rules::notEmpty('hello'); // true
Rules::isDate('2026-04-23'); // true
Rules::isDate('23.04.2026', 'd.m.Y'); // true
use Tezmanian\Utils\Validate\Composite;
use Tezmanian\Utils\Validate\Rules;
$isStrongPassword = Composite::all(
fn($v) => Rules::minLength($v, 8),
fn($v) => Rules::matches($v, '/[A-Z]/'),
fn($v) => Rules::matches($v, '/[0-9]/'),
);
$isStrongPassword('Secret42!'); // true
$isStrongPassword('weak'); // false
// OR — passes when at least one rule matches
$isEmailOrUuid = Composite::any(
fn($v) => Rules::isEmail($v),
fn($v) => Rules::isUuid($v),
);
// NONE — passes when no rule matches
$isNotReserved = Composite::none(
fn($v) => $v === 'admin',
fn($v) => $v === 'root',
);
use Tezmanian\Utils\Validate\Schema;
use Tezmanian\Utils\Validate\Guard;
Schema::validate($data, [
'name' => 'string',
'age' => 'int',
'active' => 'bool',
'email' => '?string', // optional
'score' => fn($v) => Guard::inRange($v, 0, 100), // custom rule
'address' => [ // nested schema
'city' => 'string',
'country' => '?string',
],
]);
use Tezmanian\Utils\Validate\TypeCast;
TypeCast::toInt('42'); // 42
TypeCast::toInt(3.0); // 3
TypeCast::toInt(3.7); // throws — fractional float
TypeCast::toFloat('3.14'); // 3.14
TypeCast::toString(42); // '42'
TypeCast::toBool('true'); // true
TypeCast::toBool('yes'); // true
TypeCast::toBool('off'); // false
TypeCast::toBool('maybe'); // throws — ambiguous string
// Nullable variants — pass through null, convert everything else
TypeCast::toNullableInt(null); // null
TypeCast::toNullableInt('42'); // 42
TypeCast::toNullableFloat(null); // null
TypeCast::toNullableString(null); // null
TypeCast::toNullableBool(null); // null
use Tezmanian\Utils\Validate\Each;
use Tezmanian\Utils\Validate\Guard;
$orders = [
['amount' => 50],
['amount' => -10],
];
Each::validate($orders, function (mixed $order): void {
if ($order['amount'] <= 0) {
throw new \DomainException('Amount must be positive.');
}
});
// throws DomainException: "items[1]: Amount must be positive."
// Custom path label
Each::validate($lines, fn($line) => Guard::notEmpty($line), path: 'lines');
// throws: "lines[2]: ..."