rest_find_one_matching_schema

The timeline below displays how wordpress function rest_find_one_matching_schema has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 5.7

/**
 * Finds the matching schema among the "oneOf" schemas.
 *
 * @since 5.6.0
 *
 * @param mixed  $value                  The value to validate.
 * @param array  $args                   The schema array to use.
 * @param string $param                  The parameter name, used in error messages.
 * @param bool   $stop_after_first_match Optional. Whether the process should stop after the first successful match.
 * @return array|WP_Error                The matching schema or WP_Error instance if the number of matching schemas is not equal to one.
 */
function rest_find_one_matching_schema($value, $args, $param, $stop_after_first_match = false)
{
    $matching_schemas = array();
    $errors = array();
    foreach ($args['oneOf'] as $index => $schema) {
        if (!isset($schema['type']) && isset($args['type'])) {
            $schema['type'] = $args['type'];
        }
        $is_valid = rest_validate_value_from_schema($value, $schema, $param);
        if (!is_wp_error($is_valid)) {
            if ($stop_after_first_match) {
                return $schema;
            }
            $matching_schemas[] = array('schema_object' => $schema, 'index' => $index);
        } else {
            $errors[] = array('error_object' => $is_valid, 'schema' => $schema, 'index' => $index);
        }
    }
    if (!$matching_schemas) {
        return rest_get_combining_operation_error($value, $param, $errors);
    }
    if (count($matching_schemas) > 1) {
        $schema_positions = array();
        $schema_titles = array();
        foreach ($matching_schemas as $schema) {
            $schema_positions[] = $schema['index'];
            if (isset($schema['schema_object']['title'])) {
                $schema_titles[] = $schema['schema_object']['title'];
            }
        }
        // If each schema has a title, include those titles in the error message.
        if (count($schema_titles) === count($matching_schemas)) {
            return new WP_Error(
                'rest_one_of_multiple_matches',
                /* translators: 1: Parameter, 2: Schema titles. */
                wp_sprintf(__('%1$s matches %2$l, but should match only one.'), $param, $schema_titles),
                array('positions' => $schema_positions)
            );
        }
        return new WP_Error(
            'rest_one_of_multiple_matches',
            /* translators: %s: Parameter. */
            sprintf(__('%s matches more than one of the expected formats.'), $param),
            array('positions' => $schema_positions)
        );
    }
    return $matching_schemas[0]['schema_object'];
}

WordPress Version: 5.6

/**
 * Finds the matching schema among the "oneOf" schemas.
 *
 * @since 5.6.0
 *
 * @param mixed  $value                  The value to validate.
 * @param array  $args                   The schema array to use.
 * @param string $param                  The parameter name, used in error messages.
 * @param bool   $stop_after_first_match Optional. Whether the process should stop after the first successful match.
 * @return array|WP_Error                The matching schema or WP_Error instance if the number of matching schemas is not equal to one.
 */
function rest_find_one_matching_schema($value, $args, $param, $stop_after_first_match = false)
{
    $matching_schemas = array();
    $errors = array();
    foreach ($args['oneOf'] as $index => $schema) {
        if (!isset($schema['type']) && isset($args['type'])) {
            $schema['type'] = $args['type'];
        }
        $is_valid = rest_validate_value_from_schema($value, $schema, $param);
        if (!is_wp_error($is_valid)) {
            if ($stop_after_first_match) {
                return $schema;
            }
            $matching_schemas[] = array('schema_object' => $schema, 'index' => $index);
        } else {
            $errors[] = array('error_object' => $is_valid, 'schema' => $schema, 'index' => $index);
        }
    }
    if (!$matching_schemas) {
        return rest_get_combining_operation_error($value, $param, $errors);
    }
    if (count($matching_schemas) > 1) {
        $schema_positions = array();
        $schema_titles = array();
        foreach ($matching_schemas as $schema) {
            $schema_positions[] = $schema['index'];
            if (isset($schema['schema_object']['title'])) {
                $schema_titles[] = $schema['schema_object']['title'];
            }
        }
        // If each schema has a title, include those titles in the error message.
        if (count($schema_titles) === count($matching_schemas)) {
            return new WP_Error(
                'rest_invalid_param',
                /* translators: 1: Parameter, 2: Schema titles. */
                wp_sprintf(__('%1$s matches %2$l, but should match only one.'), $param, $schema_titles),
                array('positions' => $schema_positions)
            );
        }
        return new WP_Error(
            'rest_invalid_param',
            /* translators: 1: Parameter. */
            sprintf(__('%1$s matches more than one of the expected formats.'), $param),
            array('positions' => $schema_positions)
        );
    }
    return $matching_schemas[0]['schema_object'];
}