Make WordPress Core

Changeset 50060


Ignore:
Timestamp:
01/28/2021 05:59:17 PM (6 years ago)
Author:
TimothyBlynJacobs
Message:

REST API: Refactor rest_validate_value_from_schema into separate validation functions per-type.

Props yakimun.
Fixes #52375.

File:
1 edited

Legend:

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

    r50010 r50060  
    20552055        }
    20562056
    2057         if ( 'array' === $args['type'] ) {
    2058                 if ( ! rest_is_array( $value ) ) {
    2059                         return new WP_Error(
    2060                                 'rest_invalid_type',
    2061                                 /* translators: 1: Parameter, 2: Type name. */
    2062                                 sprintf( __( '%1$s is not of type %2$s.' ), $param, 'array' ),
    2063                                 array( 'param' => $param )
    2064                         );
    2065                 }
    2066 
    2067                 $value = rest_sanitize_array( $value );
    2068 
    2069                 if ( isset( $args['items'] ) ) {
    2070                         foreach ( $value as $index => $v ) {
    2071                                 $is_valid = rest_validate_value_from_schema( $v, $args['items'], $param . '[' . $index . ']' );
    2072                                 if ( is_wp_error( $is_valid ) ) {
    2073                                         return $is_valid;
    2074                                 }
    2075                         }
    2076                 }
    2077 
    2078                 if ( isset( $args['minItems'] ) && count( $value ) < $args['minItems'] ) {
    2079                         return new WP_Error(
    2080                                 'rest_too_few_items',
    2081                                 sprintf(
    2082                                         /* translators: 1: Parameter, 2: Number. */
    2083                                         _n(
    2084                                                 '%1$s must contain at least %2$s item.',
    2085                                                 '%1$s must contain at least %2$s items.',
    2086                                                 $args['minItems']
    2087                                         ),
    2088                                         $param,
    2089                                         number_format_i18n( $args['minItems'] )
    2090                                 )
    2091                         );
    2092                 }
    2093 
    2094                 if ( isset( $args['maxItems'] ) && count( $value ) > $args['maxItems'] ) {
    2095                         return new WP_Error(
    2096                                 'test_too_many_items',
    2097                                 sprintf(
    2098                                         /* translators: 1: Parameter, 2: Number. */
    2099                                         _n(
    2100                                                 '%1$s must contain at most %2$s item.',
    2101                                                 '%1$s must contain at most %2$s items.',
    2102                                                 $args['maxItems']
    2103                                         ),
    2104                                         $param,
    2105                                         number_format_i18n( $args['maxItems'] )
    2106                                 )
    2107                         );
    2108                 }
    2109 
    2110                 if ( ! empty( $args['uniqueItems'] ) && ! rest_validate_array_contains_unique_items( $value ) ) {
    2111                         /* translators: 1: Parameter. */
    2112                         return new WP_Error( 'rest_duplicate_items', sprintf( __( '%1$s has duplicate items.' ), $param ) );
    2113                 }
    2114         }
    2115 
    2116         if ( 'object' === $args['type'] ) {
    2117                 if ( ! rest_is_object( $value ) ) {
    2118                         return new WP_Error(
    2119                                 'rest_invalid_type',
    2120                                 /* translators: 1: Parameter, 2: Type name. */
    2121                                 sprintf( __( '%1$s is not of type %2$s.' ), $param, 'object' ),
    2122                                 array( 'param' => $param )
    2123                         );
    2124                 }
    2125 
    2126                 $value = rest_sanitize_object( $value );
    2127 
    2128                 if ( isset( $args['required'] ) && is_array( $args['required'] ) ) { // schema version 4
    2129                         foreach ( $args['required'] as $name ) {
    2130                                 if ( ! array_key_exists( $name, $value ) ) {
    2131                                         /* translators: 1: Property of an object, 2: Parameter. */
    2132                                         return new WP_Error( 'rest_property_required', sprintf( __( '%1$s is a required property of %2$s.' ), $name, $param ) );
    2133                                 }
    2134                         }
    2135                 } elseif ( isset( $args['properties'] ) ) { // schema version 3
    2136                         foreach ( $args['properties'] as $name => $property ) {
    2137                                 if ( isset( $property['required'] ) && true === $property['required'] && ! array_key_exists( $name, $value ) ) {
    2138                                         /* translators: 1: Property of an object, 2: Parameter. */
    2139                                         return new WP_Error( 'rest_property_required', sprintf( __( '%1$s is a required property of %2$s.' ), $name, $param ) );
    2140                                 }
    2141                         }
    2142                 }
    2143 
    2144                 foreach ( $value as $property => $v ) {
    2145                         if ( isset( $args['properties'][ $property ] ) ) {
    2146                                 $is_valid = rest_validate_value_from_schema( $v, $args['properties'][ $property ], $param . '[' . $property . ']' );
    2147                                 if ( is_wp_error( $is_valid ) ) {
    2148                                         return $is_valid;
    2149                                 }
    2150                                 continue;
    2151                         }
    2152 
    2153                         $pattern_property_schema = rest_find_matching_pattern_property_schema( $property, $args );
    2154                         if ( null !== $pattern_property_schema ) {
    2155                                 $is_valid = rest_validate_value_from_schema( $v, $pattern_property_schema, $param . '[' . $property . ']' );
    2156                                 if ( is_wp_error( $is_valid ) ) {
    2157                                         return $is_valid;
    2158                                 }
    2159                                 continue;
    2160                         }
    2161 
    2162                         if ( isset( $args['additionalProperties'] ) ) {
    2163                                 if ( false === $args['additionalProperties'] ) {
    2164                                         /* translators: %s: Property of an object. */
    2165                                         return new WP_Error( 'rest_additional_properties_forbidden', sprintf( __( '%1$s is not a valid property of Object.' ), $property ) );
    2166                                 }
    2167 
    2168                                 if ( is_array( $args['additionalProperties'] ) ) {
    2169                                         $is_valid = rest_validate_value_from_schema( $v, $args['additionalProperties'], $param . '[' . $property . ']' );
    2170                                         if ( is_wp_error( $is_valid ) ) {
    2171                                                 return $is_valid;
    2172                                         }
    2173                                 }
    2174                         }
    2175                 }
    2176 
    2177                 if ( isset( $args['minProperties'] ) && count( $value ) < $args['minProperties'] ) {
    2178                         return new WP_Error(
    2179                                 'rest_too_few_properties',
    2180                                 sprintf(
    2181                                         /* translators: 1: Parameter, 2: Number. */
    2182                                         _n(
    2183                                                 '%1$s must contain at least %2$s property.',
    2184                                                 '%1$s must contain at least %2$s properties.',
    2185                                                 $args['minProperties']
    2186                                         ),
    2187                                         $param,
    2188                                         number_format_i18n( $args['minProperties'] )
    2189                                 )
    2190                         );
    2191                 }
    2192 
    2193                 if ( isset( $args['maxProperties'] ) && count( $value ) > $args['maxProperties'] ) {
    2194                         return new WP_Error(
    2195                                 'rest_too_many_properties',
    2196                                 sprintf(
    2197                                         /* translators: 1: Parameter, 2: Number. */
    2198                                         _n(
    2199                                                 '%1$s must contain at most %2$s property.',
    2200                                                 '%1$s must contain at most %2$s properties.',
    2201                                                 $args['maxProperties']
    2202                                         ),
    2203                                         $param,
    2204                                         number_format_i18n( $args['maxProperties'] )
    2205                                 )
    2206                         );
    2207                 }
    2208         }
    2209 
    2210         if ( 'null' === $args['type'] ) {
    2211                 if ( null !== $value ) {
    2212                         return new WP_Error(
    2213                                 'rest_invalid_type',
    2214                                 /* translators: 1: Parameter, 2: Type name. */
    2215                                 sprintf( __( '%1$s is not of type %2$s.' ), $param, 'null' ),
    2216                                 array( 'param' => $param )
    2217                         );
    2218                 }
    2219 
    2220                 return true;
    2221         }
    2222 
    2223         if ( in_array( $args['type'], array( 'integer', 'number' ), true ) ) {
    2224                 if ( ! is_numeric( $value ) ) {
    2225                         return new WP_Error(
    2226                                 'rest_invalid_type',
    2227                                 /* translators: 1: Parameter, 2: Type name. */
    2228                                 sprintf( __( '%1$s is not of type %2$s.' ), $param, $args['type'] ),
    2229                                 array( 'param' => $param )
    2230                         );
    2231                 }
    2232 
    2233                 if ( isset( $args['multipleOf'] ) && fmod( $value, $args['multipleOf'] ) !== 0.0 ) {
    2234                         /* translators: 1: Parameter, 2: Multiplier. */
    2235                         return new WP_Error( 'rest_invalid_multiple', sprintf( __( '%1$s must be a multiple of %2$s.' ), $param, $args['multipleOf'] ) );
    2236                 }
    2237         }
    2238 
    2239         if ( 'integer' === $args['type'] && ! rest_is_integer( $value ) ) {
    2240                 return new WP_Error(
    2241                         'rest_invalid_type',
    2242                         /* translators: 1: Parameter, 2: Type name. */
    2243                         sprintf( __( '%1$s is not of type %2$s.' ), $param, 'integer' ),
    2244                         array( 'param' => $param )
    2245                 );
    2246         }
    2247 
    2248         if ( 'boolean' === $args['type'] && ! rest_is_boolean( $value ) ) {
    2249                 return new WP_Error(
    2250                         'rest_invalid_type',
    2251                         /* translators: 1: Parameter, 2: Type name. */
    2252                         sprintf( __( '%1$s is not of type %2$s.' ), $param, 'boolean' ),
    2253                         array( 'param' => $param )
    2254                 );
    2255         }
    2256 
    2257         if ( 'string' === $args['type'] ) {
    2258                 if ( ! is_string( $value ) ) {
    2259                         return new WP_Error(
    2260                                 'rest_invalid_type',
    2261                                 /* translators: 1: Parameter, 2: Type name. */
    2262                                 sprintf( __( '%1$s is not of type %2$s.' ), $param, 'string' ),
    2263                                 array( 'param' => $param )
    2264                         );
    2265                 }
    2266 
    2267                 if ( isset( $args['minLength'] ) && mb_strlen( $value ) < $args['minLength'] ) {
    2268                         return new WP_Error(
    2269                                 'rest_too_short',
    2270                                 sprintf(
    2271                                         /* translators: 1: Parameter, 2: Number of characters. */
    2272                                         _n( '%1$s must be at least %2$s character long.', '%1$s must be at least %2$s characters long.', $args['minLength'] ),
    2273                                         $param,
    2274                                         number_format_i18n( $args['minLength'] )
    2275                                 )
    2276                         );
    2277                 }
    2278 
    2279                 if ( isset( $args['maxLength'] ) && mb_strlen( $value ) > $args['maxLength'] ) {
    2280                         return new WP_Error(
    2281                                 'rest_too_long',
    2282                                 sprintf(
    2283                                         /* translators: 1: Parameter, 2: Number of characters. */
    2284                                         _n( '%1$s must be at most %2$s character long.', '%1$s must be at most %2$s characters long.', $args['maxLength'] ),
    2285                                         $param,
    2286                                         number_format_i18n( $args['maxLength'] )
    2287                                 )
    2288                         );
    2289                 }
    2290 
    2291                 if ( isset( $args['pattern'] ) && ! rest_validate_json_schema_pattern( $args['pattern'], $value ) ) {
    2292                         /* translators: 1: Parameter, 2: Pattern. */
    2293                         return new WP_Error( 'rest_invalid_pattern', sprintf( __( '%1$s does not match pattern %2$s.' ), $param, $args['pattern'] ) );
    2294                 }
     2057        switch ( $args['type'] ) {
     2058                case 'null':
     2059                        $is_valid = rest_validate_null_value_from_schema( $value, $param );
     2060                        break;
     2061                case 'boolean':
     2062                        $is_valid = rest_validate_boolean_value_from_schema( $value, $param );
     2063                        break;
     2064                case 'object':
     2065                        $is_valid = rest_validate_object_value_from_schema( $value, $args, $param );
     2066                        break;
     2067                case 'array':
     2068                        $is_valid = rest_validate_array_value_from_schema( $value, $args, $param );
     2069                        break;
     2070                case 'number':
     2071                        $is_valid = rest_validate_number_value_from_schema( $value, $args, $param );
     2072                        break;
     2073                case 'string':
     2074                        $is_valid = rest_validate_string_value_from_schema( $value, $args, $param );
     2075                        break;
     2076                case 'integer':
     2077                        $is_valid = rest_validate_integer_value_from_schema( $value, $args, $param );
     2078                        break;
     2079                default:
     2080                        $is_valid = true;
     2081                        break;
     2082        }
     2083
     2084        if ( is_wp_error( $is_valid ) ) {
     2085                return $is_valid;
    22952086        }
    22962087
     
    23402131        }
    23412132
    2342         if ( in_array( $args['type'], array( 'number', 'integer' ), true ) && ( isset( $args['minimum'] ) || isset( $args['maximum'] ) ) ) {
    2343                 if ( isset( $args['minimum'] ) && ! isset( $args['maximum'] ) ) {
    2344                         if ( ! empty( $args['exclusiveMinimum'] ) && $value <= $args['minimum'] ) {
     2133        return true;
     2134}
     2135
     2136/**
     2137 * Validates a null value based on a schema.
     2138 *
     2139 * @since 5.7.0
     2140 *
     2141 * @param mixed  $value The value to validate.
     2142 * @param string $param The parameter name, used in error messages.
     2143 * @return true|WP_Error
     2144 */
     2145function rest_validate_null_value_from_schema( $value, $param ) {
     2146        if ( null !== $value ) {
     2147                return new WP_Error(
     2148                        'rest_invalid_type',
     2149                        /* translators: 1: Parameter, 2: Type name. */
     2150                        sprintf( __( '%1$s is not of type %2$s.' ), $param, 'null' ),
     2151                        array( 'param' => $param )
     2152                );
     2153        }
     2154
     2155        return true;
     2156}
     2157
     2158/**
     2159 * Validates a boolean value based on a schema.
     2160 *
     2161 * @since 5.7.0
     2162 *
     2163 * @param mixed  $value The value to validate.
     2164 * @param string $param The parameter name, used in error messages.
     2165 * @return true|WP_Error
     2166 */
     2167function rest_validate_boolean_value_from_schema( $value, $param ) {
     2168        if ( ! rest_is_boolean( $value ) ) {
     2169                return new WP_Error(
     2170                        'rest_invalid_type',
     2171                        /* translators: 1: Parameter, 2: Type name. */
     2172                        sprintf( __( '%1$s is not of type %2$s.' ), $param, 'boolean' ),
     2173                        array( 'param' => $param )
     2174                );
     2175        }
     2176
     2177        return true;
     2178}
     2179
     2180/**
     2181 * Validates an object value based on a schema.
     2182 *
     2183 * @since 5.7.0
     2184 *
     2185 * @param mixed  $value The value to validate.
     2186 * @param array  $args  Schema array to use for validation.
     2187 * @param string $param The parameter name, used in error messages.
     2188 * @return true|WP_Error
     2189 */
     2190function rest_validate_object_value_from_schema( $value, $args, $param ) {
     2191        if ( ! rest_is_object( $value ) ) {
     2192                return new WP_Error(
     2193                        'rest_invalid_type',
     2194                        /* translators: 1: Parameter, 2: Type name. */
     2195                        sprintf( __( '%1$s is not of type %2$s.' ), $param, 'object' ),
     2196                        array( 'param' => $param )
     2197                );
     2198        }
     2199
     2200        $value = rest_sanitize_object( $value );
     2201
     2202        if ( isset( $args['required'] ) && is_array( $args['required'] ) ) { // schema version 4
     2203                foreach ( $args['required'] as $name ) {
     2204                        if ( ! array_key_exists( $name, $value ) ) {
     2205                                return new WP_Error(
     2206                                        'rest_property_required',
     2207                                        /* translators: 1: Property of an object, 2: Parameter. */
     2208                                        sprintf( __( '%1$s is a required property of %2$s.' ), $name, $param )
     2209                                );
     2210                        }
     2211                }
     2212        } elseif ( isset( $args['properties'] ) ) { // schema version 3
     2213                foreach ( $args['properties'] as $name => $property ) {
     2214                        if ( isset( $property['required'] ) && true === $property['required'] && ! array_key_exists( $name, $value ) ) {
     2215                                return new WP_Error(
     2216                                        'rest_property_required',
     2217                                        /* translators: 1: Property of an object, 2: Parameter. */
     2218                                        sprintf( __( '%1$s is a required property of %2$s.' ), $name, $param )
     2219                                );
     2220                        }
     2221                }
     2222        }
     2223
     2224        foreach ( $value as $property => $v ) {
     2225                if ( isset( $args['properties'][ $property ] ) ) {
     2226                        $is_valid = rest_validate_value_from_schema( $v, $args['properties'][ $property ], $param . '[' . $property . ']' );
     2227                        if ( is_wp_error( $is_valid ) ) {
     2228                                return $is_valid;
     2229                        }
     2230                        continue;
     2231                }
     2232
     2233                $pattern_property_schema = rest_find_matching_pattern_property_schema( $property, $args );
     2234                if ( null !== $pattern_property_schema ) {
     2235                        $is_valid = rest_validate_value_from_schema( $v, $pattern_property_schema, $param . '[' . $property . ']' );
     2236                        if ( is_wp_error( $is_valid ) ) {
     2237                                return $is_valid;
     2238                        }
     2239                        continue;
     2240                }
     2241
     2242                if ( isset( $args['additionalProperties'] ) ) {
     2243                        if ( false === $args['additionalProperties'] ) {
     2244                                return new WP_Error(
     2245                                        'rest_additional_properties_forbidden',
     2246                                        /* translators: %s: Property of an object. */
     2247                                        sprintf( __( '%1$s is not a valid property of Object.' ), $property )
     2248                                );
     2249                        }
     2250
     2251                        if ( is_array( $args['additionalProperties'] ) ) {
     2252                                $is_valid = rest_validate_value_from_schema( $v, $args['additionalProperties'], $param . '[' . $property . ']' );
     2253                                if ( is_wp_error( $is_valid ) ) {
     2254                                        return $is_valid;
     2255                                }
     2256                        }
     2257                }
     2258        }
     2259
     2260        if ( isset( $args['minProperties'] ) && count( $value ) < $args['minProperties'] ) {
     2261                return new WP_Error(
     2262                        'rest_too_few_properties',
     2263                        /* translators: 1: Parameter, 2: Number. */
     2264                        sprintf(
     2265                                _n(
     2266                                        '%1$s must contain at least %2$s property.',
     2267                                        '%1$s must contain at least %2$s properties.',
     2268                                        $args['minProperties']
     2269                                ),
     2270                                $param,
     2271                                number_format_i18n( $args['minProperties'] )
     2272                        )
     2273                );
     2274        }
     2275
     2276        if ( isset( $args['maxProperties'] ) && count( $value ) > $args['maxProperties'] ) {
     2277                return new WP_Error(
     2278                        'rest_too_many_properties',
     2279                        /* translators: 1: Parameter, 2: Number. */
     2280                        sprintf(
     2281                                _n(
     2282                                        '%1$s must contain at most %2$s property.',
     2283                                        '%1$s must contain at most %2$s properties.',
     2284                                        $args['maxProperties']
     2285                                ),
     2286                                $param,
     2287                                number_format_i18n( $args['maxProperties'] )
     2288                        )
     2289                );
     2290        }
     2291
     2292        return true;
     2293}
     2294
     2295/**
     2296 * Validates an array value based on a schema.
     2297 *
     2298 * @since 5.7.0
     2299 *
     2300 * @param mixed  $value The value to validate.
     2301 * @param array  $args  Schema array to use for validation.
     2302 * @param string $param The parameter name, used in error messages.
     2303 * @return true|WP_Error
     2304 */
     2305function rest_validate_array_value_from_schema( $value, $args, $param ) {
     2306        if ( ! rest_is_array( $value ) ) {
     2307                return new WP_Error(
     2308                        'rest_invalid_type',
     2309                        /* translators: 1: Parameter, 2: Type name. */
     2310                        sprintf( __( '%1$s is not of type %2$s.' ), $param, 'array' ),
     2311                        array( 'param' => $param )
     2312                );
     2313        }
     2314
     2315        $value = rest_sanitize_array( $value );
     2316
     2317        if ( isset( $args['items'] ) ) {
     2318                foreach ( $value as $index => $v ) {
     2319                        $is_valid = rest_validate_value_from_schema( $v, $args['items'], $param . '[' . $index . ']' );
     2320                        if ( is_wp_error( $is_valid ) ) {
     2321                                return $is_valid;
     2322                        }
     2323                }
     2324        }
     2325
     2326        if ( isset( $args['minItems'] ) && count( $value ) < $args['minItems'] ) {
     2327                return new WP_Error(
     2328                        'rest_too_few_items',
     2329                        /* translators: 1: Parameter, 2: Number. */
     2330                        sprintf(
     2331                                _n(
     2332                                        '%1$s must contain at least %2$s item.',
     2333                                        '%1$s must contain at least %2$s items.',
     2334                                        $args['minItems']
     2335                                ),
     2336                                $param,
     2337                                number_format_i18n( $args['minItems'] )
     2338                        )
     2339                );
     2340        }
     2341
     2342        if ( isset( $args['maxItems'] ) && count( $value ) > $args['maxItems'] ) {
     2343                return new WP_Error(
     2344                        'test_too_many_items',
     2345                        /* translators: 1: Parameter, 2: Number. */
     2346                        sprintf(
     2347                                _n(
     2348                                        '%1$s must contain at most %2$s item.',
     2349                                        '%1$s must contain at most %2$s items.',
     2350                                        $args['maxItems']
     2351                                ),
     2352                                $param,
     2353                                number_format_i18n( $args['maxItems'] )
     2354                        )
     2355                );
     2356        }
     2357
     2358        if ( ! empty( $args['uniqueItems'] ) && ! rest_validate_array_contains_unique_items( $value ) ) {
     2359                /* translators: 1: Parameter. */
     2360                return new WP_Error( 'rest_duplicate_items', sprintf( __( '%1$s has duplicate items.' ), $param ) );
     2361        }
     2362
     2363        return true;
     2364}
     2365
     2366/**
     2367 * Validates a number value based on a schema.
     2368 *
     2369 * @since 5.7.0
     2370 *
     2371 * @param mixed  $value The value to validate.
     2372 * @param array  $args  Schema array to use for validation.
     2373 * @param string $param The parameter name, used in error messages.
     2374 * @return true|WP_Error
     2375 */
     2376function rest_validate_number_value_from_schema( $value, $args, $param ) {
     2377        if ( ! is_numeric( $value ) ) {
     2378                return new WP_Error(
     2379                        'rest_invalid_type',
     2380                        /* translators: 1: Parameter, 2: Type name. */
     2381                        sprintf( __( '%1$s is not of type %2$s.' ), $param, $args['type'] ),
     2382                        array( 'param' => $param )
     2383                );
     2384        }
     2385
     2386        if ( isset( $args['multipleOf'] ) && fmod( $value, $args['multipleOf'] ) !== 0.0 ) {
     2387                return new WP_Error(
     2388                        'rest_invalid_multiple',
     2389                        /* translators: 1: Parameter, 2: Multiplier. */
     2390                        sprintf( __( '%1$s must be a multiple of %2$s.' ), $param, $args['multipleOf'] )
     2391                );
     2392        }
     2393
     2394        if ( isset( $args['minimum'] ) && ! isset( $args['maximum'] ) ) {
     2395                if ( ! empty( $args['exclusiveMinimum'] ) && $value <= $args['minimum'] ) {
     2396                        return new WP_Error(
     2397                                'rest_out_of_bounds',
    23452398                                /* translators: 1: Parameter, 2: Minimum number. */
    2346                                 return new WP_Error( 'rest_out_of_bounds', sprintf( __( '%1$s must be greater than %2$d' ), $param, $args['minimum'] ) );
    2347                         } elseif ( empty( $args['exclusiveMinimum'] ) && $value < $args['minimum'] ) {
     2399                                sprintf( __( '%1$s must be greater than %2$d' ), $param, $args['minimum'] )
     2400                        );
     2401                }
     2402
     2403                if ( empty( $args['exclusiveMinimum'] ) && $value < $args['minimum'] ) {
     2404                        return new WP_Error(
     2405                                'rest_out_of_bounds',
    23482406                                /* translators: 1: Parameter, 2: Minimum number. */
    2349                                 return new WP_Error( 'rest_out_of_bounds', sprintf( __( '%1$s must be greater than or equal to %2$d' ), $param, $args['minimum'] ) );
    2350                         }
    2351                 } elseif ( isset( $args['maximum'] ) && ! isset( $args['minimum'] ) ) {
    2352                         if ( ! empty( $args['exclusiveMaximum'] ) && $value >= $args['maximum'] ) {
     2407                                sprintf( __( '%1$s must be greater than or equal to %2$d' ), $param, $args['minimum'] )
     2408                        );
     2409                }
     2410        }
     2411
     2412        if ( isset( $args['maximum'] ) && ! isset( $args['minimum'] ) ) {
     2413                if ( ! empty( $args['exclusiveMaximum'] ) && $value >= $args['maximum'] ) {
     2414                        return new WP_Error(
     2415                                'rest_out_of_bounds',
    23532416                                /* translators: 1: Parameter, 2: Maximum number. */
    2354                                 return new WP_Error( 'rest_out_of_bounds', sprintf( __( '%1$s must be less than %2$d' ), $param, $args['maximum'] ) );
    2355                         } elseif ( empty( $args['exclusiveMaximum'] ) && $value > $args['maximum'] ) {
     2417                                sprintf( __( '%1$s must be less than %2$d' ), $param, $args['maximum'] )
     2418                        );
     2419                }
     2420
     2421                if ( empty( $args['exclusiveMaximum'] ) && $value > $args['maximum'] ) {
     2422                        return new WP_Error(
     2423                                'rest_out_of_bounds',
    23562424                                /* translators: 1: Parameter, 2: Maximum number. */
    2357                                 return new WP_Error( 'rest_out_of_bounds', sprintf( __( '%1$s must be less than or equal to %2$d' ), $param, $args['maximum'] ) );
    2358                         }
    2359                 } elseif ( isset( $args['maximum'] ) && isset( $args['minimum'] ) ) {
    2360                         if ( ! empty( $args['exclusiveMinimum'] ) && ! empty( $args['exclusiveMaximum'] ) ) {
    2361                                 if ( $value >= $args['maximum'] || $value <= $args['minimum'] ) {
     2425                                sprintf( __( '%1$s must be less than or equal to %2$d' ), $param, $args['maximum'] )
     2426                        );
     2427                }
     2428        }
     2429
     2430        if ( isset( $args['minimum'], $args['maximum'] ) ) {
     2431                if ( ! empty( $args['exclusiveMinimum'] ) && ! empty( $args['exclusiveMaximum'] ) ) {
     2432                        if ( $value >= $args['maximum'] || $value <= $args['minimum'] ) {
     2433                                return new WP_Error(
     2434                                        'rest_out_of_bounds',
    23622435                                        /* translators: 1: Parameter, 2: Minimum number, 3: Maximum number. */
    2363                                         return new WP_Error( 'rest_out_of_bounds', sprintf( __( '%1$s must be between %2$d (exclusive) and %3$d (exclusive)' ), $param, $args['minimum'], $args['maximum'] ) );
    2364                                 }
    2365                         } elseif ( empty( $args['exclusiveMinimum'] ) && ! empty( $args['exclusiveMaximum'] ) ) {
    2366                                 if ( $value >= $args['maximum'] || $value < $args['minimum'] ) {
     2436                                        sprintf(
     2437                                                __( '%1$s must be between %2$d (exclusive) and %3$d (exclusive)' ),
     2438                                                $param,
     2439                                                $args['minimum'],
     2440                                                $args['maximum']
     2441                                        )
     2442                                );
     2443                        }
     2444                }
     2445
     2446                if ( ! empty( $args['exclusiveMinimum'] ) && empty( $args['exclusiveMaximum'] ) ) {
     2447                        if ( $value > $args['maximum'] || $value <= $args['minimum'] ) {
     2448                                return new WP_Error(
     2449                                        'rest_out_of_bounds',
    23672450                                        /* translators: 1: Parameter, 2: Minimum number, 3: Maximum number. */
    2368                                         return new WP_Error( 'rest_out_of_bounds', sprintf( __( '%1$s must be between %2$d (inclusive) and %3$d (exclusive)' ), $param, $args['minimum'], $args['maximum'] ) );
    2369                                 }
    2370                         } elseif ( ! empty( $args['exclusiveMinimum'] ) && empty( $args['exclusiveMaximum'] ) ) {
    2371                                 if ( $value > $args['maximum'] || $value <= $args['minimum'] ) {
     2451                                        sprintf(
     2452                                                __( '%1$s must be between %2$d (exclusive) and %3$d (inclusive)' ),
     2453                                                $param,
     2454                                                $args['minimum'],
     2455                                                $args['maximum']
     2456                                        )
     2457                                );
     2458                        }
     2459                }
     2460
     2461                if ( ! empty( $args['exclusiveMaximum'] ) && empty( $args['exclusiveMinimum'] ) ) {
     2462                        if ( $value >= $args['maximum'] || $value < $args['minimum'] ) {
     2463                                return new WP_Error(
     2464                                        'rest_out_of_bounds',
    23722465                                        /* translators: 1: Parameter, 2: Minimum number, 3: Maximum number. */
    2373                                         return new WP_Error( 'rest_out_of_bounds', sprintf( __( '%1$s must be between %2$d (exclusive) and %3$d (inclusive)' ), $param, $args['minimum'], $args['maximum'] ) );
    2374                                 }
    2375                         } elseif ( empty( $args['exclusiveMinimum'] ) && empty( $args['exclusiveMaximum'] ) ) {
    2376                                 if ( $value > $args['maximum'] || $value < $args['minimum'] ) {
     2466                                        sprintf(
     2467                                                __( '%1$s must be between %2$d (inclusive) and %3$d (exclusive)' ),
     2468                                                $param,
     2469                                                $args['minimum'],
     2470                                                $args['maximum']
     2471                                        )
     2472                                );
     2473                        }
     2474                }
     2475
     2476                if ( empty( $args['exclusiveMinimum'] ) && empty( $args['exclusiveMaximum'] ) ) {
     2477                        if ( $value > $args['maximum'] || $value < $args['minimum'] ) {
     2478                                return new WP_Error(
     2479                                        'rest_out_of_bounds',
    23772480                                        /* translators: 1: Parameter, 2: Minimum number, 3: Maximum number. */
    2378                                         return new WP_Error( 'rest_out_of_bounds', sprintf( __( '%1$s must be between %2$d (inclusive) and %3$d (inclusive)' ), $param, $args['minimum'], $args['maximum'] ) );
    2379                                 }
    2380                         }
    2381                 }
     2481                                        sprintf(
     2482                                                __( '%1$s must be between %2$d (inclusive) and %3$d (inclusive)' ),
     2483                                                $param,
     2484                                                $args['minimum'],
     2485                                                $args['maximum']
     2486                                        )
     2487                                );
     2488                        }
     2489                }
     2490        }
     2491
     2492        return true;
     2493}
     2494
     2495/**
     2496 * Validates a string value based on a schema.
     2497 *
     2498 * @since 5.7.0
     2499 *
     2500 * @param mixed  $value The value to validate.
     2501 * @param array  $args  Schema array to use for validation.
     2502 * @param string $param The parameter name, used in error messages.
     2503 * @return true|WP_Error
     2504 */
     2505function rest_validate_string_value_from_schema( $value, $args, $param ) {
     2506        if ( ! is_string( $value ) ) {
     2507                return new WP_Error(
     2508                        'rest_invalid_type',
     2509                        /* translators: 1: Parameter, 2: Type name. */
     2510                        sprintf( __( '%1$s is not of type %2$s.' ), $param, 'string' ),
     2511                        array( 'param' => $param )
     2512                );
     2513        }
     2514
     2515        if ( isset( $args['minLength'] ) && mb_strlen( $value ) < $args['minLength'] ) {
     2516                return new WP_Error(
     2517                        'rest_too_short',
     2518                        /* translators: 1: Parameter, 2: Number of characters. */
     2519                        sprintf(
     2520                                _n(
     2521                                        '%1$s must be at least %2$s character long.',
     2522                                        '%1$s must be at least %2$s characters long.',
     2523                                        $args['minLength']
     2524                                ),
     2525                                $param,
     2526                                number_format_i18n( $args['minLength'] )
     2527                        )
     2528                );
     2529        }
     2530
     2531        if ( isset( $args['maxLength'] ) && mb_strlen( $value ) > $args['maxLength'] ) {
     2532                return new WP_Error(
     2533                        'rest_too_long',
     2534                        /* translators: 1: Parameter, 2: Number of characters. */
     2535                        sprintf(
     2536                                _n(
     2537                                        '%1$s must be at most %2$s character long.',
     2538                                        '%1$s must be at most %2$s characters long.',
     2539                                        $args['maxLength']
     2540                                ),
     2541                                $param,
     2542                                number_format_i18n( $args['maxLength'] )
     2543                        )
     2544                );
     2545        }
     2546
     2547        if ( isset( $args['pattern'] ) && ! rest_validate_json_schema_pattern( $args['pattern'], $value ) ) {
     2548                return new WP_Error(
     2549                        'rest_invalid_pattern',
     2550                        /* translators: 1: Parameter, 2: Pattern. */
     2551                        sprintf( __( '%1$s does not match pattern %2$s.' ), $param, $args['pattern'] )
     2552                );
     2553        }
     2554
     2555        return true;
     2556}
     2557
     2558/**
     2559 * Validates an integer value based on a schema.
     2560 *
     2561 * @since 5.7.0
     2562 *
     2563 * @param mixed  $value The value to validate.
     2564 * @param array  $args  Schema array to use for validation.
     2565 * @param string $param The parameter name, used in error messages.
     2566 * @return true|WP_Error
     2567 */
     2568function rest_validate_integer_value_from_schema( $value, $args, $param ) {
     2569        $is_valid_number = rest_validate_number_value_from_schema( $value, $args, $param );
     2570        if ( is_wp_error( $is_valid_number ) ) {
     2571                return $is_valid_number;
     2572        }
     2573
     2574        if ( ! rest_is_integer( $value ) ) {
     2575                return new WP_Error(
     2576                        'rest_invalid_type',
     2577                        /* translators: 1: Parameter, 2: Type name. */
     2578                        sprintf( __( '%1$s is not of type %2$s.' ), $param, 'integer' ),
     2579                        array( 'param' => $param )
     2580                );
    23822581        }
    23832582
Note: See TracChangeset for help on using the changeset viewer.