- Notifications
You must be signed in to change notification settings - Fork0
Provides the ability to generate JSON schemas from Data Transfer Object (DTO) classes
License
context-hub/json-schema-generator
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
The JSON Schema Generator is a PHP package that simplifies the generation ofJSON schemas from Data Transfer Object (DTO) classes.It supports PHP enumerations and generic type annotations for arrays and provides an attribute for specifying title, description, and default value.
Main use case - structured output definition for LLMs.
Make sure that your server is configured with the following PHP versions and extensions:
- PHP >=8.1
You can install the package via Composer:
composer require context-hub/json-schema-generator
To generate a schema for a DTO, instantiate theSpiral\JsonSchemaGenerator\Generator and call thegenerate method,passing the DTO class as an argument (fully qualified class name or reflection). The method will return an instance ofSpiral\JsonSchemaGenerator\Schema.
Let's create a simple data transfer object:
namespaceApp\DTO;useSpiral\JsonSchemaGenerator\Attribute\Field;class Movie{publicfunction__construct( #[Field(title:'Title', description:'The title of the movie')]publicreadonlystring$title, #[Field(title:'Year', description:'The year of the movie')]publicreadonlyint$year, #[Field(title:'Description', description:'The description of the movie')]publicreadonly ?string$description =null,publicreadonly ?string$director =null, #[Field(title:'Release Status', description:'The release status of the movie')]publicreadonly ?ReleaseStatus$releaseStatus =null, ) { }}
This DTO has areleaseStatus, which is an enum. Let's create it:
namespaceApp\DTO;enum ReleaseStatus:string{case Released ='Released';case Rumored ='Rumored';case PostProduction ='Post Production';case InProduction ='In Production';case Planned ='Planned';case Canceled ='Canceled';}
Now, let's generate a schema for this DTO:
useSpiral\JsonSchemaGenerator\Generator;useApp\DTO\Movie;$generator =newGenerator();$schema =$generator->generate(Movie::class);
NoteAdditionally, the package provides the
Spiral\JsonSchemaGenerator\GeneratorInterface,which can be integrated into your application's dependency container for further customization and flexibility.
TheSpiral\JsonSchemaGenerator\Schema object implements theJsonSerializable interface, allowing easy conversionof the schema into either JSON or a PHP array.
Example array output:
['properties' => ['title' => ['title' =>'Title','description' =>'The title of the movie','type' =>'string', ],'year' => ['title' =>'Year','description' =>'The year of the movie','type' =>'integer', ],'description' => ['title' =>'Description','description' =>'The description of the movie','type' =>'string', ],'director' => ['type' =>'string', ],'releaseStatus' => ['title' =>'Release Status','description' =>'The release status of the movie','allOf' => [ ['$ref' =>'#/definitions/ReleaseStatus', ], ], ], ],'required' => ['title','year', ],'definitions' => ['ReleaseStatus' => ['title' =>'ReleaseStatus','type' =>'string','enum' => ['Released','Rumored','Post Production','In Production','Planned','Canceled', ], ], ],];
Class properties can be arrays, and the type of elements within the array can be specified using PHPDoc annotations.
For example, we have a DTO with an array of objects:
namespaceApp\DTO;useSpiral\JsonSchemaGenerator\Attribute\Field;finalclass Actor{publicfunction__construct(publicreadonlystring$name,/** * @var array<Movie> */publicreadonlyarray$movies = [], ) { }}
In this example, we use a PHPDoc block to indicate that the property$movies contains an array ofMovie objects.
NoteVarious documentation type annotations are supported, including
@var array<Movie>,@var Movie[],and@var list<Movie>. For promoted properties, you can use annotations like@param array<Movie> $movies,@param Movie[] $movies, and@param list<Movie> $movies.
Now, let's generate a schema for this DTO:
useSpiral\JsonSchemaGenerator\Generator;useApp\DTO\Actor;$generator =newGenerator();$schema =$generator->generate(Actor::class);
Example array output:
['properties' => ['name' => ['type' =>'string', ],'movies' => ['type' =>'array','items' => ['$ref' =>'#/definitions/Movie', ],'default' => [], ], ],'required' => ['name', ],'definitions' => ['Movie' => ['title' =>'Movie','type' =>'object','properties' => ['title' => ['title' =>'Title','description' =>'The title of the movie','type' =>'string', ],'year' => ['title' =>'Year','description' =>'The year of the movie','type' =>'integer', ],'description' => ['title' =>'Description','description' =>'The description of the movie','type' =>'string', ],'director' => ['type' =>'string', ],'releaseStatus' => ['title' =>'Release Status','description' =>'The release status of the movie','allOf' => [ ['$ref' =>'#/definitions/ReleaseStatus', ], ], ], ],'required' => ['title','year', ], ],'ReleaseStatus' => ['title' =>'ReleaseStatus','type' =>'string','enum' => ['Released','Rumored','Post Production','In Production','Planned','Canceled', ], ] ],];
composertestPlease seeCHANGELOG for more information on what has changed recently.
Please seeCONTRIBUTING for details.
The MIT License (MIT). Please seeLicense File for more information.
About
Provides the ability to generate JSON schemas from Data Transfer Object (DTO) classes
Resources
License
Code of conduct
Contributing
Security policy
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Releases
Languages
- PHP100.0%