Make WordPress Core


Ignore:
Timestamp:
01/24/2021 03:57:39 AM (6 years ago)
Author:
TimothyBlynJacobs
Message:

REST API: Add more specific error codes for schema validation.

Previously, the majority of JSON Schema validation errors returned a generic rest_invalid_param error code. In preparation for #46191, where the underlying validation error code will be exposed, this commit adds specific error codes for each failure scenario.

Fixes #52317.

File:
1 edited

Legend:

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

    r50005 r50007  
    16821682
    16831683                return new WP_Error(
    1684                         'rest_invalid_param',
     1684                        'rest_no_matching_schema',
    16851685                        /* translators: 1: Parameter, 2: Schema title, 3: Reason. */
    16861686                        sprintf( __( '%1$s is not a valid %2$s. Reason: %3$s' ), $param, $title, $reason ),
    … …  
    16901690
    16911691        return new WP_Error(
    1692                 'rest_invalid_param',
     1692                'rest_no_matching_schema',
    16931693                /* translators: 1: Parameter, 2: Reason. */
    16941694                sprintf( __( '%1$s does not match the expected format. Reason: %2$s' ), $param, $reason ),
    … …  
    17591759        if ( count( $schema_titles ) === count( $errors ) ) {
    17601760                /* translators: 1: Parameter, 2: Schema titles. */
    1761                 return new WP_Error( 'rest_invalid_param', wp_sprintf( __( '%1$s is not a valid %2$l.' ), $param, $schema_titles ) );
     1761                return new WP_Error( 'rest_no_matching_schema', wp_sprintf( __( '%1$s is not a valid %2$l.' ), $param, $schema_titles ) );
    17621762        }
    17631763
    17641764        /* translators: 1: Parameter. */
    1765         return new WP_Error( 'rest_invalid_param', sprintf( __( '%1$s does not match any of the expected formats.' ), $param ) );
     1765        return new WP_Error( 'rest_no_matching_schema', sprintf( __( '%1$s does not match any of the expected formats.' ), $param ) );
    17661766}
    17671767
    … …  
    18571857                if ( count( $schema_titles ) === count( $matching_schemas ) ) {
    18581858                        return new WP_Error(
    1859                                 'rest_invalid_param',
     1859                                'rest_one_of_multiple_matches',
    18601860                                /* translators: 1: Parameter, 2: Schema titles. */
    18611861                                wp_sprintf( __( '%1$s matches %2$l, but should match only one.' ), $param, $schema_titles ),
    … …  
    18651865
    18661866                return new WP_Error(
    1867                         'rest_invalid_param',
     1867                        'rest_one_of_multiple_matches',
    18681868                        /* translators: 1: Parameter. */
    18691869                        sprintf( __( '%1$s matches more than one of the expected formats.' ), $param ),
    … …  
    20112011                if ( isset( $args['minItems'] ) && count( $value ) < $args['minItems'] ) {
    20122012                        return new WP_Error(
    2013                                 'rest_invalid_param',
     2013                                'rest_too_few_items',
    20142014                                sprintf(
    20152015                                        /* translators: 1: Parameter, 2: Number. */
    … …  
    20272027                if ( isset( $args['maxItems'] ) && count( $value ) > $args['maxItems'] ) {
    20282028                        return new WP_Error(
    2029                                 'rest_invalid_param',
     2029                                'test_too_many_items',
    20302030                                sprintf(
    20312031                                        /* translators: 1: Parameter, 2: Number. */
    … …  
    20432043                if ( ! empty( $args['uniqueItems'] ) && ! rest_validate_array_contains_unique_items( $value ) ) {
    20442044                        /* translators: 1: Parameter. */
    2045                         return new WP_Error( 'rest_invalid_param', sprintf( __( '%1$s has duplicate items.' ), $param ) );
     2045                        return new WP_Error( 'rest_duplicate_items', sprintf( __( '%1$s has duplicate items.' ), $param ) );
    20462046                }
    20472047        }
    … …  
    20962096                                if ( false === $args['additionalProperties'] ) {
    20972097                                        /* translators: %s: Property of an object. */
    2098                                         return new WP_Error( 'rest_invalid_param', sprintf( __( '%1$s is not a valid property of Object.' ), $property ) );
     2098                                        return new WP_Error( 'rest_additional_properties_forbidden', sprintf( __( '%1$s is not a valid property of Object.' ), $property ) );
    20992099                                }
    21002100
    … …  
    21102110                if ( isset( $args['minProperties'] ) && count( $value ) < $args['minProperties'] ) {
    21112111                        return new WP_Error(
    2112                                 'rest_invalid_param',
     2112                                'rest_too_few_properties',
    21132113                                sprintf(
    21142114                                        /* translators: 1: Parameter, 2: Number. */
    … …  
    21262126                if ( isset( $args['maxProperties'] ) && count( $value ) > $args['maxProperties'] ) {
    21272127                        return new WP_Error(
    2128                                 'rest_invalid_param',
     2128                                'rest_too_many_properties',
    21292129                                sprintf(
    21302130                                        /* translators: 1: Parameter, 2: Number. */
    … …  
    21572157                if ( ! in_array( $value, $args['enum'], true ) ) {
    21582158                        /* translators: 1: Parameter, 2: List of valid values. */
    2159                         return new WP_Error( 'rest_invalid_param', sprintf( __( '%1$s is not one of %2$s.' ), $param, implode( ', ', $args['enum'] ) ) );
     2159                        return new WP_Error( 'rest_not_in_enum', sprintf( __( '%1$s is not one of %2$s.' ), $param, implode( ', ', $args['enum'] ) ) );
    21602160                }
    21612161        }
    … …  
    21732173                if ( isset( $args['multipleOf'] ) && fmod( $value, $args['multipleOf'] ) !== 0.0 ) {
    21742174                        /* translators: 1: Parameter, 2: Multiplier. */
    2175                         return new WP_Error( 'rest_invalid_param', sprintf( __( '%1$s must be a multiple of %2$s.' ), $param, $args['multipleOf'] ) );
     2175                        return new WP_Error( 'rest_invalid_multiple', sprintf( __( '%1$s must be a multiple of %2$s.' ), $param, $args['multipleOf'] ) );
    21762176                }
    21772177        }
    … …  
    22072207                if ( isset( $args['minLength'] ) && mb_strlen( $value ) < $args['minLength'] ) {
    22082208                        return new WP_Error(
    2209                                 'rest_invalid_param',
     2209                                'rest_too_short',
    22102210                                sprintf(
    22112211                                        /* translators: 1: Parameter, 2: Number of characters. */
    … …  
    22192219                if ( isset( $args['maxLength'] ) && mb_strlen( $value ) > $args['maxLength'] ) {
    22202220                        return new WP_Error(
    2221                                 'rest_invalid_param',
     2221                                'rest_too_long',
    22222222                                sprintf(
    22232223                                        /* translators: 1: Parameter, 2: Number of characters. */
    … …  
    22612261                                if ( ! rest_is_ip_address( $value ) ) {
    22622262                                        /* translators: %s: IP address. */
    2263                                         return new WP_Error( 'rest_invalid_param', sprintf( __( '%s is not a valid IP address.' ), $param ) );
     2263                                        return new WP_Error( 'rest_invalid_ip', sprintf( __( '%s is not a valid IP address.' ), $param ) );
    22642264                                }
    22652265                                break;
    … …  
    22772277                        if ( ! empty( $args['exclusiveMinimum'] ) && $value <= $args['minimum'] ) {
    22782278                                /* translators: 1: Parameter, 2: Minimum number. */
    2279                                 return new WP_Error( 'rest_invalid_param', sprintf( __( '%1$s must be greater than %2$d' ), $param, $args['minimum'] ) );
     2279                                return new WP_Error( 'rest_out_of_bounds', sprintf( __( '%1$s must be greater than %2$d' ), $param, $args['minimum'] ) );
    22802280                        } elseif ( empty( $args['exclusiveMinimum'] ) && $value < $args['minimum'] ) {
    22812281                                /* translators: 1: Parameter, 2: Minimum number. */
    2282                                 return new WP_Error( 'rest_invalid_param', sprintf( __( '%1$s must be greater than or equal to %2$d' ), $param, $args['minimum'] ) );
     2282                                return new WP_Error( 'rest_out_of_bounds', sprintf( __( '%1$s must be greater than or equal to %2$d' ), $param, $args['minimum'] ) );
    22832283                        }
    22842284                } elseif ( isset( $args['maximum'] ) && ! isset( $args['minimum'] ) ) {
    22852285                        if ( ! empty( $args['exclusiveMaximum'] ) && $value >= $args['maximum'] ) {
    22862286                                /* translators: 1: Parameter, 2: Maximum number. */
    2287                                 return new WP_Error( 'rest_invalid_param', sprintf( __( '%1$s must be less than %2$d' ), $param, $args['maximum'] ) );
     2287                                return new WP_Error( 'rest_out_of_bounds', sprintf( __( '%1$s must be less than %2$d' ), $param, $args['maximum'] ) );
    22882288                        } elseif ( empty( $args['exclusiveMaximum'] ) && $value > $args['maximum'] ) {
    22892289                                /* translators: 1: Parameter, 2: Maximum number. */
    2290                                 return new WP_Error( 'rest_invalid_param', sprintf( __( '%1$s must be less than or equal to %2$d' ), $param, $args['maximum'] ) );
     2290                                return new WP_Error( 'rest_out_of_bounds', sprintf( __( '%1$s must be less than or equal to %2$d' ), $param, $args['maximum'] ) );
    22912291                        }
    22922292                } elseif ( isset( $args['maximum'] ) && isset( $args['minimum'] ) ) {
    … …  
    22942294                                if ( $value >= $args['maximum'] || $value <= $args['minimum'] ) {
    22952295                                        /* translators: 1: Parameter, 2: Minimum number, 3: Maximum number. */
    2296                                         return new WP_Error( 'rest_invalid_param', sprintf( __( '%1$s must be between %2$d (exclusive) and %3$d (exclusive)' ), $param, $args['minimum'], $args['maximum'] ) );
     2296                                        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'] ) );
    22972297                                }
    22982298                        } elseif ( empty( $args['exclusiveMinimum'] ) && ! empty( $args['exclusiveMaximum'] ) ) {
    22992299                                if ( $value >= $args['maximum'] || $value < $args['minimum'] ) {
    23002300                                        /* translators: 1: Parameter, 2: Minimum number, 3: Maximum number. */
    2301                                         return new WP_Error( 'rest_invalid_param', sprintf( __( '%1$s must be between %2$d (inclusive) and %3$d (exclusive)' ), $param, $args['minimum'], $args['maximum'] ) );
     2301                                        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'] ) );
    23022302                                }
    23032303                        } elseif ( ! empty( $args['exclusiveMinimum'] ) && empty( $args['exclusiveMaximum'] ) ) {
    23042304                                if ( $value > $args['maximum'] || $value <= $args['minimum'] ) {
    23052305                                        /* translators: 1: Parameter, 2: Minimum number, 3: Maximum number. */
    2306                                         return new WP_Error( 'rest_invalid_param', sprintf( __( '%1$s must be between %2$d (exclusive) and %3$d (inclusive)' ), $param, $args['minimum'], $args['maximum'] ) );
     2306                                        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'] ) );
    23072307                                }
    23082308                        } elseif ( empty( $args['exclusiveMinimum'] ) && empty( $args['exclusiveMaximum'] ) ) {
    23092309                                if ( $value > $args['maximum'] || $value < $args['minimum'] ) {
    23102310                                        /* translators: 1: Parameter, 2: Minimum number, 3: Maximum number. */
    2311                                         return new WP_Error( 'rest_invalid_param', sprintf( __( '%1$s must be between %2$d (inclusive) and %3$d (inclusive)' ), $param, $args['minimum'], $args['maximum'] ) );
     2311                                        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'] ) );
    23122312                                }
    23132313                        }
    … …  
    23942394                if ( ! empty( $args['uniqueItems'] ) && ! rest_validate_array_contains_unique_items( $value ) ) {
    23952395                        /* translators: 1: Parameter. */
    2396                         return new WP_Error( 'rest_invalid_param', sprintf( __( '%1$s has duplicate items.' ), $param ) );
     2396                        return new WP_Error( 'rest_duplicate_items', sprintf( __( '%1$s has duplicate items.' ), $param ) );
    23972397                }
    23982398
Note: See TracChangeset for help on using the changeset viewer.