Automattic\WooCommerce\StoreApi\Schemas\V1

AbstractSchema::get_recursive_validate_callback()protectedWC 1.0

Gets a function that validates recursively.

Method of the class: AbstractSchema{}

No Hooks.

Return

function. Anonymous validation callback.

Usage

// protected - for code of main (parent) or child class
$result = $this->get_recursive_validate_callback( $properties );
$properties(array) (required)
Schema property data.

AbstractSchema::get_recursive_validate_callback() code WC 9.3.3

protected function get_recursive_validate_callback( $properties ) {
	/**
	 * Validate a request argument based on details registered to the route.
	 *
	 * @param mixed            $values
	 * @param \WP_REST_Request $request
	 * @param string           $param
	 * @return true|\WP_Error
	 */
	return function ( $values, $request, $param ) use ( $properties ) {
		foreach ( $properties as $property_key => $property_value ) {
			$current_value = isset( $values[ $property_key ] ) ? $values[ $property_key ] : null;

			$property_type = is_array( $property_value['type'] ) ? $property_value['type'] : [ $property_value['type'] ];
			if ( empty( $current_value ) && in_array( 'null', $property_type, true ) ) {
				// If the value is null and the schema allows null, we can skip validation for children.
				continue;
			}

			if ( isset( $property_value['arg_options']['validate_callback'] ) ) {
				$callback = $property_value['arg_options']['validate_callback'];
				$result   = is_callable( $callback ) ? $callback( $current_value, $request, $param ) : false;
			} else {
				$result = rest_validate_value_from_schema( $current_value, $property_value, $param . ' > ' . $property_key );
			}

			if ( ! $result || is_wp_error( $result ) ) {
				// If schema validation fails, we return here as we don't need to validate any deeper.
				return $result;
			}

			if ( isset( $property_value['properties'] ) ) {
				$validate_callback = $this->get_recursive_validate_callback( $property_value['properties'] );
				$result            = $validate_callback( $current_value, $request, $param . ' > ' . $property_key );

				if ( ! $result || is_wp_error( $result ) ) {
					// If schema validation fails, we return here as we don't need to validate any deeper.
					return $result;
				}
			}
		}

		return true;
	};
}