Automattic\WooCommerce\StoreApi\Schemas\V1

CheckoutSchema::validate_additional_fields()publicWC 1.0

Validate additional fields object.

Method of the class: CheckoutSchema{}

No Hooks.

Return

true|\WP_Error.

Usage

$CheckoutSchema = new CheckoutSchema();
$CheckoutSchema->validate_additional_fields( $fields, $request );
$fields(array) (required)
Value being sanitized.
$request(\WP_REST_Request) (required)
The Request.

Notes

CheckoutSchema::validate_additional_fields() code WC 9.6.0

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();

	// 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 ] ) && ! $schema['required'] ) {
			// Optional fields can go missing.
			continue;
		}

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

		// Only allow custom validation on fields that pass the schema validation.
		if ( true === $result ) {
			$result = $this->additional_fields_controller->validate_field( $key, $field_value );
		}

		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 );
		}
	}

	// Validate groups of properties per registered location.
	$locations = array( 'contact', 'order' );

	foreach ( $locations as $location ) {
		$location_fields = $this->additional_fields_controller->filter_fields_for_location( $fields, $location );
		$result          = $this->additional_fields_controller->validate_fields_for_location( $location_fields, $location, 'other' );

		if ( is_wp_error( $result ) && $result->has_errors() ) {
			$errors->merge_from( $result );
		}
	}

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