wpseek.com
A WordPress-centric search engine for devs and theme authors



_wp_json_schema_matches_type › WordPress Function

Since7.2.0
Deprecatedn/a
› _wp_json_schema_matches_type ( $value, $schema, $type )
Access:
  • private
Parameters: (3)
  • (mixed) $value The value to validate.
    Required: Yes
  • (array<string,mixed>) $schema The schema to inspect.
    Required: Yes
  • (string) $type The type to match.
    Required: Yes
Returns:
  • (bool) Whether the value matches the schema through the requested type.
Defined at:
Codex:

Determines whether a value matches a JSON Schema through a given type.

Combining schemas must have a matching branch that allows the requested type. Finding the type in one branch and validating the value against another branch is not sufficient.


Source

function _wp_json_schema_matches_type( $value, array $schema, string $type ): bool {
	if ( isset( $schema['type'] ) && ! in_array( $type, (array) $schema['type'], true ) ) {
		return false;
	}

	foreach ( array( 'anyOf', 'oneOf' ) as $keyword ) {
		if ( ! isset( $schema[ $keyword ] ) ) {
			continue;
		}

		foreach ( $schema[ $keyword ] as $sub_schema ) {
			if ( ! is_array( $sub_schema ) ) {
				continue;
			}

			// Match rest_find_any_matching_schema(), which applies the parent type to untyped branches.
			if ( ! isset( $sub_schema['type'] ) && isset( $schema['type'] ) ) {
				$sub_schema['type'] = $schema['type'];
			}

			if ( _wp_json_schema_matches_type( $value, $sub_schema, $type ) ) {
				continue 2;
			}
		}

		return false;
	}

	if ( ! isset( $schema['type'] ) && ! isset( $schema['anyOf'] ) && ! isset( $schema['oneOf'] ) ) {
		return false;
	}

	// Explicit and inferred types must both validate through the requested type only.
	$schema['type'] = $type;

	return ! is_wp_error( rest_validate_value_from_schema( $value, $schema ) );
}