• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

api-platform / core / 20001323174

07 Dec 2025 08:10AM UTC coverage: 25.292% (+0.001%) from 25.291%
20001323174

push

github

soyuka
chore: bump metadata to 4.3.x-dev

14642 of 57891 relevant lines covered (25.29%)

28.94 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

91.53
/src/Validator/Util/ParameterValidationConstraints.php
1
<?php
2

3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <dunglas@gmail.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

12
declare(strict_types=1);
13

14
namespace ApiPlatform\Validator\Util;
15

16
use ApiPlatform\Metadata\Parameter;
17
use ApiPlatform\OpenApi\Model\Parameter as OpenApiParameter;
18
use Symfony\Component\TypeInfo\Type\CollectionType;
19
use Symfony\Component\TypeInfo\Type\UnionType;
20
use Symfony\Component\Validator\Constraint;
21
use Symfony\Component\Validator\Constraints\All;
22
use Symfony\Component\Validator\Constraints\AtLeastOneOf;
23
use Symfony\Component\Validator\Constraints\Choice;
24
use Symfony\Component\Validator\Constraints\Count;
25
use Symfony\Component\Validator\Constraints\DivisibleBy;
26
use Symfony\Component\Validator\Constraints\GreaterThan;
27
use Symfony\Component\Validator\Constraints\GreaterThanOrEqual;
28
use Symfony\Component\Validator\Constraints\Length;
29
use Symfony\Component\Validator\Constraints\LessThan;
30
use Symfony\Component\Validator\Constraints\LessThanOrEqual;
31
use Symfony\Component\Validator\Constraints\NotBlank;
32
use Symfony\Component\Validator\Constraints\NotNull;
33
use Symfony\Component\Validator\Constraints\Range;
34
use Symfony\Component\Validator\Constraints\Regex;
35
use Symfony\Component\Validator\Constraints\Sequentially;
36
use Symfony\Component\Validator\Constraints\Type;
37
use Symfony\Component\Validator\Constraints\Unique;
38

39
/**
40
 * Helper to get a set of validation constraints for a given Parameter.
41
 */
42
trait ParameterValidationConstraints
43
{
44
    /**
45
     * @param Parameter $parameter readonly
46
     *
47
     * @return list<Constraint>
48
     */
49
    public static function getParameterValidationConstraints(Parameter $parameter, ?array $schema = null, ?bool $required = null, ?OpenApiParameter $openApi = null): array
50
    {
51
        $schema ??= $parameter->getSchema();
40✔
52
        $required ??= $parameter->getRequired() ?? false;
40✔
53
        $openApi ??= $parameter->getOpenApi();
40✔
54

55
        // When it's an array of openapi parameters take the first one as it's probably just a variant of the query parameter,
56
        // only getAllowEmptyValue is used here anyways
57
        if (\is_array($openApi)) {
40✔
58
            $openApi = $openApi[0];
8✔
59
        } elseif (false === $openApi) {
34✔
60
            $openApi = null;
8✔
61
        }
62

63
        $assertions = [];
40✔
64
        $allowEmptyValue = $openApi?->getAllowEmptyValue();
40✔
65
        if (false === ($allowEmptyValue ?? $openApi?->getAllowEmptyValue())) {
40✔
66
            $assertions[] = new NotBlank(allowNull: !$required);
2✔
67
        }
68

69
        $minimum = $schema['exclusiveMinimum'] ?? $schema['minimum'] ?? null;
40✔
70
        $exclusiveMinimum = isset($schema['exclusiveMinimum']);
40✔
71
        $maximum = $schema['exclusiveMaximum'] ?? $schema['maximum'] ?? null;
40✔
72
        $exclusiveMaximum = isset($schema['exclusiveMaximum']);
40✔
73

74
        if ($minimum && $maximum) {
40✔
75
            if (!$exclusiveMinimum && !$exclusiveMaximum) {
2✔
76
                $assertions[] = new Range(min: $minimum, max: $maximum);
2✔
77
            } else {
78
                $assertions[] = $exclusiveMinimum ? new GreaterThan(value: $minimum) : new GreaterThanOrEqual(value: $minimum);
2✔
79
                $assertions[] = $exclusiveMaximum ? new LessThan(value: $maximum) : new LessThanOrEqual(value: $maximum);
2✔
80
            }
81
        } elseif ($minimum) {
40✔
82
            $assertions[] = $exclusiveMinimum ? new GreaterThan(value: $minimum) : new GreaterThanOrEqual(value: $minimum);
×
83
        } elseif ($maximum) {
40✔
84
            $assertions[] = $exclusiveMaximum ? new LessThan(value: $maximum) : new LessThanOrEqual(value: $maximum);
×
85
        }
86

87
        if (isset($schema['pattern'])) {
40✔
88
            $assertions[] = new Regex('#'.$schema['pattern'].'#');
2✔
89
        }
90

91
        if (isset($schema['maxLength']) || isset($schema['minLength'])) {
40✔
92
            $assertions[] = new Length(min: $schema['minLength'] ?? null, max: $schema['maxLength'] ?? null);
2✔
93
        }
94

95
        if (isset($schema['multipleOf'])) {
40✔
96
            $assertions[] = new DivisibleBy(value: $schema['multipleOf']);
2✔
97
        }
98

99
        if (isset($schema['enum'])) {
40✔
100
            $assertions[] = new Choice(choices: $schema['enum']);
10✔
101
        }
102

103
        $isCollectionType = fn ($t) => $t instanceof CollectionType;
40✔
104
        $isCollection = $parameter->getNativeType()?->isSatisfiedBy($isCollectionType) ?? false;
40✔
105

106
        // type-info 7.2
107
        if (!$isCollection && $parameter->getNativeType() instanceof UnionType) {
40✔
108
            foreach ($parameter->getNativeType()->getTypes() as $t) {
×
109
                if ($isCollection = $t->isSatisfiedBy($isCollectionType)) {
×
110
                    break;
×
111
                }
112
            }
113
        }
114

115
        if ($isCollection) {
40✔
116
            if (true === ($parameter->getCastToArray() ?? false)) {
18✔
117
                $assertions = $assertions ? [new All($assertions)] : [];
2✔
118
            } else {
119
                $assertions = $assertions ? [new AtLeastOneOf([new Sequentially($assertions), new All($assertions)])] : [];
18✔
120
            }
121
        }
122

123
        if ($required && false !== $allowEmptyValue) {
40✔
124
            $assertions[] = new NotNull();
4✔
125
        }
126

127
        if (isset($schema['minItems']) || isset($schema['maxItems'])) {
40✔
128
            $assertions[] = new Count(min: $schema['minItems'] ?? null, max: $schema['maxItems'] ?? null);
2✔
129
        }
130

131
        if ($schema['uniqueItems'] ?? false) {
40✔
132
            $assertions[] = new Unique();
2✔
133
        }
134

135
        if (isset($schema['type']) && 'array' === $schema['type']) {
40✔
136
            $assertions[] = new Type(type: 'array');
2✔
137
        }
138

139
        if (isset($schema['type']) && $parameter->getCastToNativeType()) {
40✔
140
            $assertion = match ($schema['type']) {
4✔
141
                'boolean', 'integer' => new Type(type: $schema['type']),
4✔
142
                'number' => new Type(type: 'float'),
2✔
143
                default => null,
2✔
144
            };
4✔
145

146
            if ($assertion) {
4✔
147
                $assertions[] = $assertion;
4✔
148
            }
149
        }
150

151
        return $assertions;
40✔
152
    }
153
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc