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
Requires
- php: ^8.2
- webmozart/assert: ^1.11
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.8.0
- justinrainbow/json-schema: ^6.0
- pestphp/pest: ^3.2
- pestphp/pest-plugin-type-coverage: ^3.0
- phpstan/phpstan: ^2.1
- phpstan/phpstan-phpunit: ^2.0
- phpstan/phpstan-strict-rules: ^2.0
- phpstan/phpstan-webmozart-assert: ^2.0
- rector/rector: ^2.0
- slevomat/coding-standard: ^8.15
- squizlabs/php_codesniffer: *
- vimeo/psalm: ^6.0.0
README
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 likeminLength()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-schemafor 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.