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

api-platform / core / 20847864477

09 Jan 2026 09:47AM UTC coverage: 29.1% (+0.005%) from 29.095%
20847864477

Pull #7649

github

web-flow
Merge b342dd5db into d640d106b
Pull Request #7649: feat(validator): uuid/ulid parameter validation

0 of 4 new or added lines in 1 file covered. (0.0%)

15050 existing lines in 491 files now uncovered.

16996 of 58406 relevant lines covered (29.1%)

81.8 hits per line

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

95.24
/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\Ulid;
38
use Symfony\Component\Validator\Constraints\Unique;
39
use Symfony\Component\Validator\Constraints\Uuid;
40

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

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

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

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

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

UNCOV
89
        if (isset($schema['pattern'])) {
72✔
UNCOV
90
            $assertions[] = new Regex('#'.$schema['pattern'].'#');
6✔
91
        }
92

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

UNCOV
97
        if (isset($schema['multipleOf'])) {
72✔
UNCOV
98
            $assertions[] = new DivisibleBy(value: $schema['multipleOf']);
6✔
99
        }
100

UNCOV
101
        if (isset($schema['enum'])) {
72✔
UNCOV
102
            $assertions[] = new Choice(choices: $schema['enum']);
18✔
103
        }
104

UNCOV
105
        $isCollectionType = fn ($t) => $t instanceof CollectionType;
72✔
UNCOV
106
        $isCollection = $parameter->getNativeType()?->isSatisfiedBy($isCollectionType) ?? false;
72✔
107

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

UNCOV
117
        if ($isCollection) {
72✔
UNCOV
118
            if (true === ($parameter->getCastToArray() ?? false)) {
30✔
UNCOV
119
                $assertions = $assertions ? [new All($assertions)] : [];
4✔
120
            } else {
UNCOV
121
                $assertions = $assertions ? [new AtLeastOneOf([new Sequentially($assertions), new All($assertions)])] : [];
30✔
122
            }
123
        }
124

UNCOV
125
        if ($required && false !== $allowEmptyValue) {
72✔
UNCOV
126
            $assertions[] = new NotNull();
8✔
127
        }
128

UNCOV
129
        if (isset($schema['minItems']) || isset($schema['maxItems'])) {
72✔
UNCOV
130
            $assertions[] = new Count(min: $schema['minItems'] ?? null, max: $schema['maxItems'] ?? null);
6✔
131
        }
132

UNCOV
133
        if ($schema['uniqueItems'] ?? false) {
72✔
UNCOV
134
            $assertions[] = new Unique();
6✔
135
        }
136

UNCOV
137
        if (isset($schema['type']) && 'array' === $schema['type']) {
72✔
UNCOV
138
            $assertions[] = new Type(type: 'array');
6✔
139
        }
140

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

UNCOV
148
            if ($assertion) {
6✔
UNCOV
149
                $assertions[] = $assertion;
6✔
150
            }
151
        }
152

NEW
153
        if (isset($schema['type'], $schema['format']) && 'string' === $schema['type'] && 'uuid' === $schema['format']) {
72✔
NEW
154
            $assertions[] = new Uuid();
6✔
155
        }
156

NEW
157
        if (isset($schema['type'], $schema['format']) && 'string' === $schema['type'] && 'ulid' === $schema['format']) {
72✔
NEW
158
            $assertions[] = new Ulid();
4✔
159
        }
160

UNCOV
161
        return $assertions;
72✔
162
    }
163
}
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