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",
"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]]

View file

@ -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"] }

View file

@ -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<DefaultState>) -> ConfigBuilder<DefaultState> {
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);
}
}