On this page
rest_validate_enum( mixed $value, array $args, string $param ): true|WP_Error
Validates that the given value is a member of the JSON Schema “enum”.
Parameters
$valuemixed Required-
The value to validate.
$argsarray Required-
The schema array to use.
$paramstring Required-
The parameter name, used in error messages.
Return
true|WP_Error True if the "enum" contains the value or a WP_Error instance otherwise.
Source
File: wp-includes/rest-api.php. View all references
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 ) );
}
Related
Uses
| Uses | Description |
|---|---|
| rest_are_values_equal() wp-includes/rest-api.php | Checks the equality of two values, following JSON Schema semantics. |
| rest_sanitize_value_from_schema() wp-includes/rest-api.php | Sanitize a value based on a schema. |
| wp_sprintf() wp-includes/formatting.php | WordPress implementation of PHP sprintf() with filters. |
| wp_json_encode() wp-includes/functions.php | Encodes a variable into JSON, with some sanity checks. |
| __() wp-includes/l10n.php | Retrieves the translation of $text. |
| is_wp_error() wp-includes/load.php | Checks whether the given variable is a WordPress Error. |
| WP_Error::__construct() wp-includes/class-wp-error.php | Initializes the error. |
Used By
| Used By | Description |
|---|---|
| rest_validate_value_from_schema() wp-includes/rest-api.php | Validate a value based on a schema. |
Changelog
| Version | Description |
|---|---|
| 5.7.0 | Introduced. |
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/rest_validate_enum