specdocular/php-json-schema

PHP implementation of JSON Schema (Draft 2020-12)

Installs: 376

Dependents: 2

Suggesters: 0

Security: 0

Stars: 0

Watchers: 0

Forks: 0

Open Issues: 0

pkg:composer/specdocular/php-json-schema

v0.1.0 2026-02-12 18:50 UTC

This package is auto-updated.

Last update: 2026-02-13 10:59:30 UTC


README

Latest Version on Packagist PHP Version Tests codecov Code Style

A type-safe, fluent PHP implementation of JSON Schema Draft 2020-12.

Installation

composer require specdocular/php-json-schema

Usage

Build JSON Schema definitions using a fluent, type-safe API:

use Specdocular\JsonSchema\Draft202012\StrictFluentDescriptor;
use Specdocular\JsonSchema\Draft202012\Keywords\Properties\Property;

$schema = StrictFluentDescriptor::object()
    ->properties(
        Property::create('name', StrictFluentDescriptor::string()->minLength(1)),
        Property::create('email', StrictFluentDescriptor::string()->format('email')),
        Property::create('age', StrictFluentDescriptor::integer()->minimum(0)),
    )
    ->required('name', 'email');

// Compile to array
$compiled = $schema->compile();

// Or encode directly to JSON
$json = json_encode($schema, JSON_PRETTY_PRINT);

Strict vs Loose Descriptors

  • StrictFluentDescriptor (recommended) — provides type-specific method autocomplete. Methods like minLength() are only available on string schemas, minimum() only on numeric schemas, etc.
  • LooseFluentDescriptor — exposes all keywords on every schema. Useful when building schemas with multiple types.

Features

  • Full JSON Schema Draft 2020-12 support (all 50 keywords, 7 vocabularies)
  • Type-safe fluent API with IDE autocomplete
  • Extensible keyword and vocabulary system
  • Built-in schema validation (VocabularyValidator, MetaSchemaValidator)
  • Framework-agnostic — no dependencies on Laravel or any framework

Note: This library builds JSON Schema definitions. It does not validate data against schemas — use a validation library like justinrainbow/json-schema for that.

Related Packages

Package Description
specdocular/php-openapi Object-oriented OpenAPI 3.1.x builder (uses this package)
specdocular/laravel-rules-to-schema Convert Laravel validation rules to JSON Schema (uses this package)
specdocular/laravel-openapi Laravel integration for OpenAPI generation

License

MIT. See LICENSE for details.