# rest_sanitize_value_from_schema()

URL: https://chugunov.pro/api-wordpress/functions/rest_sanitize_value_from_schema/
Проверено на WordPress 6.9, обновлено 06.08.2026.
Источник: независимый русскоязычный справочник chugunov.pro. Не является официальной документацией WordPress.

Тип: функция.
Появился в версии: 4.7.0.

## Сигнатура

```php
rest_sanitize_value_from_schema( mixed $value, array $args, string $param = '' ): mixed|WP_Error
```

## Описание

Очищает значение на основе схемы.

## Параметры

- `$value` `mixed` — обязательный. Значение для очистки.
- `$args` `array` — обязательный. Массив схемы, используемый для очистки.
- `$param` `string` — необязательный, по умолчанию `''`. Имя параметра, используемое в сообщениях об ошибках.

## Возвращаемое значение

`mixed|WP_Error` — WP_Error

## Исходный код

Файл: `wp-includes/rest-api.php:2775`

```php
function rest_sanitize_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 ) ) {
			return $matching_schema;
		}

		if ( ! isset( $args['type'] ) ) {
			$args['type'] = $matching_schema['type'];
		}

		$value = rest_sanitize_value_from_schema( $value, $matching_schema, $param );
	}

	if ( isset( $args['oneOf'] ) ) {
		$matching_schema = rest_find_one_matching_schema( $value, $args, $param );
		if ( is_wp_error( $matching_schema ) ) {
			return $matching_schema;
		}

		if ( ! isset( $args['type'] ) ) {
			$args['type'] = $matching_schema['type'];
		}

		$value = rest_sanitize_value_from_schema( $value, $matching_schema, $param );
	}

	$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 );

		if ( ! $best_type ) {
			return null;
		}

		$args['type'] = $best_type;
	}

	if ( ! in_array( $args['type'], $allowed_types, true ) ) {
		_doing_it_wrong(
			__FUNCTION__,
			/* 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 ),
			'5.5.0'
		);
	}

	if ( 'array' === $args['type'] ) {
		$value = rest_sanitize_array( $value );

		if ( ! empty( $args['items'] ) ) {
			foreach ( $value as $index => $v ) {
				$value[ $index ] = rest_sanitize_value_from_schema( $v, $args['items'], $param . '[' . $index . ']' );
			}
		}

		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 $value;
	}

	if ( 'object' === $args['type'] ) {
		$value = rest_sanitize_object( $value );

		foreach ( $value as $property => $v ) {
			if ( isset( $args['properties'][ $property ] ) ) {
				$value[ $property ] = rest_sanitize_value_from_schema( $v, $args['properties'][ $property ], $param . '[' . $property . ']' );
				continue;
			}

			$pattern_property_schema = rest_find_matching_pattern_property_schema( $property, $args );
			if ( null !== $pattern_property_schema ) {
				$value[ $property ] = rest_sanitize_value_from_schema( $v, $pattern_property_schema, $param . '[' . $property . ']' );
				continue;
			}

			if ( isset( $args['additionalProperties'] ) ) {
				if ( false === $args['additionalProperties'] ) {
					unset( $value[ $property ] );
				} elseif ( is_array( $args['additionalProperties'] ) ) {
					$value[ $property ] = rest_sanitize_value_from_schema( $v, $args['additionalProperties'], $param . '[' . $property . ']' );
				}
			}
		}

		return $value;
	}

	if ( 'null' === $args['type'] ) {
		return null;
	}

	if ( 'integer' === $args['type'] ) {
		return (int) $value;
	}

	if ( 'number' === $args['type'] ) {
		return (float) $value;
	}

	if ( 'boolean' === $args['type'] ) {
		return rest_sanitize_boolean( $value );
	}

	// This behavior matches rest_validate_value_from_schema().
	if ( isset( $args['format'] )
		&& ( ! isset( $args['type'] ) || 'string' === $args['type'] || ! in_array( $args['type'], $allowed_types, true ) )
	) {
		switch ( $args['format'] ) {
			case 'hex-color':
				return (string) sanitize_hex_color( $value );

			case 'date-time':
				return sanitize_text_field( $value );

			case 'email':
				// sanitize_email() validates, which would be unexpected.
				return sanitize_text_field( $value );

			case 'uri':
				return sanitize_url( $value );

			case 'ip':
				return sanitize_text_field( $value );

			case 'uuid':
				return sanitize_text_field( $value );

			case 'text-field':
				return sanitize_text_field( $value );

			case 'textarea-field':
				return sanitize_textarea_field( $value );
		}
	}

	if ( 'string' === $args['type'] ) {
		return (string) $value;
	}

	return $value;
}
```

