Custom types
A custom type is a Schema\Descriptor subclass. It normalizes, serializes and describes its value. Register it at
boot under a name, then use it on Schema like a built-in helper.
Declare the type
use Ark4ne\ApiRevisions\Definition\Kind;
use Ark4ne\ApiRevisions\Definition\TypeDefinition;
use Ark4ne\ApiRevisions\Schema\Descriptor;
use Ark4ne\ApiRevisions\Schema\InvalidValue;
/** @extends Descriptor<Money> */
final class MoneySchema extends Descriptor
{
public function normalize(mixed $value): mixed
{
return $value instanceof Money
? $value
: new InvalidValue($value, Kind::Custom, null);
}
public function serialize(mixed $value): mixed
{
if (! $value instanceof Money) {
return $value;
}
return ['amount' => $value->amount, 'currency' => $value->currency];
}
protected function type(): TypeDefinition
{
return TypeDefinition::custom('money', [
'type' => 'object',
'properties' => [
'amount' => ['type' => 'integer'],
'currency' => ['type' => 'string'],
],
'required' => ['amount', 'currency'],
]);
}
}
| Method | Role |
|---|---|
normalize(mixed $value) | turns a read value into the PHP value of the type; returns an InvalidValue to refuse it |
serialize(mixed $value) | turns the PHP value into its wire value (optional: the value is kept as is by default) |
type() | the definition: TypeDefinition::custom($name, $jsonSchema) |
declaredRules() | the Laravel rules a request applies to it, merged with parent::declaredRules() |
The type is exported once as a component under $defs.
Register and use it
use Ark4ne\ApiRevisions\Schema\TypeRegistry;
public function boot(): void
{
TypeRegistry::register('money', MoneySchema::class);
}
'total' => Schema::money('total_cents'),
The arguments of Schema::money(...) are those of MoneySchema's constructor, which receives the source first. Map
the name for PHPStan in static analysis.
Declare its conversions
A was(schema:) between a custom type and another type needs down: / up:, unless the type declares its
conversions. Implement Schema\DeclaresConversions:
use Ark4ne\ApiRevisions\Conversion\Converter;
use Ark4ne\ApiRevisions\Conversion\Converters;
use Ark4ne\ApiRevisions\Definition\Direction;
use Ark4ne\ApiRevisions\Schema\DeclaresConversions;
final class MoneySchema extends Descriptor implements DeclaresConversions
{
public static function conversions(Converters $converters): Converters
{
foreach (Direction::cases() as $direction) {
$converters = $converters
->register(
'money',
Kind::Float,
$direction,
static fn(): Converter => new MoneyToFloat(),
)
->register(
Kind::Float,
'money',
$direction,
static fn(): Converter => new FloatToMoney('EUR'),
);
}
return $converters;
}
// ...
}
register($from, $to, $direction, $factory) takes a Kind or the name of a custom type on each side;
Direction::Down is for responses, Direction::Up for requests. The factory returns a Converter (or null to
decline):
use Ark4ne\ApiRevisions\Conversion\Converter;
final class MoneyToFloat implements Converter
{
public function convert(mixed $value, mixed $subject = null): mixed
{
return $value instanceof Money ? $value->amount / 100 : $value;
}
public function lossless(): bool
{
return false; // an automatic use warns conversion-lossy
}
public function extraRules(): array
{
return []; // rules the older request shape adds, such as 'numeric'
}
}
Fragments that do not change values
A type whose JSON Schema varies by instance while its values stay the same (a relationship naming its related type,
for instance) declares it in conversions():
public static function conversions(Converters $converters): Converters
{
return $converters->identity('relation');
}
A was(schema:) that only changes its fragment then needs no conversion, and the change still shows in the export,
the lock and the changelog. Such a type is exported in place rather than as a component, so two of its instances in
one revision never conflict.
Rules with a JSON Schema
For a smaller need, a Laravel rule object implementing Schema\DescribesSchema adds its keywords to the export of the
field it is applied to:
final class Iban implements ValidationRule, DescribesSchema
{
public function toSchema(): array
{
return ['pattern' => '^[A-Z]{2}[0-9]{2}[A-Z0-9]{11,30}$'];
}
// validate() ...
}
A rule whose keywords override the field's type (type, for instance) is reported by the check (schema-override).