wpseek.com
Eine auf WordPress spezialiserte Suchmaschine für Entwickler und Theme-Autoren



rest_validate_enum › WordPress Function

Seit5.7.0
Veraltetn/v
rest_validate_enum ( $value, $args, $param )
Parameter: (3)
  • (mixed) $value The value to validate.
    Erforderlich: Ja
  • (array) $args The schema array to use.
    Erforderlich: Ja
  • (string) $param The parameter name, used in error messages.
    Erforderlich: Ja
Gibt zurück:
  • (true|WP_Error) True if the "enum" contains the value or a WP_Error instance otherwise.
Definiert in:
Codex:

Validates that the given value is a member of the JSON Schema "enum".



Quellcode

function rest_validate_enum( $value, $args, $param ) {
	$sanitized_value = rest_sanitize_value_from_schema( $value, $args, $param );
	if ( is_wp_error( $sanitized_value ) ) {
		return $sanitized_value;
	}

	foreach ( $args['enum'] as $enum_value ) {
		if ( rest_are_values_equal( $sanitized_value, $enum_value ) ) {
			return true;
		}
	}

	$encoded_enum_values = array();
	foreach ( $args['enum'] as $enum_value ) {
		$encoded_enum_values[] = is_scalar( $enum_value ) ? $enum_value : wp_json_encode( $enum_value );
	}

	if ( count( $encoded_enum_values ) === 1 ) {
		/* translators: 1: Parameter, 2: Valid values. */
		return new WP_Error( 'rest_not_in_enum', wp_sprintf( __( '%1$s is not %2$s.' ), $param, $encoded_enum_values[0] ) );
	}

	/* translators: 1: Parameter, 2: List of valid values. */
	return new WP_Error( 'rest_not_in_enum', wp_sprintf( __( '%1$s is not one of %2$l.' ), $param, $encoded_enum_values ) );
}