<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
basillangevin / laravel-data-json-schemas example snippets
use BasilLangevin\LaravelDataJsonSchemas\Enums\JsonSchemaDialect;
return [
/*
|--------------------------------------------------------------------------
| JSON Schema Dialect
|--------------------------------------------------------------------------
|
| If this value is not null, a "$schema" keyword will be set to
| this identifier in the root of each generated JSON Schema.
| This value won't change how JSON Schemas are generated.
*/
'dialect' => JsonSchemaDialect::Draft201909,
];
/**
* Transform a Spatie Data class into a JSON Schema.
*
* @param class-string<Data> $dataClass
*/
JsonSchema::toArray(string $dataClass): array;
/**
* Transform a Spatie Data class into an array schema.
* The "items" will be instances of the Data class.
*
* @param class-string<Data> $dataClass
*/
JsonSchema::collectToArray(string $dataClass): array;
/**
* Transform a Spatie Data class into a JSON Schema object.
* The object can then be modified with keyword methods.
*
* @param class-string<Data> $dataClass
*/
JsonSchema::make(string $dataClass): BasilLangevin\LaravelDataJsonSchemas\Schemas\ObjectSchema;
/**
* Transform a Spatie Data class into an ArraySchema object.
* The object can then be modified with keyword methods.
*
* @param class-string<Data> $dataClass
*/
JsonSchema::collect(string $dataClass): BasilLangevin\LaravelDataJsonSchemas\Schemas\ArraySchema;
use Spatie\LaravelData\Attributes\Validation\In;
use Spatie\LaravelData\Attributes\Validation\Min;
use Spatie\LaravelData\Data;
class BikeData extends Data
{
public function __construct(
public string $brand,
/** The model name of the bike. */
public string $model,
#[Min(2010)]
public int $year,
#[In(['red', 'blue', 'green'])]
public ?string $color = null,
public bool $is_electric = false,
) {}
}
#[Title('The title of a data class.')]
class BikeData extends Data
{
public function __construct(
#[Title('The title of a property.')]
public string $brand,
) {}
}
/**
* The title of a data class.
*
* The description of a data class.
*/
class BikeData extends Data
{
public function __construct(
/**
* The title of a property.
*
* The description of a property.
*/
public string $brand,
) {}
}
#[Description('The description of a data class.')]
class BikeData extends Data
{
public function __construct(
#[Description('The description of a property.')]
public string $brand,
) {}
}
/**
* @var string $model The description of the model property.
*/
class BikeData extends Data
{
public function __construct(
/** @param string $brand The description of the brand property. */
public string $brand,
public string $model,
) {}
}
/**
* The description of a data class.
*/
class BikeData extends Data
{
/**
* The description of a property.
*/
public string $brand;
}
/**
* The title of a data class.
*
* The description of a data class.
*/
class BikeData extends Data
{
/**
* The title of a property.
*
* The description of a property.
*/
public string $brand;
}
class BikeData extends Data
{
public function __construct(
public string $brand = 'Trek',
) {}
}
use Spatie\LaravelData\Attributes\Validation\Min;
class BikeData extends Data
{
public function __construct(
#[Min(3)]
public string $name,
#[Min(10)]
public int $quantity,
) {}
}
$schema = JsonSchema::toArray(BikeData::class);