rest_validate_value_from_schema

Validate a value based on a schema.

Description

(true|WP_Error) rest_validate_value_from_schema( (mixed) $value, (array) $args, (string) $param = '' ); 

Returns (true|WP_Error)

Parameters (3)

0. $value (mixed)
The value to validate.
1. $args (array)
Schema array to use for validation.
2. $param — Optional. (string) => ''
The parameter name, used in error messages.

Usage

  1. if ( !function_exists( 'rest_validate_value_from_schema' ) ) { 
  2. require_once ABSPATH . WPINC . '/rest-api.php'; 
  3.  
  4. // The value to validate. 
  5. $value = null; 
  6.  
  7. // Schema array to use for validation. 
  8. $args = array(); 
  9.  
  10. // The parameter name, used in error messages. 
  11. $param = ''; 
  12.  
  13. // NOTICE! Understand what this does before running. 
  14. $result = rest_validate_value_from_schema($value, $args, $param); 
  15.  

Defined (1)

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

/wp-includes/rest-api.php  
  1. function rest_validate_value_from_schema( $value, $args, $param = '' ) { 
  2. if ( 'array' === $args['type'] ) { 
  3. if ( ! is_array( $value ) ) { 
  4. $value = preg_split( '/[\s, ]+/', $value ); 
  5. if ( ! wp_is_numeric_array( $value ) ) { 
  6. /** translators: 1: parameter, 2: type name */ 
  7. return new WP_Error( 'rest_invalid_param', sprintf( __( '%1$s is not of type %2$s.' ), $param, 'array' ) ); 
  8. foreach ( $value as $index => $v ) { 
  9. $is_valid = rest_validate_value_from_schema( $v, $args['items'], $param . '[' . $index . ']' ); 
  10. if ( is_wp_error( $is_valid ) ) { 
  11. return $is_valid; 
  12. if ( ! empty( $args['enum'] ) ) { 
  13. if ( ! in_array( $value, $args['enum'], true ) ) { 
  14. /** translators: 1: parameter, 2: list of valid values */ 
  15. return new WP_Error( 'rest_invalid_param', sprintf( __( '%1$s is not one of %2$s.' ), $param, implode( ', ', $args['enum'] ) ) ); 
  16.  
  17. if ( in_array( $args['type'], array( 'integer', 'number' ) ) && ! is_numeric( $value ) ) { 
  18. /** translators: 1: parameter, 2: type name */ 
  19. return new WP_Error( 'rest_invalid_param', sprintf( __( '%1$s is not of type %2$s.' ), $param, $args['type'] ) ); 
  20.  
  21. if ( 'integer' === $args['type'] && round( floatval( $value ) ) !== floatval( $value ) ) { 
  22. /** translators: 1: parameter, 2: type name */ 
  23. return new WP_Error( 'rest_invalid_param', sprintf( __( '%1$s is not of type %2$s.' ), $param, 'integer' ) ); 
  24.  
  25. if ( 'boolean' === $args['type'] && ! rest_is_boolean( $value ) ) { 
  26. /** translators: 1: parameter, 2: type name */ 
  27. return new WP_Error( 'rest_invalid_param', sprintf( __( '%1$s is not of type %2$s.' ), $value, 'boolean' ) ); 
  28.  
  29. if ( 'string' === $args['type'] && ! is_string( $value ) ) { 
  30. /** translators: 1: parameter, 2: type name */ 
  31. return new WP_Error( 'rest_invalid_param', sprintf( __( '%1$s is not of type %2$s.' ), $param, 'string' ) ); 
  32.  
  33. if ( isset( $args['format'] ) ) { 
  34. switch ( $args['format'] ) { 
  35. case 'date-time' : 
  36. if ( ! rest_parse_date( $value ) ) { 
  37. return new WP_Error( 'rest_invalid_date', __( 'Invalid date.' ) ); 
  38. break; 
  39.  
  40. case 'email' : 
  41. // is_email() checks for 3 characters (a@b), but 
  42. // wp_handle_comment_submission() requires 6 characters (a@b.co) 
  43. // 
  44. // https://core.trac.wordpress.org/ticket/38506 
  45. if ( ! is_email( $value ) || strlen( $value ) < 6 ) { 
  46. return new WP_Error( 'rest_invalid_email', __( 'Invalid email address.' ) ); 
  47. break; 
  48. case 'ip' : 
  49. if ( ! rest_is_ip_address( $value ) ) { 
  50. /** translators: %s: IP address */ 
  51. return new WP_Error( 'rest_invalid_param', sprintf( __( '%s is not a valid IP address.' ), $value ) ); 
  52. break; 
  53.  
  54. if ( in_array( $args['type'], array( 'number', 'integer' ), true ) && ( isset( $args['minimum'] ) || isset( $args['maximum'] ) ) ) { 
  55. if ( isset( $args['minimum'] ) && ! isset( $args['maximum'] ) ) { 
  56. if ( ! empty( $args['exclusiveMinimum'] ) && $value <= $args['minimum'] ) { 
  57. /** translators: 1: parameter, 2: minimum number */ 
  58. return new WP_Error( 'rest_invalid_param', sprintf( __( '%1$s must be greater than %2$d (exclusive)' ), $param, $args['minimum'] ) ); 
  59. } elseif ( empty( $args['exclusiveMinimum'] ) && $value < $args['minimum'] ) { 
  60. /** translators: 1: parameter, 2: minimum number */ 
  61. return new WP_Error( 'rest_invalid_param', sprintf( __( '%1$s must be greater than %2$d (inclusive)' ), $param, $args['minimum'] ) ); 
  62. } elseif ( isset( $args['maximum'] ) && ! isset( $args['minimum'] ) ) { 
  63. if ( ! empty( $args['exclusiveMaximum'] ) && $value >= $args['maximum'] ) { 
  64. /** translators: 1: parameter, 2: maximum number */ 
  65. return new WP_Error( 'rest_invalid_param', sprintf( __( '%1$s must be less than %2$d (exclusive)' ), $param, $args['maximum'] ) ); 
  66. } elseif ( empty( $args['exclusiveMaximum'] ) && $value > $args['maximum'] ) { 
  67. /** translators: 1: parameter, 2: maximum number */ 
  68. return new WP_Error( 'rest_invalid_param', sprintf( __( '%1$s must be less than %2$d (inclusive)' ), $param, $args['maximum'] ) ); 
  69. } elseif ( isset( $args['maximum'] ) && isset( $args['minimum'] ) ) { 
  70. if ( ! empty( $args['exclusiveMinimum'] ) && ! empty( $args['exclusiveMaximum'] ) ) { 
  71. if ( $value >= $args['maximum'] || $value <= $args['minimum'] ) { 
  72. /** translators: 1: parameter, 2: minimum number, 3: maximum number */ 
  73. return new WP_Error( 'rest_invalid_param', sprintf( __( '%1$s must be between %2$d (exclusive) and %3$d (exclusive)' ), $param, $args['minimum'], $args['maximum'] ) ); 
  74. } elseif ( empty( $args['exclusiveMinimum'] ) && ! empty( $args['exclusiveMaximum'] ) ) { 
  75. if ( $value >= $args['maximum'] || $value < $args['minimum'] ) { 
  76. /** translators: 1: parameter, 2: minimum number, 3: maximum number */ 
  77. return new WP_Error( 'rest_invalid_param', sprintf( __( '%1$s must be between %2$d (inclusive) and %3$d (exclusive)' ), $param, $args['minimum'], $args['maximum'] ) ); 
  78. } elseif ( ! empty( $args['exclusiveMinimum'] ) && empty( $args['exclusiveMaximum'] ) ) { 
  79. if ( $value > $args['maximum'] || $value <= $args['minimum'] ) { 
  80. /** translators: 1: parameter, 2: minimum number, 3: maximum number */ 
  81. return new WP_Error( 'rest_invalid_param', sprintf( __( '%1$s must be between %2$d (exclusive) and %3$d (inclusive)' ), $param, $args['minimum'], $args['maximum'] ) ); 
  82. } elseif ( empty( $args['exclusiveMinimum'] ) && empty( $args['exclusiveMaximum'] ) ) { 
  83. if ( $value > $args['maximum'] || $value < $args['minimum'] ) { 
  84. /** translators: 1: parameter, 2: minimum number, 3: maximum number */ 
  85. return new WP_Error( 'rest_invalid_param', sprintf( __( '%1$s must be between %2$d (inclusive) and %3$d (inclusive)' ), $param, $args['minimum'], $args['maximum'] ) ); 
  86.  
  87. return true;