Extending

Rule mappers

Describe a validation rule the generator does not know, or change how a built-in rule is described.

A rule mapper turns one validation rule into constraints of a field. The generator has one per built-in rule (SizeRuleMapper handles min, max, between and size); add yours for a rule registered by name with Validator::extend(), or to replace a built-in one.

For a rule object of your application, implement DescribesRule on the rule instead; see A rule that describes itself.

Write a mapper

app/Docs/PhoneRuleMapper.php
namespace App\Docs;

use Ark4ne\OpenApi\Context\GenerationContext;
use Ark4ne\OpenApi\Description\FieldDescription;
use Ark4ne\OpenApi\Extraction\Rules\RuleInvocation;
use Ark4ne\OpenApi\Extraction\Rules\RuleMapper;

/**
 * `phone`, a rule registered with Validator::extend(): an E.164 phone number.
 */
final class PhoneRuleMapper implements RuleMapper
{
    public function handles(): array
    {
        return ['phone'];
    }

    public function map(
        RuleInvocation $rule,
        FieldDescription $field,
        GenerationContext $context,
    ): FieldDescription {
        return $field->schema(
            $field->schema
                ->type('string')
                ->pattern('^\+[1-9][0-9]{6,14}$'),
        );
    }
}
the field phone
{
  "type": "object",
  "required": ["phone"],
  "properties": {
    "phone": { "type": "string", "pattern": "^\\+[1-9][0-9]{6,14}$" }
  }
}
config/openapi.php
'extensions' => [
    'rule_mappers' => [App\Docs\PhoneRuleMapper::class],
],

The form request uses the rule as usual: 'phone' => 'required|phone'.

What a mapper receives

handles() lists the rule names (snake case: phone, required_if) and the rule classes (Illuminate\Validation\Rules\Enum) the mapper describes. A mapper of yours replaces the built-in mapper of the same rule.

RuleInvocation is one rule applied to one field:

PropertyHolds
fieldthe field, as written in the rules (address.city, tags.*)
namethe rule name, or the class of a rule object
parametersthe parameters: max:255 gives ['255']
rulethe rule object, for Rule::enum(), Password or your own
conditionaltrue when the rule comes from Rule::when(): it applies in some cases only

FieldDescription is the field being built, immutable:

MethodEffect
schema(Schema $schema)replaces the schema; start from $field->schema to keep the constraints of the other rules
required(bool $required = true)the field is required
conditions(string ...), addConditions(string ...)sentences written in the description: required if type is company
file(bool $file = true), contentTypes(string ...)the field is a file, of these media types: the body becomes multipart/form-data
distinct(bool $distinct = true)the items of the list are unique
forbidden(bool $forbidden = true)the field must not be sent: it is left out

Constrain only what your rule says, and let the other rules of the field give the rest: start from $field->schema, and never replace a constraint your rule does not own.

Reporting what you cannot describe

When a parameter of the rule cannot be described, keep the field as it is and add an info:

use Ark4ne\OpenApi\Context\Stage;

if ($rule->parameters === []) {
    $context->diagnostics->info(
        Stage::Request,
        "phone on {$rule->field} has no region: written as a string",
    );

    return $field->schema($field->schema->type('string'));
}