rest_validate_array_value_from_schema() – Validates an array value based on a schema.

You appear to be a bot. Output may be restricted

Description

Validates an array value based on a schema.

Usage

$true|WP_Error = rest_validate_array_value_from_schema( $value, $args, $param );

Parameters

$value
( mixed ) required – The value to validate.
$args
( array ) required – Schema array to use for validation.
$param
( string ) required – The parameter name, used in error messages.

Returns

true|WP_Error

Source

File name: wordpress/wp-includes/rest-api.php
Lines:

1 to 60 of 60
function rest_validate_array_value_from_schema( $value, $args, $param ) {
  if ( ! rest_is_array( $value ) ) {
    return new WP_Error(
      'rest_invalid_type',
      /* 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 ) ) {
        return $is_valid;
      }
    }
  }

  if ( isset( $args['minItems'] ) && count( $value ) < $args['minItems'] ) {
    return new WP_Error(
      'rest_too_few_items',
      sprintf(
        /* translators: 1: Parameter, 2: Number. */
        _n(
          '%1$s must contain at least %2$s item.',
          '%1$s must contain at least %2$s items.',
          $args['minItems']
        ),
        $param,
        number_format_i18n( $args['minItems'] )
      )
    );
  }

  if ( isset( $args['maxItems'] ) && count( $value ) > $args['maxItems'] ) {
    return new WP_Error(
      'rest_too_many_items',
      sprintf(
        /* translators: 1: Parameter, 2: Number. */
        _n(
          '%1$s must contain at most %2$s item.',
          '%1$s must contain at most %2$s items.',
          $args['maxItems']
        ),
        $param,
        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 ) );
  }

  return true;
}
 

 View on GitHub View on Trac