Skip to content

Commit

Permalink
Validate view id:s
Browse files Browse the repository at this point in the history
  • Loading branch information
patriksvensson committed Feb 9, 2020
1 parent e08a009 commit 1e1aab2
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/config/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,13 @@ impl Validate for Configuration {
}

fn validate_views(configuration: &Configuration) -> DuckResult<()> {
let valid_id_pattern = Regex::new(r"^[a-zA-Z0-9_]+$")?;
if let Some(views) = &configuration.views {
let mut known_ids = HashSet::<String>::new();
for view in views.iter() {
if !valid_id_pattern.is_match(&view.id) {
return Err(format_err!("The view id '{}' is invalid.", view.id));
}
if known_ids.contains(&view.id) {
return Err(format_err!(
"Found duplicate view id '{}' in configuration.",
Expand Down Expand Up @@ -163,6 +167,28 @@ mod tests {
config.validate().unwrap();
}

#[test]
#[should_panic(expected = "The view id \\'foo bar\\' is invalid.")]
fn should_return_error_if_a_view_have_an_invalid_id() {
let config = Configuration::from_json(
&TestVariableProvider::new(),
r#"
{
"collectors": [ ],
"views": [
{
"id": "foo bar",
"name": "Foo",
"collectors": [ ]
}
]
}
"#,
)
.unwrap();
config.validate().unwrap();
}

#[test]
#[should_panic(expected = "Found duplicate id \\'foo\\' in configuration.")]
fn should_return_error_if_two_collectors_have_the_same_id() {
Expand Down

0 comments on commit 1e1aab2

Please sign in to comment.