uuki/schemable-validator

Schema based validation plugin.

Maintainers

Package info

github.com/uuki/schemable-validator

Type:package

pkg:composer/uuki/schemable-validator

Statistics

Installs: 17

Dependents: 0

Suggesters: 0

Stars: 0

v0.21.0 2026-06-21 03:53 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