Skip to content
This repository has been archived by the owner on Aug 3, 2023. It is now read-only.

warn user to remove binding if deleted + check input for bindings #560

Merged
merged 2 commits into from
Sep 12, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ url = "2.1.0"
walkdir = "2.2.9"
percent-encoding = "1.0.1"
http = "0.1.1"
regex = "1"

[dev-dependencies]
assert_cmd = "0.11.1"
Expand Down
31 changes: 31 additions & 0 deletions src/commands/kv/namespace/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ pub fn create(
) -> Result<(), failure::Error> {
let client = kv::api_client(user)?;

if !validate_binding(binding) {
failure::bail!("A binding can only have alphabetical and _ characters");
}

let title = format!("{}-{}", target.name, binding);
let msg = format!("Creating namespace with title \"{}\"", title);
message::working(&msg);
Expand Down Expand Up @@ -67,3 +71,30 @@ pub fn create(

Ok(())
}

fn validate_binding(binding: &str) -> bool {
use regex::Regex;
let re = Regex::new(r"^[a-zA-Z_]+$").unwrap();
re.is_match(binding)
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn it_can_detect_invalid_binding() {
let invalid_bindings = vec!["hi there", "1234"];
for binding in invalid_bindings {
assert!(!validate_binding(binding));
}
}

#[test]
fn it_can_detect_valid_binding() {
let invalid_bindings = vec!["ONE", "TWO_TWO"];
for binding in invalid_bindings {
assert!(validate_binding(binding));
}
}
}
7 changes: 6 additions & 1 deletion src/commands/kv/namespace/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,12 @@ pub fn delete(target: &Target, user: GlobalUser, id: &str) -> Result<(), failure
});

match response {
Ok(_) => message::success("Success"),
Ok(_) => {
message::success("Success");
message::warn(
"Make sure to remove this \"kv-namespace\" entry from your wrangler.toml!",
)
}
Err(e) => kv::print_error(e),
}

Expand Down