Skip to content

REST API: Validate object properties against all matching patternProperties schemas - #16328

Open
robelsust wants to merge 1 commit into
WordPress:trunkfrom
robelsust:fix/66871-rest-validate-all-pattern-properties
Open

robelsust wants to merge 1 commit into
WordPress:trunkfrom
robelsust:fix/66871-rest-validate-all-pattern-properties

Conversation

@robelsust

Copy link
Copy Markdown

Trac ticket: https://core.trac.wordpress.org/ticket/66871

Description

rest_validate_object_value_from_schema() previously called rest_find_matching_pattern_property_schema(), which returns only the first matching patternProperties schema. As a result, a property matching multiple patterns could pass validation even when it violated a subsequent matching pattern's constraints, making validation order-dependent and inconsistent with JSON Schema draft 4.

Changes

  • In rest_validate_object_value_from_schema() (src/wp-includes/rest-api.php), iterate over all entries in $args['patternProperties'] and validate the property value against every matching pattern's schema.
  • Preserve rest_find_matching_pattern_property_schema()'s public signature and array|null return contract for backwards compatibility with existing callers (rest_sanitize_value_from_schema(), rest_filter_response_by_context(), and third-party code).
  • Preserve existing properties precedence ([49082] / #51024) and additionalProperties fallback when no pattern matches.
  • Add unit tests in tests/phpunit/tests/rest-api/rest-schema-validation.php covering multiple matching patterns, pattern-order independence, conflicting type constraints, single-pattern matches, and additionalProperties: false fallback.

Testing

  • Ran vendor/bin/phpcs on both modified files.
  • Ran single-site PHPUnit on WP_Test_REST_Schema_Validation and WP_Test_REST_Schema_Sanitization (258 tests, 454 assertions) and multisite PHPUnit on WP_Test_REST_Schema_Validation (217 tests, 339 assertions).

…erties schemas.

Previously, `rest_validate_object_value_from_schema()` called
`rest_find_matching_pattern_property_schema()`, which returns only the first
matching pattern's schema. As a result, a property matching multiple
`patternProperties` patterns could pass validation even when failing a later
pattern's constraints, making validation order-dependent and inconsistent with
JSON Schema draft 4.

Iterate through all `patternProperties` in `rest_validate_object_value_from_schema()`
and validate the property value against every matching pattern's schema, while
leaving `rest_find_matching_pattern_property_schema()`'s public signature and
return contract unchanged for backwards compatibility.

Trac ticket: https://core.trac.wordpress.org/ticket/66871
@github-actions

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs.

Core Committers: Use this line as a base for the props when committing in SVN:

Props therssoftware.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.


Props Bot commands

You can trigger Props Bot actions by commenting on this PR:

  • @propsbot add <username> to give props to a GitHub or WordPress.org user. Requires write access.
  • @propsbot remove <username> to leave a user out of the props. Requires write access.
  • @propsbot refresh to refresh this list. Adding the props-bot label works too.

@github-actions

Copy link
Copy Markdown

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant