Skip to content

Commit

Permalink
fix hex color schema pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
amrbashir committed Oct 25, 2024
1 parent 0835ca0 commit b2e2091
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 32 deletions.
9 changes: 5 additions & 4 deletions Cargo.lock

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

12 changes: 5 additions & 7 deletions crates/tauri-cli/config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -838,10 +838,12 @@
]
},
"Color": {
"$ref": "#/definitions/InnerColor"
},
"InnerColor": {
"anyOf": [
{
"description": "Color hex string, for example: #fff, #ffffff, or #ffffffff.",
"type": "string",
"pattern": "#?([A-Fa-f0-9]{3}|[A-Fa-f0-9]{6}|[A-Fa-f0-9]{8})$"
},
{
"description": "Array of RGB colors. Each value has minimum of 0 and maximum of 255.",
"type": "array",
Expand Down Expand Up @@ -924,10 +926,6 @@
"minimum": 0.0
}
}
},
{
"description": "Color hex string, for example: #fff, #ffffff, or #ffffffff.",
"type": "string"
}
]
},
Expand Down
12 changes: 5 additions & 7 deletions crates/tauri-schema-generator/schemas/config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -838,10 +838,12 @@
]
},
"Color": {
"$ref": "#/definitions/InnerColor"
},
"InnerColor": {
"anyOf": [
{
"description": "Color hex string, for example: #fff, #ffffff, or #ffffffff.",
"type": "string",
"pattern": "#?([A-Fa-f0-9]{3}|[A-Fa-f0-9]{6}|[A-Fa-f0-9]{8})$"
},
{
"description": "Array of RGB colors. Each value has minimum of 0 and maximum of 255.",
"type": "array",
Expand Down Expand Up @@ -924,10 +926,6 @@
"minimum": 0.0
}
}
},
{
"description": "Color hex string, for example: #fff, #ffffff, or #ffffffff.",
"type": "string"
}
]
},
Expand Down
2 changes: 1 addition & 1 deletion crates/tauri-schema-worker/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ async fn try_next_schema() -> anyhow::Result<String> {

async fn schema_file_for_version(version: Version) -> anyhow::Result<String> {
let cache = Cache::open("schema".to_string()).await;
let cache_key = format!("https://scheam.tauri.app/config/{version}");
let cache_key = format!("https://schema.tauri.app/config/{version}");
if let Some(mut cached) = cache.get(cache_key.clone(), true).await? {
console_log!("Serving schema for {version} from cache");
return cached.text().await.map_err(Into::into);
Expand Down
3 changes: 1 addition & 2 deletions crates/tauri-utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ log = "0.4.21"
cargo_metadata = { version = "0.18", optional = true }
serde-untagged = "0.1"
uuid = { version = "1", features = ["serde"] }
once_cell = { version = "1", optional = true }

[target."cfg(target_os = \"macos\")".dependencies]
swift-rs = { version = "1.0.7", optional = true, features = ["build"] }
Expand All @@ -58,7 +57,7 @@ serial_test = "3.1"
[features]
build = ["proc-macro2", "quote", "cargo_metadata", "schema", "swift-rs"]
compression = ["brotli"]
schema = ["schemars", "once_cell"]
schema = ["schemars"]
isolation = ["aes-gcm", "getrandom", "serialize-to-javascript"]
process-relaunch-dangerous-allow-symlink-macos = []
config-json5 = ["json5"]
Expand Down
26 changes: 15 additions & 11 deletions crates/tauri-utils/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1323,15 +1323,12 @@ fn default_alpha() -> u8 {
255
}

#[cfg(feature = "schema")]
static HEX_COLOR_RE: once_cell::sync::Lazy<regex::Regex> = once_cell::sync::Lazy::new(|| {
regex::Regex::new(r"^#?([A-Fa-f0-9]{3}|[A-Fa-f0-9]{6}|[A-Fa-f0-9]{8})$").unwrap()
});

#[derive(Deserialize)]
#[cfg_attr(feature = "schema", derive(JsonSchema))]
#[serde(untagged)]
enum InnerColor {
/// Color hex string, for example: #fff, #ffffff, or #ffffffff.
String(String),
/// Array of RGB colors. Each value has minimum of 0 and maximum of 255.
Rgb((u8, u8, u8)),
/// Array of RGBA colors. Each value has minimum of 0 and maximum of 255.
Expand All @@ -1344,9 +1341,6 @@ enum InnerColor {
#[serde(default = "default_alpha")]
alpha: u8,
},
/// Color hex string, for example: #fff, #ffffff, or #ffffffff.
#[cfg_attr(feature = "schema", validate(regex(path = *HEX_COLOR_RE)))]
String(String),
}

impl<'de> Deserialize<'de> for Color {
Expand All @@ -1356,6 +1350,7 @@ impl<'de> Deserialize<'de> for Color {
{
let color = InnerColor::deserialize(deserializer)?;
let color = match color {
InnerColor::String(string) => string.parse().map_err(serde::de::Error::custom)?,
InnerColor::Rgb(rgb) => Color(rgb.0, rgb.1, rgb.2, 255),
InnerColor::Rgba(rgb) => rgb.into(),
InnerColor::RgbaObject {
Expand All @@ -1364,7 +1359,6 @@ impl<'de> Deserialize<'de> for Color {
blue,
alpha,
} => Color(red, green, blue, alpha),
InnerColor::String(string) => string.parse().map_err(serde::de::Error::custom)?,
};

Ok(color)
Expand All @@ -1377,8 +1371,18 @@ impl schemars::JsonSchema for Color {
"Color".to_string()
}

fn json_schema(gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema {
gen.subschema_for::<InnerColor>()
fn json_schema(_gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema {
let mut schema = schemars::schema_for!(InnerColor).schema;
schema.metadata = None; // Remove `title: InnerColor` from schema

// add hex color pattern validation
let any_of = schema.subschemas().any_of.as_mut().unwrap();
let schemars::schema::Schema::Object(str_schema) = any_of.first_mut().unwrap() else {
unreachable!()
};
str_schema.string().pattern = Some("#?([A-Fa-f0-9]{3}|[A-Fa-f0-9]{6}|[A-Fa-f0-9]{8})$".into());

schema.into()
}
}

Expand Down

0 comments on commit b2e2091

Please sign in to comment.