Repository: juspay/hyperswitch
File: crates/common_types/src/primitive_wrappers.rs (Rust)

Code before:
        type Target = i16;

        fn deref(&self) -> &Self::Target {
            &self.0
        }
    }

    impl<'de> Deserialize<'de> for CustomerListLimit {
        fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
        where
            D: serde::Deserializer<'de>,
        {
            let val = i16::deserialize(deserializer)?;
            if val < CUSTOMER_LIST_LOWER_LIMIT {
                Err(D::Error::custom(format!(
                    "CustomerListLimit cannot be less than {}",
                    CUSTOMER_LIST_LOWER_LIMIT
                )))
            } else if val > CUSTOMER_LIST_UPPER_LIMIT {
                Err(D::Error::custom(format!(
                    "CustomerListLimit exceeds the maximum allowed value of {}",
                    CUSTOMER_LIST_UPPER_LIMIT
                )))
            } else {
                Ok(Self(val))
            }
        }
    }

    impl diesel::deserialize::FromSql<diesel::sql_types::SmallInt, diesel::pg::Pg>
        for CustomerListLimit
    {
        fn from_sql(value: diesel::pg::PgValue<'_>) -> diesel::deserialize::Result<Self> {
            i16::from_sql(value).map(Self)
        }
    }

    impl diesel::serialize::ToSql<diesel::sql_types::SmallInt, diesel::pg::Pg> for CustomerListLimit {
        fn to_sql<'b>(
            &'b self,
            out: &mut diesel::serialize::Output<'b, '_, diesel::pg::Pg>,
        ) -> diesel::serialize::Result {
            <i16 as diesel::serialize::ToSql<diesel::sql_types::SmallInt, diesel::pg::Pg>>::to_sql(
                &self.0, out,
            )
        }
    }

    impl Default for CustomerListLimit {
        /// Default for `CustomerListLimit` is `20`
        fn default() -> Self {

Review comment (suggestion):
new function already had these validation logic
```suggestion
            let val = u16::deserialize(deserializer)?;
            Ok(Self.new(val).change_context(..)?)
```

Code after:

        fn deref(&self) -> &Self::Target {
            &self.0
        }
    }

    impl<'de> Deserialize<'de> for CustomerListLimit {
        fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
        where
            D: serde::Deserializer<'de>,
        {
            let val = u16::deserialize(deserializer)?;
            Self::new(val).map_err(D::Error::custom)
        }
    }

    impl Default for CustomerListLimit {
        /// Default for `CustomerListLimit` is `20`
        fn default() -> Self {
            Self(CUSTOMER_LIST_DEFAULT_LIMIT)
        }
    }

    impl CustomerListLimit {
        /// Creates a new CustomerListLimit with validation
        pub fn new(value: u16) -> Result<Self, String> {
            if value < CUSTOMER_LIST_LOWER_LIMIT {
                Err(format!(
                    "CustomerListLimit cannot be less than {}",
                    CUSTOMER_LIST_LOWER_LIMIT
                ))
            } else if value > CUSTOMER_LIST_UPPER_LIMIT {
                Err(format!(
                    "CustomerListLimit exceeds the maximum allowed value of {}",
                    CUSTOMER_LIST_UPPER_LIMIT
                ))
            } else {
                Ok(Self(value))
            }
        }
    }
}

/// Safe string wrapper that validates input against XSS attacks
mod safe_string {
    use std::ops::Deref;

    use common_utils::validation::contains_potential_xss_or_sqli;
    use masking::SerializableSecret;
    use serde::{de::Error, Deserialize, Serialize};