Skip to content

Commit 079a030

Browse files
committed
Make discardAdditionalProperties an object-level option
1 parent aacdaf3 commit 079a030

File tree

7 files changed

+119
-1498
lines changed

7 files changed

+119
-1498
lines changed

src/Codegen/Codegen.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ final class Codegen implements IBuilder {
5858
?'root_schema' => TSchema,
5959
?'root_schema_path' => string,
6060
),
61-
?'discard_additional_properties' => bool,
6261
);
6362

6463
const type TSanitizeStringConfig = shape(

src/Codegen/Constraints/Context.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,4 @@ public function acknowledgeRef(string $ref): void {
130130
public function hasSeenRef(string $ref): bool {
131131
return C\contains($this->seen_refs, $ref);
132132
}
133-
134-
public function shouldDiscardAdditionalProperties(): bool {
135-
return $this->validatorConfig['discard_additional_properties'] ?? false;
136-
}
137133
}

src/Codegen/Constraints/ObjectBuilder.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
?'coerce' => bool,
2323
?'minProperties' => int,
2424
?'maxProperties' => int,
25+
?'discardAdditionalProperties' => bool,
2526
...
2627
);
2728

@@ -143,7 +144,7 @@ protected function getCheckMethodCode(
143144
$allow_any_additional_properties =
144145
$additional_properties is nonnull && $is_additional_properties_boolean && $additional_properties;
145146
$discard_additional_properties =
146-
$additional_properties === false && $this->ctx->shouldDiscardAdditionalProperties();
147+
$additional_properties === false && Shapes::idx($this->typed_schema, 'discardAdditionalProperties', false);
147148

148149
$discard_all = $discard_additional_properties &&
149150
($properties is null || C\count($properties) == 0) &&
@@ -328,9 +329,8 @@ protected function getCheckMethodCode(
328329
// `properties` key will be run through that JSON schema. These values will
329330
// also be included in the output and will be typed to the specific schema.
330331
//
331-
// If the 'discard_aditional_properties' validator configuration is set to
332-
// true, we will just ignore and discard the additional values instead of
333-
// throwing a validation error.
332+
// If 'discard_aditional_properties' is set to true, we will just ignore
333+
// and discard the additional values instead of throwing a validation error.
334334
//
335335

336336
if (

tests/BaseCodegenTestCase.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ abstract class BaseCodegenTestCase extends HackTest {
1818
?'json_schema_codegen_config' => IJsonSchemaCodegenConfig,
1919
?'refs' => Codegen::TValidatorRefsConfig,
2020
?'defaults' => Codegen::TValidatorDefaultsConfig,
21-
?'discard_additional_properties' => bool,
2221
);
2322

2423
public function assertUnchanged(string $_value, ?string $_token = null): void {
@@ -100,10 +99,6 @@ private static function getConfig(string $name, this::TOptions $options): Codege
10099
'class' => $name,
101100
);
102101

103-
if (Shapes::keyExists($options, 'discard_additional_properties')) {
104-
$validator_config['discard_additional_properties'] = $options['discard_additional_properties'];
105-
}
106-
107102
$defaults = $options['defaults'] ?? null;
108103
if ($defaults is nonnull) {
109104
$validator_config['defaults'] = $defaults;

tests/DiscardAddititionalPropertiesValidatorTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@ final class DiscardAddititionalPropertiesValidatorTest extends BaseCodegenTestCa
1111
<<__Override>>
1212
public static async function beforeFirstTestAsync(): Awaitable<void> {
1313
$ret = self::getBuilder(
14-
'object-schema.json',
14+
'discard-additional-properties-schema.json',
1515
'DiscardAddititionalPropertiesValidator',
16-
shape('discard_additional_properties' => true),
1716
);
1817
$ret['codegen']->build();
1918
require_once($ret['path']);

0 commit comments

Comments
 (0)