Libraries / validate
Overview
Declarative schema and input validator for HolyPHP. Provides a Validator class and validate_data / validate_or_throw helpers with structured per-field error messages.
bundled — works offline
schema
rules
form
Quick start
import "validate";
$rules = [
"username" => "required|string|alnum|min:3|max:20",
"email" => "required|email",
"age" => "required|int|min:18|max:120",
"website" => "url",
"role" => "required|in:admin,editor,viewer"
];
$data = [
"username" => "alice99",
"email" => "alice@example.com",
"age" => 28,
"website" => "https://alice.dev",
"role" => "editor"
];
$result = validate_data($data, $rules);
if ($result["valid"]) {
println("Valid!");
} else {
println(json_encode($result["errors"]));
}
// Or throw an Exception on failure:
$v = new Validator($rules);
$validated = $v->validateOrThrow($data);
Installation
In a project
Inside a project (a folder with hphp.yaml, see Projects & packages ):
hphp install validate
This adds the library to hphp.yaml, installs it into .hphp/packages/validate/ and pins the exact version in hphp.lock:
require:
validate: "^1.0"
Use it
import "validate";
validate is bundled with the compiler, so import "validate"; also works in a single file without installing anything, and hphp install works offline.
Outside a project
hphp install validate # for your user, shared by all scripts
hphp create myapp # or start a project, then: hphp install validate
Documentation
Supported Rules
Rules can be specified as a pipe-delimited string ("required|string|min:3") or an array of rule strings (["required", "string", "min:3"]).
Rule Description
requiredField must be present, non-null, non-empty string (after trim), and non-empty array.
stringValue must be a string.
intValue must be an integer.
floatValue must be a float.
boolValue must be a boolean.
arrayValue must be an array.
min:<n>Minimum string length, numeric value, or array item count.
max:<n>Maximum string length, numeric value, or array item count.
emailValue must be a valid email address string.
urlValue must be a valid http:// or https:// URL string.
in:a,b,cValue must match one of the comma-separated allowed values.
alphaValue must be a non-empty string containing only ASCII letters (A-Z, a-z).
alnumValue must be a non-empty string containing only ASCII letters and digits (A-Z, a-z, 0-9).
Optional fields (rules without required) are skipped when missing or null.
API Reference
Validator
new Validator(array $rules) — Create a validator with an associative array of field rules.
validate(array $data): array — Validate $data and return ["valid" => bool, "errors" => [field => [msg, ...]]].
validateOrThrow(array $data): array — Validate $data; returns $data on success, or throws Exception with per-field error details on failure.
passes(array $data): bool — Returns true if $data passes validation.
fails(array $data): bool — Returns true if $data fails validation.
errors(): array — Returns the per-field error map from the last validation run.
Top-Level Helpers
validate_data(array $data, array $rules): array — Shorthand for (new Validator($rules))->validate($data).
validate_or_throw(array $data, array $rules): array — Shorthand for (new Validator($rules))->validateOrThrow($data).
Error Handling
Invalid schemas, invalid inputs, or failed validateOrThrow calls throw Exception with clear validate: ... messages:
validate: rules must be an associative array
validate: data must be an array
validate: rules for field '<name>' must be a string or array
validate: unknown rule '<rule>' for field '<name>'
validate: rule 'min' for field '<name>' requires a numeric parameter
validate: rule 'in' for field '<name>' requires a comma-separated list of values
validate: validation failed (<field>: <message>; ...)
Examples
01 Quick start
Bundled with the compiler — import "validate"; works offline in any file, or add it to a project with hphp install validate.
import "validate";
$rules = [
"username" => "required|string|alnum|min:3|max:20",
"email" => "required|email",
"age" => "required|int|min:18|max:120",
"website" => "url",
"role" => "required|in:admin,editor,viewer"
];
$data = [
"username" => "alice99",
"email" => "alice@example.com",
"age" => 28,
"website" => "https://alice.dev",
"role" => "editor"
];
$result = validate_data($data, $rules);
if ($result["valid"]) {
println("Valid!");
} else {
println(json_encode($result["errors"]));
}
// Or throw an Exception on failure:
$v = new Validator($rules);
$validated = $v->validateOrThrow($data);
Try it hphp install validate
hphp run main.hphp
Covered by the official test suite in tests/positive/lib_validate.hphp.