Search by

specdocular / php-json-schema

mohammad-alavi

PHP implementation of JSON Schema (Draft 2020-12)

Package info

github.com/specdocular/php-json-schema

pkg:composer/specdocular/php-json-schema

Statistics

Installs: 755

Dependents: 2

Suggesters: 0

Stars: 0

Open Issues: 0

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

This package is auto-updated.

Last update: 2026-09-08 18:08:47 UTC


README

Latest Version on Packagist PHP Version Tests codecov Code Style

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

Requirements

  • PHP 8.2 or higher

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

Contributing

Contributions are welcome. See CONTRIBUTING.md for setup, the required checks, and versioning.

Security

If you discover a security vulnerability, please follow the process in SECURITY.md rather than opening a public issue.

License

MIT. See LICENSE for details.