Automattic\WooCommerce\StoreApi\Schemas\V1

CheckoutSchema::validate_additional_fieldspublicWC 1.0

Validate additional fields object. This does not validate required fields nor customer validation rules because this may be a partial request. That will happen later when the full request is processed during POST. This only validates against the schema.

Метод класса: CheckoutSchema{}

Хуков нет.

Возвращает

true|\WP_Error.

Использование

$CheckoutSchema = new CheckoutSchema();
$CheckoutSchema->validate_additional_fields( $fields, $request );
$fields(массив) (обязательный)
Value being sanitized.
$request(WP_REST_Request) (обязательный)
The Request.

Заметки

  • Смотрите: rest_validate_value_from_schema

Код CheckoutSchema::validate_additional_fields() WC 10.0.2

public function validate_additional_fields( $fields, $request ) {
	$errors                  = new \WP_Error();
	$fields                  = $this->sanitize_additional_fields( $fields );
	$additional_field_schema = $this->get_additional_fields_schema();

	// for PUT requests, we only want to validate the fields that are being updated.
	if ( $request->get_method() === 'PUT' ) {
		$additional_field_schema = array_intersect_key( $additional_field_schema, $fields );
	}

	// on POST, loop over the schema instead of the fields. This is to ensure missing fields are validated.
	foreach ( $additional_field_schema as $key => $schema ) {
		if ( ! isset( $fields[ $key ] ) && true !== $schema['required'] ) {
			// Optional fields can go missing.
			continue;
		}

		$result = rest_validate_value_from_schema( $fields[ $key ] ?? null, $schema, $key );

		if ( is_wp_error( $result ) && $result->has_errors() ) {
			$location = $this->additional_fields_controller->get_field_location( $key );
			foreach ( $result->get_error_codes() as $code ) {
				$result->add_data(
					array(
						'location' => $location,
						'key'      => $key,
					),
					$code
				);
			}
			$errors->merge_from( $result );
		}
	}

	return $errors->has_errors() ? $errors : true;
}