uuki / schemable-validator
Schema based validation plugin.
Requires
- php: >=7.4
- symfony/polyfill-php80: ^1.0
Requires (Dev)
- opis/json-schema: ^2.6
- phpunit/phpunit: ^9.6
- respect/validation: 2.2.4
Suggests
- opis/json-schema: Enables OpisAdapter (strict JSON Schema 2020-12 backend). Not needed for the default NativeAdapter.
- respect/validation: Enables the RespectAdapter engine and the Respect driver (RespectRules / SV::respect / postalCode / creditCard / iban). Not needed for the default dependency-free NativeAdapter.
This package is auto-updated.
Last update: 2026-06-21 03:57:02 UTC
README
A PHP-first validation library whose core purpose is defining and executing validation constraints on the server. Its distinguishing feature is the ability to export those constraints as JSON Schema draft 2020-12, making the same rules available to any JavaScript framework on the client — without maintaining duplicate definitions across the stack.
The name reflects this: validator is the primary role, schemable is its defining feature.
How it works
SV::object([...]) ← PHP: single source of truth
│
├─ toValidator() → server-side validation (Respect/Validation)
│
└─ toJson() → JSON Schema draft 2020-12
│
└─ REST endpoint (WordPress)
│
└─ @schemable-validator/client → client-side validation
Zod / any JS validator
Constraints that cannot be expressed in JSON Schema (file uploads, custom rules) are recorded in x-unmapped-fields and delegated to the server automatically by the client library.
Packages
| Package | Description |
|---|---|
uuki/schemable-validator |
PHP core library (framework-agnostic) |
wp-schemable-validator |
WordPress plugin — REST endpoint, helpers, admin UI |
@schemable-validator/client |
TypeScript client — validates against JSON Schema output |
Quick Start
1. Define constraints (PHP)
use SchemableValidator\SV; $schema = SV::object([ 'name' => SV::string()->min(1)->max(100), 'email' => SV::string()->email(), 'tel' => SV::string()->pattern('^(0\d{9,10}|0\d{1,4}-\d{1,4}-\d{3,4})$')->optional(), 'type' => SV::enum(['general', 'support', 'other']), 'body' => SV::string()->min(10), ]);
2. Server-side validation
$result = $schema->toValidator()->validate($_POST)->getResult(); // { "name": { "value": "...", "is_valid": true, "errors": null }, ... }
3. Expose as REST endpoint (WordPress)
// GET /wp-json/schv/v1/schema/contact → JSON Schema schv_register_schema('/schema/contact', $schema);
4. Client-side validation (TypeScript client)
import { validateObject, isAllValid, extractErrors } from '@schemable-validator/client' const schema = await fetch('/wp-json/schv/v1/schema/contact').then(r => r.json()) const result = validateObject(formData, schema) if (!isAllValid(result)) { console.log(extractErrors(result)) }
Or with Zod:
import { z } from 'zod' // Build a Zod schema from the fetched JSON Schema, then extend with // custom .superRefine() for x-unmapped-fields (see docs/06-custom-validation.md) const zodSchema = buildZodSchema(schema) const parsed = zodSchema.safeParse(formData)
JSON Schema output
toJson() converts the PHP schema definition to JSON Schema draft 2020-12:
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"properties": {
"name": { "type": "string", "minLength": 1, "maxLength": 100 },
"email": { "type": "string", "format": "email" },
"tel": { "type": "string", "pattern": "^(0\\d{9,10}|0\\d{1,4}-\\d{1,4}-\\d{3,4})$" },
"type": { "type": "string", "enum": ["general", "support", "other"] },
"body": { "type": "string", "minLength": 10 }
},
"required": ["name", "email", "type", "body"]
}
Installation
# PHP core composer require uuki/schemable-validator:0.x@dev # WordPress plugin cd packages/wp-schemable-validator && composer install --no-dev # TypeScript client npm install @schemable-validator/client
See docs/01-installation.md for full setup.
Documentation
| Installation | Requirements, package structure |
| Feature Guide | Validator, file validation, CSRF, reCAPTCHA |
| Interfaces | WordPress helpers, REST API |
| Development | Local playground, E2E tests |
| SchemaBuilder | SV::object() API, JSON Schema output samples |
| Custom Validation | External libraries (libphonenumber etc.), x-unmapped-fields |
Dependencies
- Respect/Validation ^2.2