[pdm-devel] [PATCH datacenter-manager] pdm-api-types: views: use regex to verify resource-id values

Lukas Wagner l.wagner at proxmox.com
Thu Nov 13 11:47:27 CET 2025


Suggested-by: Dominik Csapak <d.csapak at proxmox.com>
Signed-off-by: Lukas Wagner <l.wagner at proxmox.com>
---

To be applied on top of v4 of my 'view filter' patch series

 lib/pdm-api-types/src/views.rs | 22 +++++++++++++++-------
 1 file changed, 15 insertions(+), 7 deletions(-)

diff --git a/lib/pdm-api-types/src/views.rs b/lib/pdm-api-types/src/views.rs
index 2bed68f3..ef39cc62 100644
--- a/lib/pdm-api-types/src/views.rs
+++ b/lib/pdm-api-types/src/views.rs
@@ -1,15 +1,24 @@
 use std::{fmt::Display, str::FromStr, sync::OnceLock};
 
 use anyhow::bail;
-use proxmox_section_config::{typed::ApiSectionDataEntry, SectionConfig, SectionConfigPlugin};
+use const_format::concatcp;
 use serde::{Deserialize, Serialize};
 
-use proxmox_schema::{api, ApiStringFormat, ApiType, ArraySchema, Schema, StringSchema, Updater};
+use proxmox_schema::{
+    api, api_types::SAFE_ID_REGEX_STR, const_regex, ApiStringFormat, ApiType, ArraySchema, Schema,
+    StringSchema, Updater,
+};
+use proxmox_section_config::{typed::ApiSectionDataEntry, SectionConfig, SectionConfigPlugin};
 
 use crate::{
     remotes::REMOTE_ID_SCHEMA, resource::ResourceType, PROXMOX_SAFE_ID_REGEX, VIEW_ID_SCHEMA,
 };
 
+const_regex! {
+    /// Regex for matching global resource IDs
+    pub GLOBAL_RESOURCE_ID_REGEX = concatcp!(r"^", SAFE_ID_REGEX_STR, r"(\/", SAFE_ID_REGEX_STR, r")+$");
+}
+
 /// Schema for filter rules.
 pub const FILTER_RULE_SCHEMA: Schema = StringSchema::new("Filter rule for resources.")
     .format(&ApiStringFormat::VerifyFn(verify_filter_rule))
@@ -121,12 +130,10 @@ impl FromStr for FilterRule {
                 FilterRule::ResourcePool(value.to_string())
             }
             Some(("resource-id", value)) => {
-                // TODO: Define schema and use it for validation. Can't use PROXMOX_SAFE_ID_REGEX
-                // since it does not allow '/'.
-
-                if value.is_empty() {
-                    bail!("empty resource-id rule not allowed");
+                if !GLOBAL_RESOURCE_ID_REGEX.is_match(value) {
+                    bail!("invalid resource-id value: {value}");
                 }
+
                 FilterRule::ResourceId(value.to_string())
             }
             Some(("tag", value)) => {
@@ -192,6 +199,7 @@ mod test {
 
         assert!(parse_and_check_display("resource-id:").is_err());
         assert!(parse_and_check_display("resource-id:remote/someremote/guest/100").unwrap());
+        assert!(parse_and_check_display("resource-id:remote").is_err());
 
         assert!(parse_and_check_display("tag:").is_err());
         assert!(parse_and_check_display("tag:sometag").unwrap());
-- 
2.47.3





More information about the pdm-devel mailing list