函数文档

rest_validate_array_value_from_schema()

💡 云策文档标注

概述

rest_validate_array_value_from_schema() 是一个 WordPress REST API 函数,用于根据提供的 schema 验证数组值。它检查数组类型、项目验证、最小/最大项目数限制以及唯一性要求,返回 true 或 WP_Error。

关键要点

  • 函数接受三个参数:$value(要验证的值)、$args(schema 数组)和 $param(参数名,用于错误消息)。
  • 验证步骤包括:检查是否为数组类型、使用 rest_sanitize_array() 清理数组、根据 schema['items'] 递归验证每个项目、检查 minItems 和 maxItems 限制、验证 uniqueItems 要求。
  • 返回 true 表示验证通过,否则返回 WP_Error 对象,包含错误代码和消息。

代码示例

function rest_validate_array_value_from_schema( $value, $args, $param ) {
    if ( ! rest_is_array( $value ) ) {
        return new WP_Error(
            'rest_invalid_type',
            sprintf( __( '%1$s is not of type %2$s.' ), $param, 'array' ),
            array( 'param' => $param )
        );
    }

    $value = rest_sanitize_array( $value );

    if ( isset( $args['items'] ) ) {
        foreach ( $value as $index => $v ) {
            $is_valid = rest_validate_value_from_schema( $v, $args['items'], $param . '[' . $index . ']' );
            if ( is_wp_error( $is_valid ) ) {
                return $is_valid;
            }
        }
    }

    if ( isset( $args['minItems'] ) && count( $value ) < $args['minItems'] ) {
        return new WP_Error(
            'rest_too_few_items',
            sprintf(
                _n(
                    '%1$s must contain at least %2$s item.',
                    '%1$s must contain at least %2$s items.',
                    $args['minItems']
                ),
                $param,
                number_format_i18n( $args['minItems'] )
            )
        );
    }

    if ( isset( $args['maxItems'] ) && count( $value ) > $args['maxItems'] ) {
        return new WP_Error(
            'rest_too_many_items',
            sprintf(
                _n(
                    '%1$s must contain at most %2$s item.',
                    '%1$s must contain at most %2$s items.',
                    $args['maxItems']
                ),
                $param,
                number_format_i18n( $args['maxItems'] )
            )
        );
    }

    if ( ! empty( $args['uniqueItems'] ) && ! rest_validate_array_contains_unique_items( $value ) ) {
        return new WP_Error( 'rest_duplicate_items', sprintf( __( '%s has duplicate items.' ), $param ) );
    }

    return true;
}

注意事项

  • 函数依赖于其他辅助函数如 rest_is_array()、rest_sanitize_array() 和 rest_validate_array_contains_unique_items(),确保在调用前这些函数可用。
  • 错误消息使用国际化函数 __() 和 _n(),支持多语言环境。
  • 在 WordPress 5.7.0 版本中引入,使用时需注意版本兼容性。

📄 原文内容

Validates an array value based on a schema.

Parameters

$valuemixedrequired
The value to validate.
$argsarrayrequired
Schema array to use for validation.
$paramstringrequired
The parameter name, used in error messages.

Return

true|WP_Error

Source

function rest_validate_array_value_from_schema( $value, $args, $param ) {
	if ( ! rest_is_array( $value ) ) {
		return new WP_Error(
			'rest_invalid_type',
			/* translators: 1: Parameter, 2: Type name. */
			sprintf( __( '%1$s is not of type %2$s.' ), $param, 'array' ),
			array( 'param' => $param )
		);
	}

	$value = rest_sanitize_array( $value );

	if ( isset( $args['items'] ) ) {
		foreach ( $value as $index => $v ) {
			$is_valid = rest_validate_value_from_schema( $v, $args['items'], $param . '[' . $index . ']' );
			if ( is_wp_error( $is_valid ) ) {
				return $is_valid;
			}
		}
	}

	if ( isset( $args['minItems'] ) && count( $value ) < $args['minItems'] ) {
		return new WP_Error(
			'rest_too_few_items',
			sprintf(
				/* translators: 1: Parameter, 2: Number. */
				_n(
					'%1$s must contain at least %2$s item.',
					'%1$s must contain at least %2$s items.',
					$args['minItems']
				),
				$param,
				number_format_i18n( $args['minItems'] )
			)
		);
	}

	if ( isset( $args['maxItems'] ) && count( $value ) > $args['maxItems'] ) {
		return new WP_Error(
			'rest_too_many_items',
			sprintf(
				/* translators: 1: Parameter, 2: Number. */
				_n(
					'%1$s must contain at most %2$s item.',
					'%1$s must contain at most %2$s items.',
					$args['maxItems']
				),
				$param,
				number_format_i18n( $args['maxItems'] )
			)
		);
	}

	if ( ! empty( $args['uniqueItems'] ) && ! rest_validate_array_contains_unique_items( $value ) ) {
		/* translators: %s: Parameter. */
		return new WP_Error( 'rest_duplicate_items', sprintf( __( '%s has duplicate items.' ), $param ) );
	}

	return true;
}

Changelog

Version Description
5.7.0 Introduced.