Validate a label selector string format. Format: "key1=value1,key2=value2" Each key and value is validated using `validate_label_key` and `validate_label_value`. Empty selectors are allowed. Trailing commas are ignored.
(selector: &str)
| 538 | /// Each key and value is validated using `validate_label_key` and `validate_label_value`. |
| 539 | /// Empty selectors are allowed. Trailing commas are ignored. |
| 540 | pub(super) fn validate_label_selector(selector: &str) -> Result<(), Status> { |
| 541 | if selector.trim().is_empty() { |
| 542 | return Ok(()); |
| 543 | } |
| 544 | |
| 545 | for pair in selector.split(',') { |
| 546 | let pair = pair.trim(); |
| 547 | if pair.is_empty() { |
| 548 | continue; |
| 549 | } |
| 550 | |
| 551 | let parts: Vec<&str> = pair.splitn(2, '=').collect(); |
| 552 | if parts.len() != 2 { |
| 553 | return Err(Status::invalid_argument(format!( |
| 554 | "invalid label selector: expected 'key=value', got '{pair}'" |
| 555 | ))); |
| 556 | } |
| 557 | |
| 558 | let key = parts[0].trim(); |
| 559 | let value = parts[1].trim(); |
| 560 | |
| 561 | if key.is_empty() { |
| 562 | return Err(Status::invalid_argument(format!( |
| 563 | "invalid label selector: key cannot be empty in '{pair}'" |
| 564 | ))); |
| 565 | } |
| 566 | |
| 567 | // Validate key and value using the Kubernetes-compliant validators |
| 568 | validate_label_key(key)?; |
| 569 | validate_label_value(value)?; |
| 570 | } |
| 571 | |
| 572 | Ok(()) |
| 573 | } |
| 574 | |
| 575 | // --------------------------------------------------------------------------- |
| 576 | // Object metadata validation |