Skip to content

Commit 02b03ae

Browse files
committed
validate *_date params pre-fetch
1 parent feb65b7 commit 02b03ae

3 files changed

Lines changed: 26 additions & 1 deletion

File tree

src/models/user_insights.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ pub struct ChartData {
211211
pub cadence: ChartMetricCadence,
212212
}
213213

214-
#[derive(Clone, Debug, PartialEq, Serialize)]
214+
#[derive(Clone, Debug, PartialEq, Serialize, Default)]
215215
pub struct FetchChartDataQuery {
216216
#[serde(skip_serializing_if = "Option::is_none", rename = "cadence")]
217217
pub cadence: Option<ChartMetricCadence>,

src/propelauth/errors.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,9 @@ pub enum FetchUserInsightsError {
516516
#[error("Invalid API Key")]
517517
InvalidApiKey,
518518

519+
#[error("Invalid parameters: {0}")]
520+
InvalidParams(&'static str),
521+
519522
#[error("Rate limited by PropelAuth")]
520523
PropelAuthRateLimit,
521524

src/propelauth/user_insights.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,28 @@ impl UserInsightsService<'_> {
266266
chart_metric: ChartMetric,
267267
params: FetchChartDataQuery,
268268
) -> Result<crate::models::user_insights::ChartData, FetchUserInsightsError> {
269+
if let Some(start_date_raw) = &params.start_date {
270+
if let Ok(start_date) = chrono::NaiveDate::parse_from_str(start_date_raw, "%Y-%m-%d") {
271+
if start_date > chrono::Utc::now().naive_utc().date() {
272+
return Err(FetchUserInsightsError::InvalidParams(
273+
"start_date cannot be in the future",
274+
));
275+
}
276+
} else {
277+
return Err(FetchUserInsightsError::InvalidParams(
278+
"start_date must be in YYYY-MM-DD format",
279+
));
280+
}
281+
}
282+
283+
if let Some(end_date_raw) = &params.end_date {
284+
if chrono::NaiveDate::parse_from_str(end_date_raw, "%Y-%m-%d").is_err() {
285+
return Err(FetchUserInsightsError::InvalidParams(
286+
"end_date must be in YYYY-MM-DD format",
287+
));
288+
}
289+
}
290+
269291
let result =
270292
user_insights_service_api::fetch_chart_metric_data(&self.config, chart_metric, params)
271293
.await;

0 commit comments

Comments
 (0)