rest_validate_request_arg

Validate a request argument based on details registered to the route.

Description

(WP_Error|boolean) rest_validate_request_arg( (mixed) $value, (WP_REST_Request) $request, (string) $param ); 

Returns (WP_Error|boolean)

Parameters (3)

0. $value (mixed)
The value.
1. $request (WP_REST_Request)
The request.
2. $param (string)
The param.

Usage

  1. if ( !function_exists( 'rest_validate_request_arg' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'woocommerce/includes/vendor/wp-rest-functions.php'; 
  3.  
  4. // The value. 
  5. $value = null; 
  6.  
  7. // The request. 
  8. $request = null; 
  9.  
  10. // The param. 
  11. $param = ''; 
  12.  
  13. // NOTICE! Understand what this does before running. 
  14. $result = rest_validate_request_arg($value, $request, $param); 
  15.  

Defined (1)

The function is defined in the following location(s).

/includes/vendor/wp-rest-functions.php  
  1. function rest_validate_request_arg( $value, $request, $param ) { 
  2.  
  3. $attributes = $request->get_attributes(); 
  4. if ( ! isset( $attributes['args'][ $param ] ) || ! is_array( $attributes['args'][ $param ] ) ) { 
  5. return true; 
  6. $args = $attributes['args'][ $param ]; 
  7.  
  8. if ( ! empty( $args['enum'] ) ) { 
  9. if ( ! in_array( $value, $args['enum'] ) ) { 
  10. /** translators: 1: parameter 2: arguments */ 
  11. return new WP_Error( 'rest_invalid_param', sprintf( __( '%1$s is not one of %2$s', woocommerce ), $param, implode( ', ', $args['enum'] ) ) ); 
  12.  
  13. if ( 'integer' === $args['type'] && ! is_numeric( $value ) ) { 
  14. /** translators: 1: parameter 2: integer type */ 
  15. return new WP_Error( 'rest_invalid_param', sprintf( __( '%1$s is not of type %2$s', woocommerce ), $param, 'integer' ) ); 
  16.  
  17. if ( 'boolean' === $args['type'] && ! rest_is_boolean( $value ) ) { 
  18. /** translators: 1: parameter 2: boolean type */ 
  19. return new WP_Error( 'rest_invalid_param', sprintf( __( '%1$s is not of type %2$s', woocommerce ), $value, 'boolean' ) ); 
  20.  
  21. if ( 'string' === $args['type'] && ! is_string( $value ) ) { 
  22. /** translators: 1: parameter 2: string type */ 
  23. return new WP_Error( 'rest_invalid_param', sprintf( __( '%1$s is not of type %2$s', woocommerce ), $param, 'string' ) ); 
  24.  
  25. if ( isset( $args['format'] ) ) { 
  26. switch ( $args['format'] ) { 
  27. case 'date-time' : 
  28. if ( ! rest_parse_date( $value ) ) { 
  29. return new WP_Error( 'rest_invalid_date', __( 'The date you provided is invalid.', woocommerce ) ); 
  30. break; 
  31.  
  32. case 'email' : 
  33. if ( ! is_email( $value ) ) { 
  34. return new WP_Error( 'rest_invalid_email', __( 'The email address you provided is invalid.', woocommerce ) ); 
  35. break; 
  36. case 'ipv4' : 
  37. if ( ! rest_is_ip_address( $value ) ) { 
  38. /** translators: %s: IP address */ 
  39. return new WP_Error( 'rest_invalid_param', sprintf( __( '%s is not a valid IP address.', woocommerce ), $value ) ); 
  40. break; 
  41.  
  42. if ( in_array( $args['type'], array( 'numeric', 'integer' ) ) && ( isset( $args['minimum'] ) || isset( $args['maximum'] ) ) ) { 
  43. if ( isset( $args['minimum'] ) && ! isset( $args['maximum'] ) ) { 
  44. if ( ! empty( $args['exclusiveMinimum'] ) && $value <= $args['minimum'] ) { 
  45. return new WP_Error( 'rest_invalid_param', sprintf( __( '%1$s must be greater than %2$d (exclusive)', woocommerce ), $param, $args['minimum'] ) ); 
  46. } elseif ( empty( $args['exclusiveMinimum'] ) && $value < $args['minimum'] ) { 
  47. return new WP_Error( 'rest_invalid_param', sprintf( __( '%1$s must be greater than %2$d (inclusive)', woocommerce ), $param, $args['minimum'] ) ); 
  48. } elseif ( isset( $args['maximum'] ) && ! isset( $args['minimum'] ) ) { 
  49. if ( ! empty( $args['exclusiveMaximum'] ) && $value >= $args['maximum'] ) { 
  50. return new WP_Error( 'rest_invalid_param', sprintf( __( '%1$s must be less than %2$d (exclusive)', woocommerce ), $param, $args['maximum'] ) ); 
  51. } elseif ( empty( $args['exclusiveMaximum'] ) && $value > $args['maximum'] ) { 
  52. return new WP_Error( 'rest_invalid_param', sprintf( __( '%1$s must be less than %2$d (inclusive)', woocommerce ), $param, $args['maximum'] ) ); 
  53. } elseif ( isset( $args['maximum'] ) && isset( $args['minimum'] ) ) { 
  54. if ( ! empty( $args['exclusiveMinimum'] ) && ! empty( $args['exclusiveMaximum'] ) ) { 
  55. if ( $value >= $args['maximum'] || $value <= $args['minimum'] ) { 
  56. return new WP_Error( 'rest_invalid_param', sprintf( __( '%1$s must be between %2$d (exclusive) and %3$d (exclusive)', woocommerce ), $param, $args['minimum'], $args['maximum'] ) ); 
  57. } elseif ( empty( $args['exclusiveMinimum'] ) && ! empty( $args['exclusiveMaximum'] ) ) { 
  58. if ( $value >= $args['maximum'] || $value < $args['minimum'] ) { 
  59. return new WP_Error( 'rest_invalid_param', sprintf( __( '%1$s must be between %2$d (inclusive) and %3$d (exclusive)', woocommerce ), $param, $args['minimum'], $args['maximum'] ) ); 
  60. } elseif ( ! empty( $args['exclusiveMinimum'] ) && empty( $args['exclusiveMaximum'] ) ) { 
  61. if ( $value > $args['maximum'] || $value <= $args['minimum'] ) { 
  62. return new WP_Error( 'rest_invalid_param', sprintf( __( '%1$s must be between %2$d (exclusive) and %3$d (inclusive)', woocommerce ), $param, $args['minimum'], $args['maximum'] ) ); 
  63. } elseif ( empty( $args['exclusiveMinimum'] ) && empty( $args['exclusiveMaximum'] ) ) { 
  64. if ( $value > $args['maximum'] || $value < $args['minimum'] ) { 
  65. return new WP_Error( 'rest_invalid_param', sprintf( __( '%1$s must be between %2$d (inclusive) and %3$d (inclusive)', woocommerce ), $param, $args['minimum'], $args['maximum'] ) ); 
  66.  
  67. return true;