Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/giann/schematics
Translates PHP classes to JSON Schema by annotating them with attributes. Provides also validation.
https://github.com/giann/schematics
api json json-schema php php74 php81
Last synced: 24 days ago
JSON representation
Translates PHP classes to JSON Schema by annotating them with attributes. Provides also validation.
- Host: GitHub
- URL: https://github.com/giann/schematics
- Owner: giann
- License: mit
- Created: 2022-03-22T07:35:35.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-09-27T08:26:04.000Z (about 1 month ago)
- Last Synced: 2024-09-29T20:01:19.526Z (about 1 month ago)
- Topics: api, json, json-schema, php, php74, php81
- Language: PHP
- Homepage:
- Size: 255 KB
- Stars: 5
- Watchers: 5
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# schematics
Translates PHP classes to JSON Schema and back.
## Supported Drafts
Only commonly used drafts `draft-04` and `2020-12` are supported.
## Example
```php
enum Sex: string
{
case Male = 'male';
case Female = 'female';
case Other = 'other';
}#[ObjectSchema]
class Person
{
public function __construct(
#[StringSchema(format: Format::Uuid)]
#[Description('unique id of the person')]
public string $id,#[ArraySchema(
items: new StringSchema(),
minContains: 1
)]
public array $names,#[IntegerSchema(minimum: 0)]
public int $age,#[StringSchema(enumClass: Sex::class)]
public string $sex,// Inferred $ref to self
public ?Person $father = null
) {
}
}enum Power: string
{
case Fly = 'weeeee!';
case Strong = 'smash!';
case Psychic = 'hummmm!';
}// Infer $allOf Person
#[ObjectSchema]
class Hero extends Person
{
public function __construct(
string $id,
array $names,
int $age,
string $sex,
?Person $father = null,// Infers string property
public string $superName,#[StringSchema(enumClass: Power::class)]
public string $power
) {
parent::__construct($id, $names, $age, $sex, $father);
}
}
```Results in the following JSON Schema:
```json
{
"type": "object",
"$defs": {
"Person": {
"type": "object",
"properties": {
"id": {
"type": "string",
"description": "unique id of the person",
"format": "uuid"
},
"names": {
"type": "array",
"items": {
"type": "string"
},
"minContains": 1
},
"age": {
"type": "integer",
"minimum": 0
},
"sex": {
"type": "string",
"enum": ["male", "female", "other"]
},
"father": {
"oneOf": [
{
"type": "null"
},
{
"$ref": "#/$defs/Person"
}
]
}
},
"required": ["id", "names", "age", "sex", "father"]
}
},
"allOf": [
{
"$ref": "#/$defs/Person"
}
],
"properties": {
"superName": {
"type": "string"
},
"power": {
"type": "string",
"enum": ["weeeee!", "smash!", "hummmm!"]
}
},
"required": ["superName", "power"]
}
```## Not Yet Supported
- `$dynamicRef`
- `$dynamicAnchor`