rest_validate_object_value_from_schema

函式
rest_validate_object_value_from_schema ( $value, $args, $param )
引數
  • (mixed) $value The value to validate.
    Required:
  • (array) $args Schema array to use for validation.
    Required:
  • (string) $param The parameter name, used in error messages.
    Required:
返回值
  • (true|WP_Error)
定義位置
相關方法
rest_validate_value_from_schemarest_validate_number_value_from_schemarest_validate_boolean_value_from_schemarest_validate_string_value_from_schemarest_validate_integer_value_from_schema
引入
5.7.0
棄用
-

rest_validate_object_value_from_schema: 這是一個WordPress函式,用於驗證一個物件值是否與模式相匹配: 該函式接收一個物件引數和一個模式引數,如果物件與模式不匹配,則返回一個錯誤資訊。

根據一個schema驗證一個物件的值。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function rest_validate_object_value_from_schema( $value, $args, $param ) {
if ( ! rest_is_object( $value ) ) {
return new WP_Error(
'rest_invalid_type',
/* translators: 1: Parameter, 2: Type name. */
sprintf( __( '%1$s is not of type %2$s.' ), $param, 'object' ),
array( 'param' => $param )
);
}
$value = rest_sanitize_object( $value );
if ( isset( $args['required'] ) && is_array( $args['required'] ) ) { // schema version 4
foreach ( $args['required'] as $name ) {
if ( ! array_key_exists( $name, $value ) ) {
return new WP_Error(
'rest_property_required',
/* translators: 1: Property of an object, 2: Parameter. */
sprintf( __( '%1$s is a required property of %2$s.' ), $name, $param )
);
}
}
} elseif ( isset( $args['properties'] ) ) { // schema version 3
foreach ( $args['properties'] as $name => $property ) {
if ( isset( $property['required'] ) && true === $property['required'] && ! array_key_exists( $name, $value ) ) {
return new WP_Error(
'rest_property_required',
/* translators: 1: Property of an object, 2: Parameter. */
sprintf( __( '%1$s is a required property of %2$s.' ), $name, $param )
);
}
}
}
foreach ( $value as $property => $v ) {
if ( isset( $args['properties'][ $property ] ) ) {
$is_valid = rest_validate_value_from_schema( $v, $args['properties'][ $property ], $param . '[' . $property . ']' );
if ( is_wp_error( $is_valid ) ) {
return $is_valid;
}
continue;
}
$pattern_property_schema = rest_find_matching_pattern_property_schema( $property, $args );
if ( null !== $pattern_property_schema ) {
$is_valid = rest_validate_value_from_schema( $v, $pattern_property_schema, $param . '[' . $property . ']' );
if ( is_wp_error( $is_valid ) ) {
return $is_valid;
}
continue;
}
if ( isset( $args['additionalProperties'] ) ) {
if ( false === $args['additionalProperties'] ) {
return new WP_Error(
'rest_additional_properties_forbidden',
/* translators: %s: Property of an object. */
sprintf( __( '%1$s is not a valid property of Object.' ), $property )
);
}
if ( is_array( $args['additionalProperties'] ) ) {
$is_valid = rest_validate_value_from_schema( $v, $args['additionalProperties'], $param . '[' . $property . ']' );
if ( is_wp_error( $is_valid ) ) {
return $is_valid;
}
}
}
}
if ( isset( $args['minProperties'] ) && count( $value ) < $args['minProperties'] ) {
return new WP_Error(
'rest_too_few_properties',
sprintf(
/* translators: 1: Parameter, 2: Number. */
_n(
'%1$s must contain at least %2$s property.',
'%1$s must contain at least %2$s properties.',
$args['minProperties']
),
$param,
number_format_i18n( $args['minProperties'] )
)
);
}
if ( isset( $args['maxProperties'] ) && count( $value ) > $args['maxProperties'] ) {
return new WP_Error(
'rest_too_many_properties',
sprintf(
/* translators: 1: Parameter, 2: Number. */
_n(
'%1$s must contain at most %2$s property.',
'%1$s must contain at most %2$s properties.',
$args['maxProperties']
),
$param,
number_format_i18n( $args['maxProperties'] )
)
);
}
return true;
}
function rest_validate_object_value_from_schema( $value, $args, $param ) { if ( ! rest_is_object( $value ) ) { return new WP_Error( 'rest_invalid_type', /* translators: 1: Parameter, 2: Type name. */ sprintf( __( '%1$s is not of type %2$s.' ), $param, 'object' ), array( 'param' => $param ) ); } $value = rest_sanitize_object( $value ); if ( isset( $args['required'] ) && is_array( $args['required'] ) ) { // schema version 4 foreach ( $args['required'] as $name ) { if ( ! array_key_exists( $name, $value ) ) { return new WP_Error( 'rest_property_required', /* translators: 1: Property of an object, 2: Parameter. */ sprintf( __( '%1$s is a required property of %2$s.' ), $name, $param ) ); } } } elseif ( isset( $args['properties'] ) ) { // schema version 3 foreach ( $args['properties'] as $name => $property ) { if ( isset( $property['required'] ) && true === $property['required'] && ! array_key_exists( $name, $value ) ) { return new WP_Error( 'rest_property_required', /* translators: 1: Property of an object, 2: Parameter. */ sprintf( __( '%1$s is a required property of %2$s.' ), $name, $param ) ); } } } foreach ( $value as $property => $v ) { if ( isset( $args['properties'][ $property ] ) ) { $is_valid = rest_validate_value_from_schema( $v, $args['properties'][ $property ], $param . '[' . $property . ']' ); if ( is_wp_error( $is_valid ) ) { return $is_valid; } continue; } $pattern_property_schema = rest_find_matching_pattern_property_schema( $property, $args ); if ( null !== $pattern_property_schema ) { $is_valid = rest_validate_value_from_schema( $v, $pattern_property_schema, $param . '[' . $property . ']' ); if ( is_wp_error( $is_valid ) ) { return $is_valid; } continue; } if ( isset( $args['additionalProperties'] ) ) { if ( false === $args['additionalProperties'] ) { return new WP_Error( 'rest_additional_properties_forbidden', /* translators: %s: Property of an object. */ sprintf( __( '%1$s is not a valid property of Object.' ), $property ) ); } if ( is_array( $args['additionalProperties'] ) ) { $is_valid = rest_validate_value_from_schema( $v, $args['additionalProperties'], $param . '[' . $property . ']' ); if ( is_wp_error( $is_valid ) ) { return $is_valid; } } } } if ( isset( $args['minProperties'] ) && count( $value ) < $args['minProperties'] ) { return new WP_Error( 'rest_too_few_properties', sprintf( /* translators: 1: Parameter, 2: Number. */ _n( '%1$s must contain at least %2$s property.', '%1$s must contain at least %2$s properties.', $args['minProperties'] ), $param, number_format_i18n( $args['minProperties'] ) ) ); } if ( isset( $args['maxProperties'] ) && count( $value ) > $args['maxProperties'] ) { return new WP_Error( 'rest_too_many_properties', sprintf( /* translators: 1: Parameter, 2: Number. */ _n( '%1$s must contain at most %2$s property.', '%1$s must contain at most %2$s properties.', $args['maxProperties'] ), $param, number_format_i18n( $args['maxProperties'] ) ) ); } return true; }
function rest_validate_object_value_from_schema( $value, $args, $param ) {
	if ( ! rest_is_object( $value ) ) {
		return new WP_Error(
			'rest_invalid_type',
			/* translators: 1: Parameter, 2: Type name. */
			sprintf( __( '%1$s is not of type %2$s.' ), $param, 'object' ),
			array( 'param' => $param )
		);
	}

	$value = rest_sanitize_object( $value );

	if ( isset( $args['required'] ) && is_array( $args['required'] ) ) { // schema version 4
		foreach ( $args['required'] as $name ) {
			if ( ! array_key_exists( $name, $value ) ) {
				return new WP_Error(
					'rest_property_required',
					/* translators: 1: Property of an object, 2: Parameter. */
					sprintf( __( '%1$s is a required property of %2$s.' ), $name, $param )
				);
			}
		}
	} elseif ( isset( $args['properties'] ) ) { // schema version 3
		foreach ( $args['properties'] as $name => $property ) {
			if ( isset( $property['required'] ) && true === $property['required'] && ! array_key_exists( $name, $value ) ) {
				return new WP_Error(
					'rest_property_required',
					/* translators: 1: Property of an object, 2: Parameter. */
					sprintf( __( '%1$s is a required property of %2$s.' ), $name, $param )
				);
			}
		}
	}

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

		$pattern_property_schema = rest_find_matching_pattern_property_schema( $property, $args );
		if ( null !== $pattern_property_schema ) {
			$is_valid = rest_validate_value_from_schema( $v, $pattern_property_schema, $param . '[' . $property . ']' );
			if ( is_wp_error( $is_valid ) ) {
				return $is_valid;
			}
			continue;
		}

		if ( isset( $args['additionalProperties'] ) ) {
			if ( false === $args['additionalProperties'] ) {
				return new WP_Error(
					'rest_additional_properties_forbidden',
					/* translators: %s: Property of an object. */
					sprintf( __( '%1$s is not a valid property of Object.' ), $property )
				);
			}

			if ( is_array( $args['additionalProperties'] ) ) {
				$is_valid = rest_validate_value_from_schema( $v, $args['additionalProperties'], $param . '[' . $property . ']' );
				if ( is_wp_error( $is_valid ) ) {
					return $is_valid;
				}
			}
		}
	}

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

	if ( isset( $args['maxProperties'] ) && count( $value ) > $args['maxProperties'] ) {
		return new WP_Error(
			'rest_too_many_properties',
			sprintf(
				/* translators: 1: Parameter, 2: Number. */
				_n(
					'%1$s must contain at most %2$s property.',
					'%1$s must contain at most %2$s properties.',
					$args['maxProperties']
				),
				$param,
				number_format_i18n( $args['maxProperties'] )
			)
		);
	}

	return true;
}

常見問題

FAQs
檢視更多 >