Skip to main content

dpp_domain/validation/
batch.rs

1//! Batch validation of multiple product group-data items in one pass.
2
3use super::functions::validate_product_group_data;
4use crate::field_error::ValidationErrors;
5use crate::product_group::ProductGroupData;
6
7/// Result of validating a single item in a batch.
8#[derive(Debug, Clone)]
9pub struct BatchValidationItem {
10    /// Zero-based index in the input slice.
11    pub index: usize,
12    /// Validation result: `Ok(())` if valid, `Err` with field-level errors otherwise.
13    pub result: Result<(), ValidationErrors>,
14}
15
16/// Validate a batch of product group data items, collecting all errors per item.
17///
18/// The returned `Vec` has the same length and order as the input.
19pub fn validate_product_group_data_batch(items: &[ProductGroupData]) -> Vec<BatchValidationItem> {
20    items
21        .iter()
22        .enumerate()
23        .map(|(index, data)| BatchValidationItem {
24            index,
25            result: validate_product_group_data(data),
26        })
27        .collect()
28}
29
30/// Returns only the failures from a batch validation run.
31pub fn batch_errors(results: &[BatchValidationItem]) -> Vec<&BatchValidationItem> {
32    results.iter().filter(|item| item.result.is_err()).collect()
33}