WordPress.org

Make WordPress Core


Ignore:
Timestamp:
10/01/2020 02:47:08 AM (16 months ago)
Author:
TimothyBlynJacobs
Message:

REST API: Support the patternProperties JSON Schema keyword.

Props yakimun.
Fixes #51024.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api.php

    r49063 r49082  
    15411541
    15421542/**
     1543 * Validates if the JSON Schema pattern matches a value.
     1544 *
     1545 * @since 5.6.0
     1546 *
     1547 * @param string $pattern The pattern to match against.
     1548 * @param string $value   The value to check.
     1549 * @return bool           True if the pattern matches the given value, false otherwise.
     1550 */
     1551function rest_validate_json_schema_pattern( $pattern, $value ) {
     1552    $escaped_pattern = str_replace( '#', '\\#', $pattern );
     1553
     1554    return 1 === preg_match( '#' . $escaped_pattern . '#u', $value );
     1555}
     1556
     1557/**
     1558 * Finds the schema for a property using the patternProperties keyword.
     1559 *
     1560 * @since 5.6.0
     1561 *
     1562 * @param string $property The property name to check.
     1563 * @param array  $args     The schema array to use.
     1564 * @return array|null      The schema of matching pattern property, or null if no patterns match.
     1565 */
     1566function rest_find_matching_pattern_property_schema( $property, $args ) {
     1567    if ( isset( $args['patternProperties'] ) ) {
     1568        foreach ( $args['patternProperties'] as $pattern => $child_schema ) {
     1569            if ( rest_validate_json_schema_pattern( $pattern, $property ) ) {
     1570                return $child_schema;
     1571            }
     1572        }
     1573    }
     1574
     1575    return null;
     1576}
     1577
     1578/**
    15431579 * Validate a value based on a schema.
    15441580 *
     
    15541590 * @since 5.6.0 Support the "minProperties" and "maxProperties" keywords for objects.
    15551591 *              Support the "multipleOf" keyword for numbers and integers.
     1592 *              Support the "patternProperties" keyword for objects.
    15561593 *
    15571594 * @param mixed  $value The value to validate.
     
    16511688                    return $is_valid;
    16521689                }
    1653             } elseif ( isset( $args['additionalProperties'] ) ) {
     1690                continue;
     1691            }
     1692
     1693            $pattern_property_schema = rest_find_matching_pattern_property_schema( $property, $args );
     1694            if ( null !== $pattern_property_schema ) {
     1695                $is_valid = rest_validate_value_from_schema( $v, $pattern_property_schema, $param . '[' . $property . ']' );
     1696                if ( is_wp_error( $is_valid ) ) {
     1697                    return $is_valid;
     1698                }
     1699                continue;
     1700            }
     1701
     1702            if ( isset( $args['additionalProperties'] ) ) {
    16541703                if ( false === $args['additionalProperties'] ) {
    16551704                    /* translators: %s: Property of an object. */
     
    17451794        }
    17461795
    1747         if ( isset( $args['pattern'] ) ) {
    1748             $pattern = str_replace( '#', '\\#', $args['pattern'] );
    1749             if ( ! preg_match( '#' . $pattern . '#u', $value ) ) {
    1750                 /* translators: 1: Parameter, 2: Pattern. */
    1751                 return new WP_Error( 'rest_invalid_pattern', sprintf( __( '%1$s does not match pattern %2$s.' ), $param, $args['pattern'] ) );
    1752             }
     1796        if ( isset( $args['pattern'] ) && ! rest_validate_json_schema_pattern( $args['pattern'], $value ) ) {
     1797            /* translators: 1: Parameter, 2: Pattern. */
     1798            return new WP_Error( 'rest_invalid_pattern', sprintf( __( '%1$s does not match pattern %2$s.' ), $param, $args['pattern'] ) );
    17531799        }
    17541800    }
     
    18981944            if ( isset( $args['properties'][ $property ] ) ) {
    18991945                $value[ $property ] = rest_sanitize_value_from_schema( $v, $args['properties'][ $property ], $param . '[' . $property . ']' );
    1900             } elseif ( isset( $args['additionalProperties'] ) ) {
     1946                continue;
     1947            }
     1948
     1949            $pattern_property_schema = rest_find_matching_pattern_property_schema( $property, $args );
     1950            if ( null !== $pattern_property_schema ) {
     1951                $value[ $property ] = rest_sanitize_value_from_schema( $v, $pattern_property_schema, $param . '[' . $property . ']' );
     1952                continue;
     1953            }
     1954
     1955            if ( isset( $args['additionalProperties'] ) ) {
    19011956                if ( false === $args['additionalProperties'] ) {
    19021957                    unset( $value[ $property ] );
     
    20542109 *
    20552110 * @since 5.5.0
     2111 * @since 5.6.0 Support the "patternProperties" keyword for objects.
    20562112 *
    20572113 * @param array|object $data    The response data to modify.
     
    20942150            if ( isset( $schema['properties'][ $key ] ) ) {
    20952151                $check = $schema['properties'][ $key ];
    2096             } elseif ( $has_additional_properties ) {
    2097                 $check = $schema['additionalProperties'];
     2152            } else {
     2153                $pattern_property_schema = rest_find_matching_pattern_property_schema( $key, $schema );
     2154                if ( null !== $pattern_property_schema ) {
     2155                    $check = $pattern_property_schema;
     2156                } elseif ( $has_additional_properties ) {
     2157                    $check = $schema['additionalProperties'];
     2158                }
    20982159            }
    20992160        }
     
    21332194 *
    21342195 * @since 5.5.0
     2196 * @since 5.6.0 Support the "patternProperties" keyword.
    21352197 *
    21362198 * @param array $schema The schema to modify.
     
    21442206            foreach ( $schema['properties'] as $key => $child_schema ) {
    21452207                $schema['properties'][ $key ] = rest_default_additional_properties_to_false( $child_schema );
     2208            }
     2209        }
     2210
     2211        if ( isset( $schema['patternProperties'] ) ) {
     2212            foreach ( $schema['patternProperties'] as $key => $child_schema ) {
     2213                $schema['patternProperties'][ $key ] = rest_default_additional_properties_to_false( $child_schema );
    21462214            }
    21472215        }
     
    23012369        'properties',
    23022370        'additionalProperties',
     2371        'patternProperties',
    23032372        'minProperties',
    23042373        'maxProperties',
Note: See TracChangeset for help on using the changeset viewer.