函数文档

rest_get_combining_operation_error()

💡 云策文档标注

概述

rest_get_combining_operation_error() 函数用于获取组合操作中的验证错误,主要处理 REST API 中参数验证时多个错误场景的筛选和格式化。它通过分析错误数组,返回一个合适的 WP_Error 对象,以提供清晰的错误信息。

关键要点

  • 函数接受三个参数:$value(待验证的值)、$param(参数名,用于错误消息)、$errors(错误数组,用于搜索可能的错误)。
  • 返回一个 WP_Error 对象,表示组合操作错误。
  • 函数逻辑包括:如果只有一个错误,直接返回格式化后的错误;过滤掉与类型验证相关的错误;针对对象类型错误,选择最匹配的错误;如果所有模式都有标题,则在错误消息中包含标题。
  • 内部调用 rest_format_combining_operation_error() 来格式化错误,并可能使用 wp_sprintf() 和 __() 进行本地化处理。

代码示例

function rest_get_combining_operation_error( $value, $param, $errors ) {
    // If there is only one error, simply return it.
    if ( 1 === count( $errors ) ) {
        return rest_format_combining_operation_error( $param, $errors[0] );
    }

    // Filter out all errors related to type validation.
    $filtered_errors = array();
    foreach ( $errors as $error ) {
        $error_code = $error['error_object']->get_error_code();
        $error_data = $error['error_object']->get_error_data();

        if ( 'rest_invalid_type' !== $error_code || ( isset( $error_data['param'] ) && $param !== $error_data['param'] ) ) {
            $filtered_errors[] = $error;
        }
    }

    // If there is only one error left, simply return it.
    if ( 1 === count( $filtered_errors ) ) {
        return rest_format_combining_operation_error( $param, $filtered_errors[0] );
    }

    // If there are only errors related to object validation, try choosing the most appropriate one.
    if ( count( $filtered_errors ) > 1 && 'object' === $filtered_errors[0]['schema']['type'] ) {
        $result = null;
        $number = 0;

        foreach ( $filtered_errors as $error ) {
            if ( isset( $error['schema']['properties'] ) ) {
                $n = count( array_intersect_key( $error['schema']['properties'], $value ) );
                if ( $n > $number ) {
                    $result = $error;
                    $number = $n;
                }
            }
        }

        if ( null !== $result ) {
            return rest_format_combining_operation_error( $param, $result );
        }
    }

    // If each schema has a title, include those titles in the error message.
    $schema_titles = array();
    foreach ( $errors as $error ) {
        if ( isset( $error['schema']['title'] ) ) {
            $schema_titles[] = $error['schema']['title'];
        }
    }

    if ( count( $schema_titles ) === count( $errors ) ) {
        /* translators: 1: Parameter, 2: Schema titles. */
        return new WP_Error( 'rest_no_matching_schema', wp_sprintf( __( '%1$s is not a valid %2$l.' ), $param, $schema_titles ) );
    }

    /* translators: %s: Parameter. */
    return new WP_Error( 'rest_no_matching_schema', sprintf( __( '%s does not match any of the expected formats.' ), $param ) );
}

注意事项

  • 此函数主要用于 REST API 验证场景,如处理 anyOf 或 oneOf 模式时的错误组合。
  • 错误数组 $errors 应包含结构化错误信息,通常来自其他验证函数。
  • 函数依赖于 WP_Error 类和相关辅助函数,确保在 WordPress 环境中使用。
  • 自 WordPress 5.6.0 版本引入,使用时需注意版本兼容性。

📄 原文内容

Gets the error of combining operation.

Parameters

$valuearrayrequired
The value to validate.
$paramstringrequired
The parameter name, used in error messages.
$errorsarrayrequired
The errors array, to search for possible error.

Return

WP_Error The combining operation error.

Source

function rest_get_combining_operation_error( $value, $param, $errors ) {
	// If there is only one error, simply return it.
	if ( 1 === count( $errors ) ) {
		return rest_format_combining_operation_error( $param, $errors[0] );
	}

	// Filter out all errors related to type validation.
	$filtered_errors = array();
	foreach ( $errors as $error ) {
		$error_code = $error['error_object']->get_error_code();
		$error_data = $error['error_object']->get_error_data();

		if ( 'rest_invalid_type' !== $error_code || ( isset( $error_data['param'] ) && $param !== $error_data['param'] ) ) {
			$filtered_errors[] = $error;
		}
	}

	// If there is only one error left, simply return it.
	if ( 1 === count( $filtered_errors ) ) {
		return rest_format_combining_operation_error( $param, $filtered_errors[0] );
	}

	// If there are only errors related to object validation, try choosing the most appropriate one.
	if ( count( $filtered_errors ) > 1 && 'object' === $filtered_errors[0]['schema']['type'] ) {
		$result = null;
		$number = 0;

		foreach ( $filtered_errors as $error ) {
			if ( isset( $error['schema']['properties'] ) ) {
				$n = count( array_intersect_key( $error['schema']['properties'], $value ) );
				if ( $n > $number ) {
					$result = $error;
					$number = $n;
				}
			}
		}

		if ( null !== $result ) {
			return rest_format_combining_operation_error( $param, $result );
		}
	}

	// If each schema has a title, include those titles in the error message.
	$schema_titles = array();
	foreach ( $errors as $error ) {
		if ( isset( $error['schema']['title'] ) ) {
			$schema_titles[] = $error['schema']['title'];
		}
	}

	if ( count( $schema_titles ) === count( $errors ) ) {
		/* translators: 1: Parameter, 2: Schema titles. */
		return new WP_Error( 'rest_no_matching_schema', wp_sprintf( __( '%1$s is not a valid %2$l.' ), $param, $schema_titles ) );
	}

	/* translators: %s: Parameter. */
	return new WP_Error( 'rest_no_matching_schema', sprintf( __( '%s does not match any of the expected formats.' ), $param ) );
}

Changelog

Version Description
5.6.0 Introduced.