rest_validate_enum( mixed $value, array $args, string $param ): true|WP_Error
- Since
- 5.7.0
- Source
wp-includes/rest-api.php:2073
Compatibility
- WordPress
- since 5.7.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$valuemixed- The value to validate.
$argsarray- The schema array to use.
$paramstring- The parameter name, used in error messages.
Return value
true|WP_Error- True if the "enum" contains the value or a WP_Error instance otherwise.
Performance profile
How much work a call to rest_validate_enum() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Light
- Scaling
- Scales with input
- Instructions
- 14–40
- Plugin surface
- None
- Called by
- 1
Touches nothing outside its own arguments.
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 72.
Nothing here hands control to plugin code.
1 place in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
do_action()one call below rest_validate_enum()
Further down the call graph this can also reach option, cache, serialize, query and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 3 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost rest_validate_enum() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
is_wp_error() | 14 | rest_sanitize_value_from_schema(), is_wp_error() |
!is_wp_error() && !$args && rest_are_values_equal() | 23 | rest_sanitize_value_from_schema(), is_wp_error(), rest_are_values_equal() |
!is_wp_error() | 36–40 | rest_sanitize_value_from_schema(), is_wp_error(), __(), wp_sprintf() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 71 | 14–40 | 8 | 1 fewer instruction than PHP 8.5 |
| 8.5 | 72 | 14–40 | 8 | |
| 8.4 | 72 | 14–40 | 8 | |
| 8.3 | 72 | 14–40 | 8 | |
| 8.2 | 72 | 14–40 | 8 | |
| 8.1 | 72 | 14–40 | 8 | 2 fewer instructions than PHP 7.4 |
| 7.4 | 74 | 14–40 | 8 |
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Uses · 7
- rest_sanitize_value_from_schema()Sanitize a value based on a schema.
- is_wp_error()Checks whether the given variable is a WordPress Error.
- rest_are_values_equal()Checks the equality of two values, following JSON Schema semantics.
- wp_json_encode()Encodes a variable into JSON, with some confidence checks.
- wp_sprintf()WordPress' implementation of PHP sprintf() with filters.
- __()Retrieves the translation of $text.
- WP_Error::__construct()Initializes the error.
Used by · 1
- rest_validate_value_from_schema()Validate a value based on a schema.
Source code
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 ) );}Changelog
Introduced in 5.7.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.7.7 tag, from
src/wp-includes/rest-api.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.