wppaste
WordPress

rest_validate_number_value_from_schema( mixed $value, array $args, string $param ): true|WP_Error

Since
5.7.0
Source
wp-includes/rest-api.php:2557
Validates a number value based on a schema.

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
Schema array to use for validation.
$paramstring
The parameter name, used in error messages.

Return value

true|WP_Error

Performance profile

How much work a call to rest_validate_number_value_from_schema() 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
Trivial

Touches nothing outside its own arguments.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
14–92

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 237.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
2

2 places in core call this, so the cost is paid more often than your own code shows.

What one call costs · 2 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost rest_validate_number_value_from_schema() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
$value && !isset($args)14–77none
$value32–92__(), sprintf()

This body has more branch combinations than are worth enumerating, so the table covers the outcomes found first rather than every one that exists.

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev23714–9233
8.523714–9233
8.423714–92332 fewer instructions than PHP 8.3
8.323916–9433
8.223916–9433
8.123916–9433
7.423916–9433

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 · 2

Used by · 2

Source code

function rest_validate_number_value_from_schema( $value, $args, $param ) {	if ( ! is_numeric( $value ) ) {		return new WP_Error(			'rest_invalid_type',			/* translators: 1: Parameter, 2: Type name. */			sprintf( __( '%1$s is not of type %2$s.' ), $param, $args['type'] ),			array( 'param' => $param )		);	} 	if ( isset( $args['multipleOf'] ) && fmod( $value, $args['multipleOf'] ) !== 0.0 ) {		return new WP_Error(			'rest_invalid_multiple',			/* translators: 1: Parameter, 2: Multiplier. */			sprintf( __( '%1$s must be a multiple of %2$s.' ), $param, $args['multipleOf'] )		);	} 	if ( isset( $args['minimum'] ) && ! isset( $args['maximum'] ) ) {		if ( ! empty( $args['exclusiveMinimum'] ) && $value <= $args['minimum'] ) {			return new WP_Error(				'rest_out_of_bounds',				/* translators: 1: Parameter, 2: Minimum number. */				sprintf( __( '%1$s must be greater than %2$d' ), $param, $args['minimum'] )			);		} 		if ( empty( $args['exclusiveMinimum'] ) && $value < $args['minimum'] ) {			return new WP_Error(				'rest_out_of_bounds',				/* translators: 1: Parameter, 2: Minimum number. */				sprintf( __( '%1$s must be greater than or equal to %2$d' ), $param, $args['minimum'] )			);		}	} 	if ( isset( $args['maximum'] ) && ! isset( $args['minimum'] ) ) {		if ( ! empty( $args['exclusiveMaximum'] ) && $value >= $args['maximum'] ) {			return new WP_Error(				'rest_out_of_bounds',				/* translators: 1: Parameter, 2: Maximum number. */				sprintf( __( '%1$s must be less than %2$d' ), $param, $args['maximum'] )			);		} 		if ( empty( $args['exclusiveMaximum'] ) && $value > $args['maximum'] ) {			return new WP_Error(				'rest_out_of_bounds',				/* translators: 1: Parameter, 2: Maximum number. */				sprintf( __( '%1$s must be less than or equal to %2$d' ), $param, $args['maximum'] )			);		}	} 	if ( isset( $args['minimum'], $args['maximum'] ) ) {		if ( ! empty( $args['exclusiveMinimum'] ) && ! empty( $args['exclusiveMaximum'] ) ) {			if ( $value >= $args['maximum'] || $value <= $args['minimum'] ) {				return new WP_Error(					'rest_out_of_bounds',					sprintf(						/* translators: 1: Parameter, 2: Minimum number, 3: Maximum number. */						__( '%1$s must be between %2$d (exclusive) and %3$d (exclusive)' ),						$param,						$args['minimum'],						$args['maximum']					)				);			}		} 		if ( ! empty( $args['exclusiveMinimum'] ) && empty( $args['exclusiveMaximum'] ) ) {			if ( $value > $args['maximum'] || $value <= $args['minimum'] ) {				return new WP_Error(					'rest_out_of_bounds',					sprintf(						/* translators: 1: Parameter, 2: Minimum number, 3: Maximum number. */						__( '%1$s must be between %2$d (exclusive) and %3$d (inclusive)' ),						$param,						$args['minimum'],						$args['maximum']

Changelog

Introduced in 5.7.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 6.9.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.