validate

Declarative schema and input validator with structured per-field error messages.

v1.0.0Application & logicbundledimport "validate";
Overview Installation Documentation Examples

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
Packagevalidate
Version1.0.0 (follows the compiler)
Entry filevalidate.hphp
Sourcepackages/lib/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"]).

RuleDescription
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

Top-Level Helpers

Error Handling

Invalid schemas, invalid inputs, or failed validateOrThrow calls throw Exception with clear validate: ... messages:

Examples

01Quick 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.