Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions src/wp-includes/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -2526,6 +2526,7 @@ function rest_validate_boolean_value_from_schema( $value, $param ) {
* Validates an object value based on a schema.
*
* @since 5.7.0
* @since 7.3.0 Validate against all matching `patternProperties` schemas.
*
* @param mixed $value The value to validate.
* @param array $args Schema array to use for validation.
Expand Down Expand Up @@ -2575,12 +2576,20 @@ function rest_validate_object_value_from_schema( $value, $args, $param ) {
continue;
}

$pattern_property_schema = rest_find_matching_pattern_property_schema( $property, $args );
if ( null !== $pattern_property_schema ) {
$is_valid = rest_validate_value_from_schema( $v, $pattern_property_schema, $param . '[' . $property . ']' );
if ( is_wp_error( $is_valid ) ) {
return $is_valid;
$matched_pattern_property = false;
if ( isset( $args['patternProperties'] ) ) {
foreach ( $args['patternProperties'] as $pattern => $child_schema ) {
if ( rest_validate_json_schema_pattern( $pattern, $property ) ) {
$matched_pattern_property = true;
$is_valid = rest_validate_value_from_schema( $v, $child_schema, $param . '[' . $property . ']' );
if ( is_wp_error( $is_valid ) ) {
return $is_valid;
}
}
}
}

if ( $matched_pattern_property ) {
continue;
}

Expand Down
96 changes: 96 additions & 0 deletions tests/phpunit/tests/rest-api/rest-schema-validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -910,6 +910,102 @@ public static function data_type_object_pattern_properties() {
);
}

/**
* Tests that object validation checks all matching `patternProperties` schemas regardless of order.
*
* @ticket 66871
*
* @dataProvider data_type_object_multiple_matching_pattern_properties
*
* @param array $pattern_properties The `patternProperties` schema map.
* @param array $value The object value to validate.
* @param bool $expected Whether validation should succeed.
* @param string $message Assertion failure message.
*/
public function test_type_object_multiple_matching_pattern_properties( $pattern_properties, $value, $expected, $message ) {
$schema = array(
'type' => 'object',
'patternProperties' => $pattern_properties,
'additionalProperties' => false,
);

$result = rest_validate_value_from_schema( $value, $schema );

if ( $expected ) {
$this->assertTrue( $result, $message );
} else {
$this->assertWPError( $result, $message );
}
}

/**
* Data provider for test_type_object_multiple_matching_pattern_properties().
*
* @return array[]
*/
public static function data_type_object_multiple_matching_pattern_properties() {
$min_then_max = array(
'^a' => array(
'type' => 'string',
'minLength' => 3,
),
'b$' => array(
'type' => 'string',
'maxLength' => 5,
),
);

$max_then_min = array_reverse( $min_then_max, true );

return array(
'satisfies both matching patterns' => array(
$min_then_max,
array( 'axb' => 'abcd' ),
true,
'Value satisfying all matching patternProperties schemas should pass.',
),
'fails second matching pattern (min then max)' => array(
$min_then_max,
array( 'axb' => 'abcdef' ),
false,
'Value failing the second matching patternProperties schema should fail.',
),
'fails second matching pattern (max then min)' => array(
$max_then_min,
array( 'axb' => 'ab' ),
false,
'Value failing the second matching patternProperties schema after reversing order should fail.',
),
'fails first matching pattern (max then min)' => array(
$max_then_min,
array( 'axb' => 'abcdef' ),
false,
'Value failing the first matching patternProperties schema should fail.',
),
'conflicting type constraints on same key' => array(
array(
'^x' => array( 'type' => 'string' ),
'y$' => array( 'type' => 'integer' ),
),
array( 'xy' => 'hello' ),
false,
'Value cannot satisfy both string and integer matching patternProperties schemas.',
),
'matches only first pattern and passes' => array(
$min_then_max,
array( 'axc' => 'abcdef' ),
true,
'Property matching only the first pattern should not be checked against non-matching patterns.',
),
'matches no pattern with additionalProperties false' => array(
$min_then_max,
array( 'other' => 'abcd' ),
false,
'Property matching no pattern should still be rejected when additionalProperties is false.',
),
);
}

public function test_type_object_additional_properties_false() {
$schema = array(
'type' => 'object',
Expand Down
Loading