PHP code example of philiprehberger / php-schema-validator
1. Go to this page and download the library: Download philiprehberger/php-schema-validator 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/ */
philiprehberger / php-schema-validator example snippets
$schema = Schema::object([
'name' => Schema::string(),
'nickname' => Schema::string()->optional(), // field may be absent
'bio' => Schema::string()->nullable(), // field may be null
]);
Schema::string()->min(3)->max(50); // length constraints
Schema::string()->email(); // email format
Schema::string()->url(); // URL format
Schema::string()->uuid(); // UUID format
Schema::string()->regex('/^\d+$/'); // custom pattern
Schema::enum(['draft', 'published', 'archived']);
Schema::any(); // accepts any non-null value
Schema::any()->nullable(); // accepts anything including null
$schema = Schema::object([
'username' => Schema::string()->min(3)->custom(function (string $value): ?string {
if (str_starts_with($value, 'admin')) {
return 'must not start with "admin"';
}
return null;
}),
'age' => Schema::int()->min(0)->custom(function (int $value): ?string {
if ($value % 2 !== 0) {
return 'must be an even number';
}
return null;
}),
]);
$result = $schema->validateData(['username' => 'admin_user', 'age' => 25]);
$result->errors();
// ["username must not start with "admin"", "age must be an even number"]
$schema = Schema::object([
'password' => Schema::string()->min(8),
'password_confirm' => Schema::string(),
'start_date' => Schema::string(),
'end_date' => Schema::string(),
])->crossField(function (array $data): ?string {
if ($data['password'] !== $data['password_confirm']) {
return 'password_confirm must match password';
}
return null;
})->crossField(function (array $data): ?string {
if ($data['start_date'] >= $data['end_date']) {
return 'end_date must be after start_date';
}
return null;
});
$result = $schema->validateData([
'password' => 'secret123',
'password_confirm' => 'different',
'start_date' => '2026-03-20',
'end_date' => '2026-03-10',
]);
$result->errors();
// ["password_confirm must match password", "end_date must be after start_date"]
$schema = Schema::object([
'type' => Schema::string(),
'email' => Schema::string()->email(),
])->when('type', 'business', [
'company_name' => Schema::string()->min(1),
'tax_id' => Schema::string(),
]);
// When type is 'business', company_name and tax_id are also validated
// When type is anything else, those fields are ignored
$result = $schema->validateData($data);
$result = $result->withMessages([
'name' => 'Please enter your full name',
'email' => 'A valid email address is
bash
composer
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.