函数文档

rest_find_matching_pattern_property_schema()

💡 云策文档标注

概述

rest_find_matching_pattern_property_schema() 函数用于在 JSON Schema 中基于 patternProperties 关键字查找匹配属性的模式。它通过遍历模式并验证属性名是否匹配,返回对应的子模式或 null。

关键要点

  • 函数用途:根据 patternProperties 查找属性匹配的 JSON Schema 模式。
  • 参数:$property(字符串,必需,要检查的属性名)和 $args(数组,必需,使用的模式数组)。
  • 返回值:匹配的模式属性数组,若无匹配则返回 null。
  • 内部逻辑:检查 $args 中是否存在 patternProperties,遍历每个模式,使用 rest_validate_json_schema_pattern() 验证属性是否匹配。
  • 相关函数:rest_validate_json_schema_pattern()、rest_validate_object_value_from_schema()、rest_filter_response_by_context()、rest_sanitize_value_from_schema()。
  • 引入版本:WordPress 5.6.0。

代码示例

function rest_find_matching_pattern_property_schema( $property, $args ) {
	if ( isset( $args['patternProperties'] ) ) {
		foreach ( $args['patternProperties'] as $pattern => $child_schema ) {
			if ( rest_validate_json_schema_pattern( $pattern, $property ) ) {
				return $child_schema;
			}
		}
	}

	return null;
}

📄 原文内容

Finds the schema for a property using the patternProperties keyword.

Parameters

$propertystringrequired
The property name to check.
$argsarrayrequired
The schema array to use.

Return

array|null The schema of matching pattern property, or null if no patterns match.

Source

function rest_find_matching_pattern_property_schema( $property, $args ) {
	if ( isset( $args['patternProperties'] ) ) {
		foreach ( $args['patternProperties'] as $pattern => $child_schema ) {
			if ( rest_validate_json_schema_pattern( $pattern, $property ) ) {
				return $child_schema;
			}
		}
	}

	return null;
}

Changelog

Version Description
5.6.0 Introduced.