WP_Ability::validate_outputprotectedWP 6.9.0

Validates output data against the output schema.

Method of the class: WP_Ability{}

No Hooks.

Returns

true|WP_Error. Returns true if valid, or a WP_Error object if validation fails.

Usage

// protected - for code of main (parent) or child class
$result = $this->validate_output( $output );
$output(mixed) (required)
The output data to validate.

Changelog

Since 6.9.0 Introduced.

WP_Ability::validate_output() code WP 6.9

protected function validate_output( $output ) {
	$output_schema = $this->get_output_schema();
	if ( empty( $output_schema ) ) {
		return true;
	}

	$valid_output = rest_validate_value_from_schema( $output, $output_schema, 'output' );
	if ( is_wp_error( $valid_output ) ) {
		return new WP_Error(
			'ability_invalid_output',
			sprintf(
				/* translators: %1$s ability name, %2$s error message. */
				__( 'Ability "%1$s" has invalid output. Reason: %2$s' ),
				esc_html( $this->name ),
				$valid_output->get_error_message()
			)
		);
	}

	return true;
}