Skip to content

Commit

Permalink
make source name validation consistent with subgraph name validation (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
lennyburdette authored Nov 4, 2024
1 parent 8f76f46 commit 23a2efd
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 5 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,10 @@ dependencies = [
"multimap 0.10.0",
"nom",
"nom_locate",
"once_cell",
"petgraph",
"pretty_assertions",
"regex",
"ron",
"rstest",
"serde",
Expand Down
2 changes: 2 additions & 0 deletions apollo-federation/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ lazy_static = "1.4.0"
line-col = "0.2.1"
multimap = "0.10.0"
nom = "7.1.3"
once_cell = "1.19.0"
petgraph = { version = "0.6.4", features = ["serde-1"] }
regex = "1.10.5"
serde.workspace = true
serde_json.workspace = true
serde_json_bytes.workspace = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,30 @@ input_file: apollo-federation/src/sources/connect/validation/test_data/invalid_c
[
Message {
code: InvalidSourceName,
message: "There are invalid characters in `@source(name: \"u$ers\")`. Only alphanumeric and underscores are allowed.",
message: "`@source(name: \"u$ers\")` is invalid; all source names must follow pattern '^[a-zA-Z][a-zA-Z0-9_-]{0,63}$",
locations: [
6:17..6:24,
],
},
Message {
code: InvalidSourceName,
message: "`@source(name: \"1\")` is invalid; all source names must follow pattern '^[a-zA-Z][a-zA-Z0-9_-]{0,63}$",
locations: [
7:17..7:20,
],
},
Message {
code: InvalidSourceName,
message: "`@source(name: \"no.dots\")` is invalid; all source names must follow pattern '^[a-zA-Z][a-zA-Z0-9_-]{0,63}$",
locations: [
8:17..8:26,
],
},
Message {
code: InvalidSourceName,
message: "`@source(name: \"areallylongnamethatisoversixtythreecharacterstakesalongwhiletotypebutthisshoulddoit\")` is invalid; all source names must follow pattern '^[a-zA-Z][a-zA-Z0-9_-]{0,63}$",
locations: [
10:11..10:96,
],
},
]
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ input_file: apollo-federation/src/sources/connect/validation/test_data/multiple_
},
Message {
code: InvalidSourceName,
message: "There are invalid characters in `@source(name: \"u$ers\")`. Only alphanumeric and underscores are allowed.",
message: "`@source(name: \"u$ers\")` is invalid; all source names must follow pattern '^[a-zA-Z][a-zA-Z0-9_-]{0,63}$",
locations: [
6:17..6:24,
],
Expand Down
18 changes: 16 additions & 2 deletions apollo-federation/src/sources/connect/validation/source_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,19 @@ use super::Message;
use crate::sources::connect::spec::schema::SOURCE_NAME_ARGUMENT_NAME;
use crate::sources::connect::validation::graphql::SchemaInfo;

// Adding a module to allow changing clippy lints for the regex
#[allow(clippy::expect_used)]
mod patterns {
use once_cell::sync::Lazy;
use regex::Regex;

/// This is the same regular expression used for subgraph names
pub(super) static SOURCE_NAME_REGEX: Lazy<Regex> = Lazy::new(|| {
Regex::new(r"^[a-zA-Z][a-zA-Z0-9_-]{0,63}$")
.expect("this regex to check source names is valid")
});
}

pub(super) fn validate_source_name_arg(
field_name: &Name,
object_name: &Name,
Expand Down Expand Up @@ -112,7 +125,7 @@ impl SourceName {
directive_name,
value: arg.value.clone(),
}
} else if str_value.chars().all(|c| c.is_alphanumeric() || c == '_') {
} else if patterns::SOURCE_NAME_REGEX.is_match(str_value) {
Self::Valid {
value: arg.value.clone(),
directive_name,
Expand All @@ -132,7 +145,8 @@ impl SourceName {
value,
directive_name,
} => Err(Message {
message: format!("There are invalid characters in {coordinate}. Only alphanumeric and underscores are allowed.", coordinate = source_name_value_coordinate(&directive_name, &value)),
// This message is the same as Studio when trying to publish a subgraph with an invalid name
message: format!("{coordinate} is invalid; all source names must follow pattern '^[a-zA-Z][a-zA-Z0-9_-]{{0,63}}$", coordinate = source_name_value_coordinate(&directive_name, &value)),
code: Code::InvalidSourceName,
locations: value.line_column_range(sources).into_iter().collect(),
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,24 @@ extend schema
import: ["@connect", "@source"]
)
@source(name: "u$ers", http: { baseURL: "http://127.0.0.1" })
@source(name: "1", http: { baseURL: "http://127.0.0.1" })
@source(name: "no.dots", http: { baseURL: "http://127.0.0.1" })
@source(
name: "areallylongnamethatisoversixtythreecharacterstakesalongwhiletotypebutthisshoulddoit"
http: { baseURL: "http://127.0.0.1" }
)

type Query {
resources: [String!]!
resources1: [String!]!
@connect(source: "u$ers", http: { GET: "/resources" }, selection: "$")
resources2: [String!]!
@connect(source: "1", http: { GET: "/resources" }, selection: "$")
resources3: [String!]!
@connect(source: "no.dots", http: { GET: "/resources" }, selection: "$")
resources4: [String!]!
@connect(
source: "areallylongnamethatisoversixtythreecharacterstakesalongwhiletotypebutthisshoulddoit"
http: { GET: "/resources" }
selection: "$"
)
}

0 comments on commit 23a2efd

Please sign in to comment.