Skip to content

Commit 5d57b68

Browse files
authored
Fix to let schema attribute enum type hint allow null values (#271)
1 parent ea38ef7 commit 5d57b68

3 files changed

Lines changed: 27 additions & 24 deletions

File tree

src/Capability/Attribute/Schema.php

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
* definition?: array<string, mixed>,
2525
* type?: string,
2626
* description?: string,
27-
* enum?: array<int, float|string|int>,
28-
* gormat?: string,
27+
* enum?: array<int, int|float|string|null>,
28+
* format?: string,
2929
* minLength?: int,
3030
* maxLength?: int,
3131
* pattern?: string,
@@ -64,7 +64,7 @@ class Schema
6464
public ?string $description = null;
6565
public mixed $default = null;
6666
/**
67-
* @var ?array<int, float|string|int>
67+
* @var ?array<int, int|float|string|null>
6868
*/
6969
public ?array $enum = null; // list of allowed values
7070
public ?string $format = null; // e.g., 'email', 'date-time'
@@ -105,26 +105,26 @@ class Schema
105105
public bool|array|null $additionalProperties = null; // true, false, or a schema array
106106

107107
/**
108-
* @param ?array<string, mixed> $definition A complete JSON schema array. If provided, other parameters are ignored.
109-
* @param ?string $type the JSON schema type
110-
* @param ?string $description description of the element
111-
* @param ?array<int, float|string|int> $enum allowed enum values
112-
* @param ?string $format String format (e.g., 'date-time', 'email').
113-
* @param ?int $minLength minimum length for strings
114-
* @param ?int $maxLength maximum length for strings
115-
* @param ?string $pattern regex pattern for strings
116-
* @param int|float|null $minimum minimum value for numbers/integers
117-
* @param int|float|null $maximum maximum value for numbers/integers
118-
* @param ?bool $exclusiveMinimum exclusive minimum
119-
* @param ?bool $exclusiveMaximum exclusive maximum
120-
* @param int|float|null $multipleOf must be a multiple of this value
121-
* @param ?array<string, mixed> $items JSON Schema for items if type is 'array'
122-
* @param ?int $minItems minimum items for an array
123-
* @param ?int $maxItems maximum items for an array
124-
* @param ?bool $uniqueItems whether array items must be unique
125-
* @param ?array<string, mixed> $properties Property definitions if type is 'object'. [name => schema_array].
126-
* @param ?array<int, string> $required list of required properties for an object
127-
* @param bool|array<string, mixed>|null $additionalProperties policy for additional properties in an object
108+
* @param ?array<string, mixed> $definition A complete JSON schema array. If provided, other parameters are ignored.
109+
* @param ?string $type the JSON schema type
110+
* @param ?string $description description of the element
111+
* @param ?array<int, int|float|string|null> $enum allowed enum values
112+
* @param ?string $format String format (e.g., 'date-time', 'email').
113+
* @param ?int $minLength minimum length for strings
114+
* @param ?int $maxLength maximum length for strings
115+
* @param ?string $pattern regex pattern for strings
116+
* @param int|float|null $minimum minimum value for numbers/integers
117+
* @param int|float|null $maximum maximum value for numbers/integers
118+
* @param ?bool $exclusiveMinimum exclusive minimum
119+
* @param ?bool $exclusiveMaximum exclusive maximum
120+
* @param int|float|null $multipleOf must be a multiple of this value
121+
* @param ?array<string, mixed> $items JSON Schema for items if type is 'array'
122+
* @param ?int $minItems minimum items for an array
123+
* @param ?int $maxItems maximum items for an array
124+
* @param ?bool $uniqueItems whether array items must be unique
125+
* @param ?array<string, mixed> $properties Property definitions if type is 'object'. [name => schema_array].
126+
* @param ?array<int, string> $required list of required properties for an object
127+
* @param bool|array<string, mixed>|null $additionalProperties policy for additional properties in an object
128128
*/
129129
public function __construct(
130130
?array $definition = null,

src/Capability/Discovery/SchemaGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
* type?: string|array,
4747
* description?: string,
4848
* default?: mixed,
49-
* enum?: array<string|int>,
49+
* enum?: array<int, int|float|string|null>,
5050
* items?: array<string, mixed>,
5151
* }
5252
* @phpstan-type VariadicParameterSchema array{

tests/Unit/Capability/Discovery/SchemaValidatorTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,7 @@ private function getSimpleSchema(): array
471471
'active' => ['type' => 'boolean'],
472472
'score' => ['type' => 'number'],
473473
'items' => ['type' => 'array', 'items' => ['type' => 'string']],
474+
'status' => ['enum' => [null, 'active', 'inactive']],
474475
'nullableValue' => ['type' => ['string', 'null']],
475476
'optionalValue' => ['type' => 'string'],
476477
],
@@ -486,6 +487,7 @@ private function getSimpleSchema(): array
486487
* active: bool,
487488
* score: float,
488489
* items: string[],
490+
* status: 'active'|'inactive'|null,
489491
* nullableValue: null,
490492
* optionalValue: string
491493
* }
@@ -498,6 +500,7 @@ private function getValidData(): array
498500
'active' => true,
499501
'score' => 99.5,
500502
'items' => ['a', 'b'],
503+
'status' => null,
501504
'nullableValue' => null,
502505
'optionalValue' => 'present',
503506
];

0 commit comments

Comments
 (0)