feat: validate configuration parameters

This commit is contained in:
Aravinth Manivannan 2024-05-18 19:40:30 +05:30
parent 189d164ebc
commit 607d2350b6
Signed by: realaravinth
GPG key ID: F8F50389936984FF
3 changed files with 39 additions and 5 deletions

35
Cargo.lock generated
View file

@ -361,7 +361,7 @@ dependencies = [
"regex", "regex",
"rust-argon2", "rust-argon2",
"unicode-normalization", "unicode-normalization",
"validator", "validator 0.16.1",
] ]
[[package]] [[package]]
@ -3678,7 +3678,23 @@ dependencies = [
"serde_derive", "serde_derive",
"serde_json", "serde_json",
"url", "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]] [[package]]
@ -3697,6 +3713,20 @@ dependencies = [
"validator_types", "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]] [[package]]
name = "validator_types" name = "validator_types"
version = "0.16.0" version = "0.16.0"
@ -3735,6 +3765,7 @@ dependencies = [
"tracing", "tracing",
"tracing-actix-web", "tracing-actix-web",
"url", "url",
"validator 0.18.1",
] ]
[[package]] [[package]]

View file

@ -32,3 +32,4 @@ time = { version = "0.3.36", features = ["serde"] }
tracing = { version = "0.1.40", features = ["log"] } tracing = { version = "0.1.40", features = ["log"] }
tracing-actix-web = "0.7.10" tracing-actix-web = "0.7.10"
url = { version = "2.5.0", features = ["serde"] } url = { version = "2.5.0", features = ["serde"] }
validator = { version = "0.18.1", features = ["derive"] }

View file

@ -8,6 +8,7 @@ use config::{builder::DefaultState, ConfigBuilder, ConfigError};
use derive_more::Display; use derive_more::Display;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use url::Url; use url::Url;
use validator::Validate;
#[derive(Deserialize, Serialize, Display, Eq, PartialEq, Clone, Debug)] #[derive(Deserialize, Serialize, Display, Eq, PartialEq, Clone, Debug)]
#[serde(rename_all = "lowercase")] #[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 { pub struct Database {
#[validate(url)]
pub url: String, pub url: String,
pub pool: u32, pub pool: u32,
pub database_type: DBType, pub database_type: DBType,
@ -37,7 +39,7 @@ impl Database {
pub fn env_override(mut s: ConfigBuilder<DefaultState>) -> ConfigBuilder<DefaultState> { pub fn env_override(mut s: ConfigBuilder<DefaultState>) -> ConfigBuilder<DefaultState> {
for (parameter, env_var_name) in [ for (parameter, env_var_name) in [
("database.url", "DATABASE_URL"), ("database.url", "DATABASE_URL"),
("database.pool", "FORGEFLUX_database_POOL"), ("database.pool", "VANIKAM_database_POOL"),
] ]
.iter() .iter()
{ {
@ -72,6 +74,6 @@ mod tests {
"postgres://test_db_env_override", "postgres://test_db_env_override",
database.url database.url
); );
env_helper!(init_settings, "FORGEFLUX_database_POOL", 99, database.pool); env_helper!(init_settings, "VANIKAM_database_POOL", 99, database.pool);
} }
} }