1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
use crate::assets::{DP_VAL_KEYS_LOOKUP, REGISTERED_SSVC_NAMESPACES, SSVC_DECISION_POINTS};
use crate::namespaces::{BaseNamespace, validate_namespace};
use crate::selection_list::SelectionList;
use std::ops::Deref;
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct ValidationResult {
pub success: bool,
pub errors: Vec<ValidationError>,
}
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct ValidationError {
pub message: String,
#[serde(rename = "instancePath")]
pub instance_path: Vec<String>,
}
/// Helper function for validating field matches between selection and base
/// decision points.
///
/// Returns a ValidationError if the selection field is Some and does not match
/// the base field, otherwise returns None.
#[allow(clippy::too_many_arguments)]
fn validate_field_match<S, B>(
selection_field: &Option<S>,
base_field: &B,
field_name: &str,
ext_namespace: &str,
base_namespace: &str,
context: &str,
i_s: usize,
path_suffix: &[&str],
) -> Option<ValidationError>
where
S: Deref<Target = String>,
B: Deref<Target = String>,
{
if let Some(sel_field) = selection_field
&& sel_field.deref() != base_field.deref()
{
let mut instance_path = vec!["selections".to_string(), i_s.to_string()];
instance_path.extend(path_suffix.iter().map(|s| s.to_string()));
instance_path.push(field_name.to_string());
return Some(ValidationError {
message: format!(
"Extension namespace '{}' {} {} '{}' does not match base namespace '{}' {} '{}'",
ext_namespace,
context,
field_name,
sel_field.deref(),
base_namespace,
field_name,
base_field.deref()
),
instance_path,
});
}
None
}
/// Main validation function for SSVC SelectionList. Validates that all
/// selections with registered SSVC namespaces conform to the structure of their
/// corresponding decision points, including extension rules.
///
/// # Arguments
/// * `selection_list` - The SelectionList to validate
/// * `allow_test_namespaces` - Whether to allow namespaces with "test"
/// extensions
pub fn validate_selection_list(
selection_list: &SelectionList,
allow_test_namespaces: bool,
) -> ValidationResult {
let mut errors: Vec<ValidationError> = Vec::new();
for (i_s, selection) in selection_list.selections.iter().enumerate() {
// Parse and validate namespace structure
let parsed_ns = match validate_namespace(selection.namespace.deref(), allow_test_namespaces)
{
Ok(ns) => ns,
Err(err) => {
errors.push(ValidationError {
message: format!("Invalid SSVC namespace: {}", err),
instance_path: vec![
"selections".to_string(),
i_s.to_string(),
"namespace".to_string(),
],
});
continue;
}
};
// Extract base namespace name (without extensions and fragment)
let base_name = match parsed_ns.base {
BaseNamespace::Registered { name, .. } => name,
// Skip unregistered namespaces - they are not validated against known decision points
BaseNamespace::Unregistered { .. } => continue,
};
// Skip if the base namespace is not explicitly registered in SSVC
if !REGISTERED_SSVC_NAMESPACES.contains(base_name.as_str()) {
continue;
}
// Look up the decision point using base namespace (without extensions).
// This implements the extension validation rule: extensions apply to
// registered base namespaces and must follow the same decision point structure.
let s_key = selection.key.deref().to_owned();
let version = selection.version.deref().to_owned();
let dp_key = (base_name.clone(), s_key.clone(), version.clone());
match SSVC_DECISION_POINTS.get(&dp_key) {
Some(dp) => {
// Get value indices of decision point from base namespace
let reference_indices = DP_VAL_KEYS_LOOKUP.get(&dp_key).unwrap();
// Validate extension rules:
// - Extensions can limit values (subset) but not add new ones
// - Extensions cannot change the order of values
// - Extensions can translate/refine name and definition but not change key
// All validation is done against the base namespace decision point
// If the namespace has no extensions, validate that name and definition match the base (if provided)
if parsed_ns.extensions.is_empty() {
if let Some(error) = validate_field_match(
&selection.name,
&dp.name,
"name",
selection.namespace.deref(),
&base_name,
"decision point",
i_s,
&[],
) {
errors.push(error);
}
if let Some(error) = validate_field_match(
&selection.definition,
&dp.definition,
"definition",
selection.namespace.deref(),
&base_name,
"decision point",
i_s,
&[],
) {
errors.push(error);
}
}
let mut last_index: i32 = -1;
// Check if all values exist in the base decision point and are correctly ordered.
for (i_val, sel_val) in selection.values.iter().enumerate() {
let v_key = sel_val.key.deref();
match reference_indices.get(v_key) {
None => {
// The value is not found in the base decision point
errors.push(ValidationError {
message: format!(
"The SSVC decision point '{}::{}' (version {}) doesn't have a value with key '{}'",
selection.namespace.deref(),
dp.name.deref(),
version,
v_key
),
instance_path: vec![
"selections".to_string(),
i_s.to_string(),
"values".to_string(),
i_val.to_string(),
],
});
continue;
}
Some(i_dp_val) => {
// Verify order is maintained (subset must preserve order from base)
if last_index > *i_dp_val {
errors.push(ValidationError {
message: format!(
"The values for SSVC decision point '{}::{}' (version {}) are not in correct order",
selection.namespace.deref(),
dp.name.deref(),
version
),
instance_path: vec![
"selections".to_string(),
i_s.to_string(),
"values".to_string(),
i_val.to_string(),
],
});
continue;
}
last_index = *i_dp_val;
// If the namespace has no extensions, validate value name and definition match base (if provided)
if parsed_ns.extensions.is_empty() {
let base_val = &dp.values[*i_dp_val as usize];
let context = format!("value '{}'", v_key);
if let Some(error) = validate_field_match(
&sel_val.name,
&base_val.name,
"name",
selection.namespace.deref(),
&base_name,
&context,
i_s,
&["values", &i_val.to_string()],
) {
errors.push(error);
}
if let Some(error) = validate_field_match(
&sel_val.definition,
&base_val.definition,
"definition",
selection.namespace.deref(),
&base_name,
&context,
i_s,
&["values", &i_val.to_string()],
) {
errors.push(error);
}
}
}
}
}
}
None => {
errors.push(ValidationError {
message: format!(
"Unknown SSVC decision point '{}::{}' with version '{}'",
selection.namespace.deref(),
s_key,
version
),
instance_path: vec!["selections".to_string(), i_s.to_string()],
});
continue;
}
}
}
ValidationResult {
success: errors.is_empty(),
errors,
}
}
#[cfg(test)]
mod tests {
use super::ValidationError;
#[test]
fn validation_error_is_serde_serializable() {
let error = ValidationError {
message: "example".to_string(),
instance_path: vec!["selections".to_string(), "0".to_string()],
};
let json = serde_json::to_value(&error).expect("serialize ValidationError");
assert_eq!(
json.get("message").and_then(|v| v.as_str()),
Some("example")
);
assert_eq!(
json.get("instancePath")
.and_then(|v| v.as_array())
.map(|a| a.len()),
Some(2)
);
let roundtrip: ValidationError =
serde_json::from_value(json).expect("deserialize ValidationError");
assert_eq!(roundtrip, error);
}
}