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

api-platform / core / 16050929464

03 Jul 2025 12:51PM UTC coverage: 22.065% (+0.2%) from 21.821%
16050929464

push

github

soyuka
chore: todo improvement

11516 of 52192 relevant lines covered (22.06%)

22.08 hits per line

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

91.07
/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\Collection;
25
use Symfony\Component\Validator\Constraints\Count;
26
use Symfony\Component\Validator\Constraints\DivisibleBy;
27
use Symfony\Component\Validator\Constraints\GreaterThan;
28
use Symfony\Component\Validator\Constraints\GreaterThanOrEqual;
29
use Symfony\Component\Validator\Constraints\Length;
30
use Symfony\Component\Validator\Constraints\LessThan;
31
use Symfony\Component\Validator\Constraints\LessThanOrEqual;
32
use Symfony\Component\Validator\Constraints\NotBlank;
33
use Symfony\Component\Validator\Constraints\NotNull;
34
use Symfony\Component\Validator\Constraints\Range;
35
use Symfony\Component\Validator\Constraints\Regex;
36
use Symfony\Component\Validator\Constraints\Sequentially;
37
use Symfony\Component\Validator\Constraints\Type;
38
use Symfony\Component\Validator\Constraints\Unique;
39

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

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

66
        $assertions = [];
32✔
67
        $allowEmptyValue = $openApi?->getAllowEmptyValue();
32✔
68
        if (false === ($allowEmptyValue ?? $openApi?->getAllowEmptyValue())) {
32✔
69
            $assertions[] = new NotBlank(allowNull: !$required);
2✔
70
        }
71

72
        $minimum = $schema['exclusiveMinimum'] ?? $schema['minimum'] ?? null;
32✔
73
        $exclusiveMinimum = isset($schema['exclusiveMinimum']);
32✔
74
        $maximum = $schema['exclusiveMaximum'] ?? $schema['maximum'] ?? null;
32✔
75
        $exclusiveMaximum = isset($schema['exclusiveMaximum']);
32✔
76

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

90
        if (isset($schema['pattern'])) {
32✔
91
            $assertions[] = new Regex('#'.$schema['pattern'].'#');
2✔
92
        }
93

94
        if (isset($schema['maxLength']) || isset($schema['minLength'])) {
32✔
95
            $assertions[] = new Length(min: $schema['minLength'] ?? null, max: $schema['maxLength'] ?? null);
2✔
96
        }
97

98
        if (isset($schema['multipleOf'])) {
32✔
99
            $assertions[] = new DivisibleBy(value: $schema['multipleOf']);
2✔
100
        }
101

102
        if (isset($schema['enum'])) {
32✔
103
            $assertions[] = new Choice(choices: $schema['enum']);
10✔
104
        }
105

106
        if ($properties = $parameter->getExtraProperties()['_properties'] ?? []) {
32✔
107
            $fields = [];
8✔
108
            foreach ($properties as $propertyName) {
8✔
109
                $fields[$propertyName] = $assertions;
8✔
110
            }
111

112
            return [new Collection(fields: $fields, allowMissingFields: true)];
8✔
113
        }
114

115
        $isCollectionType = fn ($t) => $t instanceof CollectionType;
30✔
116
        $isCollection = $parameter->getNativeType()?->isSatisfiedBy($isCollectionType) ?? false;
30✔
117

118
        // type-info 7.2
119
        if (!$isCollection && $parameter->getNativeType() instanceof UnionType) {
30✔
120
            foreach ($parameter->getNativeType()->getTypes() as $t) {
×
121
                if ($isCollection = $t->isSatisfiedBy($isCollectionType)) {
×
122
                    break;
×
123
                }
124
            }
125
        }
126

127
        if ($isCollection) {
30✔
128
            if (true === ($parameter->getCastToArray() ?? false)) {
12✔
129
                $assertions = $assertions ? [new All($assertions)] : [];
2✔
130
            } else {
131
                $assertions = $assertions ? [new AtLeastOneOf([new Sequentially($assertions), new All($assertions)])] : [];
12✔
132
            }
133
        }
134

135
        if ($required && false !== $allowEmptyValue) {
30✔
136
            $assertions[] = new NotNull(message: \sprintf('The parameter "%s" is required.', $parameter->getKey()));
2✔
137
        }
138

139
        if (isset($schema['minItems']) || isset($schema['maxItems'])) {
30✔
140
            $assertions[] = new Count(min: $schema['minItems'] ?? null, max: $schema['maxItems'] ?? null);
2✔
141
        }
142

143
        if ($schema['uniqueItems'] ?? false) {
30✔
144
            $assertions[] = new Unique();
2✔
145
        }
146

147
        if (isset($schema['type']) && 'array' === $schema['type']) {
30✔
148
            $assertions[] = new Type(type: 'array');
2✔
149
        }
150

151
        return $assertions;
30✔
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