Skip to content

Commit 8e0a8ea

Browse files
authored
docs(core): use ScalarHeaderParameter for header validation (#2359)
1 parent 8469a6c commit 8e0a8ea

1 file changed

Lines changed: 6 additions & 8 deletions

File tree

‎core/filters.md‎

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -592,11 +592,9 @@ Symfony Validator constraints.
592592
// api/src/Resource/User.php
593593
use ApiPlatform\Metadata\ApiResource;
594594
use ApiPlatform\Metadata\GetCollection;
595-
use ApiPlatform\Metadata\HeaderParameter;
596595
use ApiPlatform\Metadata\QueryParameter;
596+
use ApiPlatform\Metadata\ScalarHeaderParameter;
597597
use Symfony\Component\Validator\Constraints as Assert;
598-
use Symfony\Component\TypeInfo\Type\BuiltinType;
599-
use Symfony\Component\TypeInfo\TypeIdentifier;
600598
601599
#[ApiResource(operations: [
602600
new GetCollection(
@@ -606,11 +604,10 @@ use Symfony\Component\TypeInfo\TypeIdentifier;
606604
description: 'Filter by country code.',
607605
constraints: [new Assert\Country()]
608606
),
609-
'X-Request-ID' => new HeaderParameter(
607+
'X-Request-ID' => new ScalarHeaderParameter(
610608
description: 'A unique request identifier.',
611609
required: true,
612-
constraints: [new Assert\Uuid()],
613-
nativeType: new BuiltinType(typeIdentifier: TypeIdentifier::STRING)
610+
constraints: [new Assert\Uuid()]
614611
)
615612
]
616613
)
@@ -621,8 +618,9 @@ class User {}
621618
> [!NOTE] When `castToNativeType` is enabled, API Platform infers type validation from the JSON
622619
> Schema.
623620

624-
If `constraints` are used then a valid type needs to be passed using `nativeType` named argument.
625-
Otherwise, the values will be passed as an array to each constraints.
621+
A `HeaderParameter` value is typed `string|list<string>` because a header can be repeated, so
622+
constraints receive an array. Use `ScalarHeaderParameter` (available since API Platform 5.1) to
623+
validate a single string value, or pass a `schema` or a `nativeType` yourself.
626624

627625
The `ApiPlatform\Validator\Util\ParameterValidationConstraints` trait can be used to automatically
628626
infer validation constraints from the JSON Schema and OpenAPI definitions of a parameter.

0 commit comments

Comments
 (0)