## История изменений

- 5.9.0 — Added text-field and textarea-field formats.
- 5.6.0 — Support the "anyOf" and "oneOf" keywords.
- 5.5.0 — Added the $param parameter.
- 4.7.0 — Introduced.

## Связанные

Использует: [`rest_find_any_matching_schema`](https://chugunov.pro/api-wordpress/functions/rest_find_any_matching_schema/), [`rest_find_one_matching_schema`](https://chugunov.pro/api-wordpress/functions/rest_find_one_matching_schema/), [`rest_find_matching_pattern_property_schema`](https://chugunov.pro/api-wordpress/functions/rest_find_matching_pattern_property_schema/), [`rest_handle_multi_type_schema`](https://chugunov.pro/api-wordpress/functions/rest_handle_multi_type_schema/), [`rest_sanitize_array`](https://chugunov.pro/api-wordpress/functions/rest_sanitize_array/), [`rest_validate_array_contains_unique_items`](https://chugunov.pro/api-wordpress/functions/rest_validate_array_contains_unique_items/), [`rest_sanitize_object`](https://chugunov.pro/api-wordpress/functions/rest_sanitize_object/), [`rest_sanitize_value_from_schema`](https://chugunov.pro/api-wordpress/functions/rest_sanitize_value_from_schema/), [`rest_sanitize_boolean`](https://chugunov.pro/api-wordpress/functions/rest_sanitize_boolean/), [`sanitize_textarea_field`](https://chugunov.pro/api-wordpress/functions/sanitize_textarea_field/), [`sanitize_hex_color`](https://chugunov.pro/api-wordpress/functions/sanitize_hex_color/), [`wp_sprintf`](https://chugunov.pro/api-wordpress/functions/wp_sprintf/), [`__`](https://chugunov.pro/api-wordpress/functions/__/), [`sanitize_text_field`](https://chugunov.pro/api-wordpress/functions/sanitize_text_field/), [`sanitize_url`](https://chugunov.pro/api-wordpress/functions/sanitize_url/), [`_doing_it_wrong`](https://chugunov.pro/api-wordpress/functions/_doing_it_wrong/), [`is_wp_error`](https://chugunov.pro/api-wordpress/functions/is_wp_error/), `WP_Error::__construct`.
Используется в: `WP_REST_Widget_Types_Controller::prepare_item_for_response`, [`rest_validate_enum`](https://chugunov.pro/api-wordpress/functions/rest_validate_enum/), `WP_REST_Block_Types_Controller::prepare_item_for_response`, `WP_REST_Themes_Controller::prepare_theme_support`, `WP_REST_Block_Renderer_Controller::register_routes`, `WP_Widget_Media::update`, [`rest_sanitize_value_from_schema`](https://chugunov.pro/api-wordpress/functions/rest_sanitize_value_from_schema/), [`rest_sanitize_request_arg`](https://chugunov.pro/api-wordpress/functions/rest_sanitize_request_arg/), `WP_REST_Meta_Fields::prepare_value`, `WP_REST_Meta_Fields::update_value`, `WP_REST_Settings_Controller::prepare_value`.

Оригинал в официальной документации: https://developer.wordpress.org/reference/functions/rest_sanitize_value_from_schema/
