rest_validate_string_value_from_schema()
云策文档标注
概述
rest_validate_string_value_from_schema() 是一个 WordPress REST API 函数,用于根据 JSON Schema 验证字符串值。它检查值是否为字符串类型,并可选地验证最小长度、最大长度和正则表达式模式。
关键要点
- 函数接受三个参数:$value(要验证的值)、$args(包含验证规则的 schema 数组)和 $param(参数名,用于错误消息)。
- 验证包括类型检查(必须是字符串)、最小长度(minLength)、最大长度(maxLength)和正则表达式模式(pattern)。
- 如果验证失败,函数返回一个 WP_Error 对象,包含错误代码和本地化消息;否则返回 true。
代码示例
function rest_validate_string_value_from_schema( $value, $args, $param ) {
if ( ! is_string( $value ) ) {
return new WP_Error(
'rest_invalid_type',
sprintf( __( '%1$s is not of type %2$s.' ), $param, 'string' ),
array( 'param' => $param )
);
}
if ( isset( $args['minLength'] ) && mb_strlen( $value ) < $args['minLength'] ) {
return new WP_Error(
'rest_too_short',
sprintf(
_n(
'%1$s must be at least %2$s character long.',
'%1$s must be at least %2$s characters long.',
$args['minLength']
),
$param,
number_format_i18n( $args['minLength'] )
)
);
}
if ( isset( $args['maxLength'] ) && mb_strlen( $value ) > $args['maxLength'] ) {
return new WP_Error(
'rest_too_long',
sprintf(
_n(
'%1$s must be at most %2$s character long.',
'%1$s must be at most %2$s characters long.',
$args['maxLength']
),
$param,
number_format_i18n( $args['maxLength'] )
)
);
}
if ( isset( $args['pattern'] ) && ! rest_validate_json_schema_pattern( $args['pattern'], $value ) ) {
return new WP_Error(
'rest_invalid_pattern',
sprintf( __( '%1$s does not match pattern %2$s.' ), $param, $args['pattern'] )
);
}
return true;
}注意事项
- 此函数在 WordPress 5.7.0 版本中引入,是 REST API 验证系统的一部分。
- 它依赖于其他函数如 rest_validate_json_schema_pattern() 进行模式验证,并使用了本地化函数如 __() 和 _n() 以支持多语言。
- 开发者应确保 $args 数组正确设置验证规则,例如 minLength、maxLength 和 pattern,以避免验证错误。
原文内容
Validates a string 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.
Source
function rest_validate_string_value_from_schema( $value, $args, $param ) {
if ( ! is_string( $value ) ) {
return new WP_Error(
'rest_invalid_type',
/* translators: 1: Parameter, 2: Type name. */
sprintf( __( '%1$s is not of type %2$s.' ), $param, 'string' ),
array( 'param' => $param )
);
}
if ( isset( $args['minLength'] ) && mb_strlen( $value ) < $args['minLength'] ) {
return new WP_Error(
'rest_too_short',
sprintf(
/* translators: 1: Parameter, 2: Number of characters. */
_n(
'%1$s must be at least %2$s character long.',
'%1$s must be at least %2$s characters long.',
$args['minLength']
),
$param,
number_format_i18n( $args['minLength'] )
)
);
}
if ( isset( $args['maxLength'] ) && mb_strlen( $value ) > $args['maxLength'] ) {
return new WP_Error(
'rest_too_long',
sprintf(
/* translators: 1: Parameter, 2: Number of characters. */
_n(
'%1$s must be at most %2$s character long.',
'%1$s must be at most %2$s characters long.',
$args['maxLength']
),
$param,
number_format_i18n( $args['maxLength'] )
)
);
}
if ( isset( $args['pattern'] ) && ! rest_validate_json_schema_pattern( $args['pattern'], $value ) ) {
return new WP_Error(
'rest_invalid_pattern',
/* translators: 1: Parameter, 2: Pattern. */
sprintf( __( '%1$s does not match pattern %2$s.' ), $param, $args['pattern'] )
);
}
return true;
}
Changelog
| Version | Description |
|---|---|
| 5.7.0 | Introduced. |