: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
* @param mixed $value1 The first value to check.
* @param mixed $value2 The second value to check.
* @return bool True if the values are equal or false otherwise.
function rest_are_values_equal( $value1, $value2 ) {
if ( is_array( $value1 ) && is_array( $value2 ) ) {
if ( count( $value1 ) !== count( $value2 ) ) {
foreach ( $value1 as $index => $value ) {
if ( ! array_key_exists( $index, $value2 ) || ! rest_are_values_equal( $value, $value2[ $index ] ) ) {
if ( is_int( $value1 ) && is_float( $value2 )
|| is_float( $value1 ) && is_int( $value2 )
return (float) $value1 === (float) $value2;
return $value1 === $value2;
* Validates that the given value is a member of the JSON Schema "enum".
* @param mixed $value The value to validate.
* @param array $args The schema array to use.
* @param string $param The parameter name, used in error messages.
* @return true|WP_Error True if the "enum" contains the value or a WP_Error instance otherwise.
function rest_validate_enum( $value, $args, $param ) {
$sanitized_value = rest_sanitize_value_from_schema( $value, $args, $param );
if ( is_wp_error( $sanitized_value ) ) {
foreach ( $args['enum'] as $enum_value ) {
if ( rest_are_values_equal( $sanitized_value, $enum_value ) ) {
$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 ) );
* Get all valid JSON schema properties.
* @return string[] All valid JSON schema properties.
function rest_get_allowed_schema_keywords() {
* Validate a value based on a schema.
* @since 4.9.0 Support the "object" type.
* @since 5.2.0 Support validating "additionalProperties" against a schema.
* @since 5.3.0 Support multiple types.
* @since 5.4.0 Convert an empty string to an empty object.
* @since 5.5.0 Add the "uuid" and "hex-color" formats.
* Support the "minLength", "maxLength" and "pattern" keywords for strings.
* Support the "minItems", "maxItems" and "uniqueItems" keywords for arrays.
* Validate required properties.
* @since 5.6.0 Support the "minProperties" and "maxProperties" keywords for objects.
* Support the "multipleOf" keyword for numbers and integers.
* Support the "patternProperties" keyword for objects.
* Support the "anyOf" and "oneOf" keywords.
* @param mixed $value The value to validate.
* @param array $args Schema array to use for validation.
* @param string $param The parameter name, used in error messages.
function rest_validate_value_from_schema( $value, $args, $param = '' ) {
if ( isset( $args['anyOf'] ) ) {
$matching_schema = rest_find_any_matching_schema( $value, $args, $param );
if ( is_wp_error( $matching_schema ) ) {
if ( ! isset( $args['type'] ) && isset( $matching_schema['type'] ) ) {
$args['type'] = $matching_schema['type'];
if ( isset( $args['oneOf'] ) ) {
$matching_schema = rest_find_one_matching_schema( $value, $args, $param );
if ( is_wp_error( $matching_schema ) ) {
if ( ! isset( $args['type'] ) && isset( $matching_schema['type'] ) ) {
$args['type'] = $matching_schema['type'];
$allowed_types = array( 'array', 'object', 'string', 'number', 'integer', 'boolean', 'null' );
if ( ! isset( $args['type'] ) ) {
/* translators: %s: Parameter. */
_doing_it_wrong( __FUNCTION__, sprintf( __( 'The "type" schema keyword for %s is required.' ), $param ), '5.5.0' );
if ( is_array( $args['type'] ) ) {
$best_type = rest_handle_multi_type_schema( $value, $args, $param );
/* translators: 1: Parameter, 2: List of types. */
sprintf( __( '%1$s is not of type %2$s.' ), $param, implode( ',', $args['type'] ) ),
array( 'param' => $param )
$args['type'] = $best_type;
if ( ! in_array( $args['type'], $allowed_types, true ) ) {
/* translators: 1: Parameter, 2: The list of allowed types. */
wp_sprintf( __( 'The "type" schema keyword for %1$s can only be one of the built-in types: %2$l.' ), $param, $allowed_types ),
switch ( $args['type'] ) {
$is_valid = rest_validate_null_value_from_schema( $value, $param );
$is_valid = rest_validate_boolean_value_from_schema( $value, $param );
$is_valid = rest_validate_object_value_from_schema( $value, $args, $param );
$is_valid = rest_validate_array_value_from_schema( $value, $args, $param );
$is_valid = rest_validate_number_value_from_schema( $value, $args, $param );
$is_valid = rest_validate_string_value_from_schema( $value, $args, $param );
$is_valid = rest_validate_integer_value_from_schema( $value, $args, $param );
if ( is_wp_error( $is_valid ) ) {
if ( ! empty( $args['enum'] ) ) {
$enum_contains_value = rest_validate_enum( $value, $args, $param );
if ( is_wp_error( $enum_contains_value ) ) {
return $enum_contains_value;
* The "format" keyword should only be applied to strings. However, for backward compatibility,
* we allow the "format" keyword if the type keyword was not specified, or was set to an invalid value.
if ( isset( $args['format'] )
&& ( ! isset( $args['type'] ) || 'string' === $args['type'] || ! in_array( $args['type'], $allowed_types, true ) )
switch ( $args['format'] ) {
if ( ! rest_parse_hex_color( $value ) ) {
return new WP_Error( 'rest_invalid_hex_color', __( 'Invalid hex color.' ) );
if ( ! rest_parse_date( $value ) ) {
return new WP_Error( 'rest_invalid_date', __( 'Invalid date.' ) );
if ( ! is_email( $value ) ) {
return new WP_Error( 'rest_invalid_email', __( 'Invalid email address.' ) );
if ( ! rest_is_ip_address( $value ) ) {
/* translators: %s: IP address. */
return new WP_Error( 'rest_invalid_ip', sprintf( __( '%s is not a valid IP address.' ), $param ) );
if ( ! wp_is_uuid( $value ) ) {
/* translators: %s: The name of a JSON field expecting a valid UUID. */
return new WP_Error( 'rest_invalid_uuid', sprintf( __( '%s is not a valid UUID.' ), $param ) );
* Validates a null value based on a schema.
* @param mixed $value The value to validate.
* @param string $param The parameter name, used in error messages.
function rest_validate_null_value_from_schema( $value, $param ) {
/* translators: 1: Parameter, 2: Type name. */
sprintf( __( '%1$s is not of type %2$s.' ), $param, 'null' ),
array( 'param' => $param )
* Validates a boolean value based on a schema.
* @param mixed $value The value to validate.
* @param string $param The parameter name, used in error messages.
function rest_validate_boolean_value_from_schema( $value, $param ) {
if ( ! rest_is_boolean( $value ) ) {
/* translators: 1: Parameter, 2: Type name. */
sprintf( __( '%1$s is not of type %2$s.' ), $param, 'boolean' ),
array( 'param' => $param )
* Validates an object value based on a schema.
* @param mixed $value The value to validate.
* @param array $args Schema array to use for validation.
* @param string $param The parameter name, used in error messages.
function rest_validate_object_value_from_schema( $value, $args, $param ) {
if ( ! rest_is_object( $value ) ) {
/* translators: 1: Parameter, 2: Type name. */
sprintf( __( '%1$s is not of type %2$s.' ), $param, 'object' ),
array( 'param' => $param )
$value = rest_sanitize_object( $value );
if ( isset( $args['required'] ) && is_array( $args['required'] ) ) { // schema version 4
foreach ( $args['required'] as $name ) {
if ( ! array_key_exists( $name, $value ) ) {
'rest_property_required',
/* translators: 1: Property of an object, 2: Parameter. */
sprintf( __( '%1$s is a required property of %2$s.' ), $name, $param )
} elseif ( isset( $args['properties'] ) ) { // schema version 3
foreach ( $args['properties'] as $name => $property ) {
if ( isset( $property['required'] ) && true === $property['required'] && ! array_key_exists( $name, $value ) ) {
'rest_property_required',
/* translators: 1: Property of an object, 2: Parameter. */
sprintf( __( '%1$s is a required property of %2$s.' ), $name, $param )
foreach ( $value as $property => $v ) {
if ( isset( $args['properties'][ $property ] ) ) {
$is_valid = rest_validate_value_from_schema( $v, $args['properties'][ $property ], $param . '[' . $property . ']' );
if ( is_wp_error( $is_valid ) ) {
$pattern_property_schema = rest_find_matching_pattern_property_schema( $property, $args );
if ( null !== $pattern_property_schema ) {
$is_valid = rest_validate_value_from_schema( $v, $pattern_property_schema, $param . '[' . $property . ']' );
if ( is_wp_error( $is_valid ) ) {
if ( isset( $args['additionalProperties'] ) ) {
if ( false === $args['additionalProperties'] ) {
'rest_additional_properties_forbidden',
/* translators: %s: Property of an object. */
sprintf( __( '%1$s is not a valid property of Object.' ), $property )
if ( is_array( $args['additionalProperties'] ) ) {
$is_valid = rest_validate_value_from_schema( $v, $args['additionalProperties'], $param . '[' . $property . ']' );
if ( is_wp_error( $is_valid ) ) {
if ( isset( $args['minProperties'] ) && count( $value ) < $args['minProperties'] ) {
'rest_too_few_properties',
/* translators: 1: Parameter, 2: Number. */
'%1$s must contain at least %2$s property.',
'%1$s must contain at least %2$s properties.',
number_format_i18n( $args['minProperties'] )
if ( isset( $args['maxProperties'] ) && count( $value ) > $args['maxProperties'] ) {
'rest_too_many_properties',
/* translators: 1: Parameter, 2: Number. */
'%1$s must contain at most %2$s property.',
'%1$s must contain at most %2$s properties.',
number_format_i18n( $args['maxProperties'] )
* Validates an array value based on a schema.
* @param mixed $value The value to validate.
* @param array $args Schema array to use for validation.
* @param string $param The parameter name, used in error messages.
function rest_validate_array_value_from_schema( $value, $args, $param ) {
if ( ! rest_is_array( $value ) ) {
/* translators: 1: Parameter, 2: Type name. */
sprintf( __( '%1$s is not of type %2$s.' ), $param, 'array' ),
array( 'param' => $param )
$value = rest_sanitize_array( $value );
if ( isset( $args['items'] ) ) {
foreach ( $value as $index => $v ) {
$is_valid = rest_validate_value_from_schema( $v, $args['items'], $param . '[' . $index . ']' );
if ( is_wp_error( $is_valid ) ) {
if ( isset( $args['minItems'] ) && count( $value ) < $args['minItems'] ) {
/* translators: 1: Parameter, 2: Number. */
'%1$s must contain at least %2$s item.',
'%1$s must contain at least %2$s items.',
number_format_i18n( $args['minItems'] )
if ( isset( $args['maxItems'] ) && count( $value ) > $args['maxItems'] ) {
/* translators: 1: Parameter, 2: Number. */
'%1$s must contain at most %2$s item.',
'%1$s must contain at most %2$s items.',
number_format_i18n( $args['maxItems'] )
if ( ! empty( $args['uniqueItems'] ) && ! rest_validate_array_contains_unique_items( $value ) ) {
/* translators: %s: Parameter. */
return new WP_Error( 'rest_duplicate_items', sprintf( __( '%s has duplicate items.' ), $param ) );
* Validates a number value based on a schema.
* @param mixed $value The value to validate.
* @param array $args Schema array to use for validation.
* @param string $param The parameter name, used in error messages.