rest_validate_enum
函数
rest_validate_enum ( $value, $args, $param )
- 参数
-
-
(mixed)
$value
The value to validate.- Required: 是
-
(array)
$args
The schema array to use.- Required: 是
-
(string)
$param
The parameter name, used in error messages.- Required: 是
-
(mixed)
- 返回值
-
- (true|WP_Error) True if the “enum” contains the value or a WP_Error instance otherwise.
- 定义位置
-
-
wp-includes/rest-api.php
, line 2004
-
wp-includes/rest-api.php
- 引入
- 5.7.0
- 弃用
- –
Validates that the given value is a member of the JSON Schema “enum”.
function rest_validate_enum( $value, $args, $param ) { $sanitized_value = rest_sanitize_value_from_schema( $value, $args, $param ); if ( is_wp_error( $sanitized_value ) ) { return $sanitized_value; } foreach ( $args['enum'] as $enum_value ) { if ( rest_are_values_equal( $sanitized_value, $enum_value ) ) { return true; } } $encoded_enum_values = array(); foreach ( $args['enum'] as $enum_value ) { $encoded_enum_values[] = is_scalar( $enum_value ) ? $enum_value : wp_json_encode( $enum_value ); } if ( count( $encoded_enum_values ) === 1 ) { /* translators: 1: Parameter, 2: Valid values. */ return new WP_Error( 'rest_not_in_enum', wp_sprintf( __( '%1$s is not %2$s.' ), $param, $encoded_enum_values[0] ) ); } /* translators: 1: Parameter, 2: List of valid values. */ return new WP_Error( 'rest_not_in_enum', wp_sprintf( __( '%1$s is not one of %2$l.' ), $param, $encoded_enum_values ) ); }
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。