Download the PHP package spiral/json-schema-generator without Composer

On this page you can find all versions of the php package spiral/json-schema-generator. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package json-schema-generator

JSON Schema Generator

PHP Version Require Latest Stable Version phpunit psalm Total Downloads psalm-level

Overview

The JSON Schema Generator is a PHP package that simplifies the generation of JSON schemas from Data Transfer Object (DTO) classes.

Main use case: Structured output definition for LLMs.

Table of Contents

Requirements

Make sure that your server is configured with the following PHP versions and extensions:

Installation

You can install the package via Composer:

Basic Usage

Let's create a simple DTO with an enum:

To generate a schema for a DTO, instantiate the Spiral\JsonSchemaGenerator\Generator and call the generate method, passing the DTO class as an argument (fully qualified class name or reflection). The method will return an instance of Spiral\JsonSchemaGenerator\Schema.

Note The package provides the Spiral\JsonSchemaGenerator\GeneratorInterface, which can be integrated into your application's dependency container for further customization and flexibility.

The generated schema for this DTO would include the following structure:

Array Type Annotations

The generator supports arrays of objects with type information from PHPDoc annotations:

Note Various documentation type annotations are supported:

  • @var array<Movie>
  • @var Movie[]
  • @var list<Movie>

For constructor-promoted properties, you can use annotations like:

  • @param array<Movie> $movies
  • @param Movie[] $movies
  • @param list<Movie> $movies

Generated schema (simplified):

Polymorphic Arrays (anyOf)

The generator supports arrays that contain different types of DTOs using PHPDoc annotations like @var list<Movie|Series>:

The generated schema will include an anyOf definition in the items section:

Here's what the Series class might look like:

Note When using polymorphic arrays, make sure all referenced DTOs are properly annotated so their definitions can be generated correctly.

Union Types

The JSON Schema Generator supports native PHP union types (introduced in PHP 8.0), including nullable and multi-type definitions:

The generated schema will include a oneOf section to reflect the union types:

Constraint Attributes

Generator supports dedicated constraint attributes that provide a clean, modular approach to validation rules.

Available Constraint Attributes

String Constraints

Numeric Constraints

Array Constraints

General Constraints

Usage Examples

String Validation

Numeric Validation

Array and Enum Validation

Generated Schema Output

The constraint attributes generate clean, standards-compliant JSON Schema validation rules:

Type Safety

Constraint attributes are automatically validated for type compatibility:

PHPDoc Validation Constraints

Generator supports extracting validation constraints from PHPDoc comments, providing rich validation rules directly in your generated schemas.

Supported PHPDoc Constraints

Numeric Constraints

String Constraints

Array Constraints

Example Usage

The generated schema will include validation constraints:

Format Support

The generator supports JSON Schema format validation through the Format enum:

Available Formats

Additional Properties

The generator supports defining additional properties for object types using the AdditionalProperties attribute. This is useful for creating dynamic objects with a specific property type.

The generated schema will include additionalProperties definitions:

Supported Value Types

The AdditionalProperties attribute supports the following value types:

Example with Multiple Dynamic Property Types

Configuration Options

Property Data Extractors

The generator uses a modular property data extractor system that allows you to customize how validation constraints are extracted from properties:

Available Extractors:

Usage Examples:

Custom Property Data Extractors

You can create custom property data extractors by implementing the PropertyDataExtractorInterface:

Integration with Valinor

The JSON Schema Generator works perfectly with the Valinor PHP package for complete data mapping and validation workflows. Valinor can validate incoming data based on the same PHPDoc constraints that the generator uses to create JSON schemas.

Installation

First, install Valinor alongside the JSON Schema Generator:

Complete Schema and Mapping Solution

Here's a complete example showing how to combine both packages:

Usage Example

Benefits of This Integration

  1. Consistent Validation: Both packages respect the same PHPDoc validation constraints
  2. Schema Generation: Generate JSON schemas for API documentation or LLM structured output
  3. Data Mapping: Safely convert incoming JSON data to strongly-typed PHP DTOs
  4. Runtime Validation: Valinor validates data against the same constraints used in schema generation
  5. Error Handling: Get detailed validation errors when data doesn't match your DTO structure

Real-world Example

API Endpoint Example

This integration is particularly useful for API endpoints:

Error Handling

Both packages provide detailed error information:

Testing

Contributing

Please see CONTRIBUTING for details.

License

The MIT License (MIT). Please see License File for more information.


All versions of json-schema-generator with dependencies

PHP Build Version
Package Version
Requires php Version >=8.3
symfony/property-info Version ^7.2.0 || ^8.0.0
phpstan/phpdoc-parser Version ^1.33 | ^2.1
phpdocumentor/reflection-docblock Version ^5.3
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package spiral/json-schema-generator contains the following files

Loading the files please wait ....