Changeset 49082 for trunk/src/wp-includes/rest-api.php
- Timestamp:
- 10/01/2020 02:47:08 AM (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/rest-api.php
r49063 r49082 1541 1541 1542 1542 /** 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 */ 1551 function 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 */ 1566 function 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 /** 1543 1579 * Validate a value based on a schema. 1544 1580 * … … 1554 1590 * @since 5.6.0 Support the "minProperties" and "maxProperties" keywords for objects. 1555 1591 * Support the "multipleOf" keyword for numbers and integers. 1592 * Support the "patternProperties" keyword for objects. 1556 1593 * 1557 1594 * @param mixed $value The value to validate. … … 1651 1688 return $is_valid; 1652 1689 } 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'] ) ) { 1654 1703 if ( false === $args['additionalProperties'] ) { 1655 1704 /* translators: %s: Property of an object. */ … … 1745 1794 } 1746 1795 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'] ) ); 1753 1799 } 1754 1800 } … … 1898 1944 if ( isset( $args['properties'][ $property ] ) ) { 1899 1945 $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'] ) ) { 1901 1956 if ( false === $args['additionalProperties'] ) { 1902 1957 unset( $value[ $property ] ); … … 2054 2109 * 2055 2110 * @since 5.5.0 2111 * @since 5.6.0 Support the "patternProperties" keyword for objects. 2056 2112 * 2057 2113 * @param array|object $data The response data to modify. … … 2094 2150 if ( isset( $schema['properties'][ $key ] ) ) { 2095 2151 $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 } 2098 2159 } 2099 2160 } … … 2133 2194 * 2134 2195 * @since 5.5.0 2196 * @since 5.6.0 Support the "patternProperties" keyword. 2135 2197 * 2136 2198 * @param array $schema The schema to modify. … … 2144 2206 foreach ( $schema['properties'] as $key => $child_schema ) { 2145 2207 $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 ); 2146 2214 } 2147 2215 } … … 2301 2369 'properties', 2302 2370 'additionalProperties', 2371 'patternProperties', 2303 2372 'minProperties', 2304 2373 'maxProperties',
Note: See TracChangeset
for help on using the changeset viewer.