From 607d2350b6ac91920e6ee040527ca931441c9e95 Mon Sep 17 00:00:00 2001 From: Aravinth Manivannan Date: Sat, 18 May 2024 19:40:30 +0530 Subject: [PATCH] feat: validate configuration parameters --- Cargo.lock | 35 +++++++++++++++++++++++++++++++++-- Cargo.toml | 1 + src/settings/database.rs | 8 +++++--- 3 files changed, 39 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 391f8a7..3cf2f62 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -361,7 +361,7 @@ dependencies = [ "regex", "rust-argon2", "unicode-normalization", - "validator", + "validator 0.16.1", ] [[package]] @@ -3678,7 +3678,23 @@ dependencies = [ "serde_derive", "serde_json", "url", - "validator_derive", + "validator_derive 0.16.0", +] + +[[package]] +name = "validator" +version = "0.18.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "db79c75af171630a3148bd3e6d7c4f42b6a9a014c2945bc5ed0020cbb8d9478e" +dependencies = [ + "idna 0.5.0", + "once_cell", + "regex", + "serde", + "serde_derive", + "serde_json", + "url", + "validator_derive 0.18.1", ] [[package]] @@ -3697,6 +3713,20 @@ dependencies = [ "validator_types", ] +[[package]] +name = "validator_derive" +version = "0.18.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "55591299b7007f551ed1eb79a684af7672c19c3193fb9e0a31936987bb2438ec" +dependencies = [ + "darling 0.20.9", + "once_cell", + "proc-macro-error", + "proc-macro2", + "quote", + "syn 2.0.63", +] + [[package]] name = "validator_types" version = "0.16.0" @@ -3735,6 +3765,7 @@ dependencies = [ "tracing", "tracing-actix-web", "url", + "validator 0.18.1", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 570c874..13dc5d5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -32,3 +32,4 @@ time = { version = "0.3.36", features = ["serde"] } tracing = { version = "0.1.40", features = ["log"] } tracing-actix-web = "0.7.10" url = { version = "2.5.0", features = ["serde"] } +validator = { version = "0.18.1", features = ["derive"] } diff --git a/src/settings/database.rs b/src/settings/database.rs index f5b2722..5f449a7 100644 --- a/src/settings/database.rs +++ b/src/settings/database.rs @@ -8,6 +8,7 @@ use config::{builder::DefaultState, ConfigBuilder, ConfigError}; use derive_more::Display; use serde::{Deserialize, Serialize}; use url::Url; +use validator::Validate; #[derive(Deserialize, Serialize, Display, Eq, PartialEq, Clone, Debug)] #[serde(rename_all = "lowercase")] @@ -26,8 +27,9 @@ impl DBType { } } -#[derive(Debug, Clone, Deserialize, Eq, PartialEq)] +#[derive(Debug, Clone, Validate, Deserialize, Eq, PartialEq)] pub struct Database { + #[validate(url)] pub url: String, pub pool: u32, pub database_type: DBType, @@ -37,7 +39,7 @@ impl Database { pub fn env_override(mut s: ConfigBuilder) -> ConfigBuilder { for (parameter, env_var_name) in [ ("database.url", "DATABASE_URL"), - ("database.pool", "FORGEFLUX_database_POOL"), + ("database.pool", "VANIKAM_database_POOL"), ] .iter() { @@ -72,6 +74,6 @@ mod tests { "postgres://test_db_env_override", database.url ); - env_helper!(init_settings, "FORGEFLUX_database_POOL", 99, database.pool); + env_helper!(init_settings, "VANIKAM_database_POOL", 99, database.pool); } }