Custom checks
A package or an application adds its own rules to the check: a class implementing Checking\Check, tagged in a
service provider. revision:check and assertRevisionsAreConsistent() run it after the built-in rules.
Write the rule
use Ark4ne\ApiRevisions\Checking\Check;
use Ark4ne\ApiRevisions\Checking\CheckContext;
final readonly class ReservedNameCheck implements Check
{
public function run(CheckContext $context): void
{
foreach ($context->kinds() as $kind) {
foreach ($context->order() as $revision) {
foreach ($context->shape($kind, $revision)->fields as $field) {
if (in_array($field->name, ['type', 'id'], true)) {
$context->error(
'reserved-name',
"'{$field->name}' is reserved.",
$field->headKey,
$revision,
);
}
}
}
}
}
}
The rule runs once on the enum ($context->class is null, and kinds() is empty, so the loop above skips it), then
once per class.
CheckContext | Gives |
|---|---|
$context->class | the class checked, null on the enum |
kinds() | its declaration kinds (Response, RequestBody, RequestQuery) |
order() | the revisions, oldest first, where the class exists |
shape($kind, $revision) | the shape: fields, each with its name at that revision and its headKey |
descriptors($kind) | the head descriptors |
export($kind, $revision) | the JSON Schema of that revision |
error(), warning() | add a diagnostic: code, message, field, revision |
$context->subset | true when the check runs on some classes only |
Register it
use Ark4ne\ApiRevisions\Laravel\RevisionsServiceProvider;
public function register(): void
{
$this->app->tag(
[ReservedNameCheck::class],
RevisionsServiceProvider::CHECKS,
);
}
The container builds the rule, so it can take constructor dependencies. A tagged service that is not a Check
throws a ConfigurationException.
Check some classes only
A package's own check command can run the same checker on its classes only:
use Ark4ne\ApiRevisions\Checking\Checker;
$diagnostics = app(Checker::class)->forClasses($finder)->run();
$finder is a Checking\ClassFinder. Every rule runs, including the lock comparison of the classes given; only a
locked class the finder does not give is not reported gone. A tagged rule that needs every class reads
$context->subset to skip what a subset cannot tell.
Laravel JSON:API adds its own rules this way.