rest_get_combining_operation_error

函数
rest_get_combining_operation_error ( $value, $param, $errors )
参数
  • (array) $value The value to validate.
    Required:
  • (string) $param The parameter name, used in error messages.
    Required:
  • (array) $errors The errors array, to search for possible error.
    Required:
返回值
  • (WP_Error) The combining operation error.
定义位置
相关方法
rest_format_combining_operation_errorget_comments_pagination_arrowwp_register_comment_personal_data_eraserwp_register_comment_personal_data_exporterregister_widget_control
引入
5.6.0
弃用
-

rest_get_combining_operation_error: 这个函数用于为使用”allOf”或”anyOf”等组合操作的JSON模式生成一个错误响应。它用于确保JSON模式验证工作正常,并确保响应的格式正确。

获取合并操作的错误。

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 ) );
}

常见问题

FAQs
查看更多 >