rest_find_any_matching_schema

函式
rest_find_any_matching_schema ( $value, $args, $param )
引數
  • (mixed) $value The value to validate.
    Required:
  • (array) $args The schema array to use.
    Required:
  • (string) $param The parameter name, used in error messages.
    Required:
返回值
  • (array|WP_Error) The matching schema or WP_Error instance if all schemas do not match.
定義位置
相關方法
rest_find_one_matching_schemarest_find_matching_pattern_property_schemarest_handle_multi_type_schemarest_get_endpoint_args_for_schemaget_block_metadata_i18n_schema
引入
5.6.0
棄用
-

rest_find_any_matching_schema: 這個函式用於查詢與給定資料相匹配的模式。它接收一個資料物件和一個模式物件的陣列,並返回與資料相匹配的第一個模式物件。

在"anyOf"模式中找到匹配的模式。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function rest_find_any_matching_schema( $value, $args, $param ) {
$errors = array();
foreach ( $args['anyOf'] as $index => $schema ) {
if ( ! isset( $schema['type'] ) && isset( $args['type'] ) ) {
$schema['type'] = $args['type'];
}
$is_valid = rest_validate_value_from_schema( $value, $schema, $param );
if ( ! is_wp_error( $is_valid ) ) {
return $schema;
}
$errors[] = array(
'error_object' => $is_valid,
'schema' => $schema,
'index' => $index,
);
}
return rest_get_combining_operation_error( $value, $param, $errors );
}
function rest_find_any_matching_schema( $value, $args, $param ) { $errors = array(); foreach ( $args['anyOf'] as $index => $schema ) { if ( ! isset( $schema['type'] ) && isset( $args['type'] ) ) { $schema['type'] = $args['type']; } $is_valid = rest_validate_value_from_schema( $value, $schema, $param ); if ( ! is_wp_error( $is_valid ) ) { return $schema; } $errors[] = array( 'error_object' => $is_valid, 'schema' => $schema, 'index' => $index, ); } return rest_get_combining_operation_error( $value, $param, $errors ); }
function rest_find_any_matching_schema( $value, $args, $param ) {
	$errors = array();

	foreach ( $args['anyOf'] as $index => $schema ) {
		if ( ! isset( $schema['type'] ) && isset( $args['type'] ) ) {
			$schema['type'] = $args['type'];
		}

		$is_valid = rest_validate_value_from_schema( $value, $schema, $param );
		if ( ! is_wp_error( $is_valid ) ) {
			return $schema;
		}

		$errors[] = array(
			'error_object' => $is_valid,
			'schema'       => $schema,
			'index'        => $index,
		);
	}

	return rest_get_combining_operation_error( $value, $param, $errors );
}

常見問題

FAQs
檢視更多 >