Skip to content

Commit f221cb4

Browse files
committed
Expand validation for Component
* fix test Signed-off-by: Sebastian Ziebell <[email protected]>
1 parent a70ebeb commit f221cb4

File tree

2 files changed

+63
-215
lines changed

2 files changed

+63
-215
lines changed

cyclonedx-bom/src/models/component.rs

Lines changed: 37 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
use once_cell::sync::Lazy;
2020
use regex::Regex;
2121

22-
use crate::external_models::uri::validate_uri;
22+
use crate::external_models::normalized_string::validate_normalized_string;
23+
use crate::external_models::uri::{validate_purl, validate_uri};
2324
use crate::models::attached_text::AttachedText;
2425
use crate::models::code::{Commits, Patches};
2526
use crate::models::external_reference::ExternalReferences;
@@ -114,126 +115,41 @@ impl Validate for Component {
114115
)
115116
.add_field_option("mime_type", self.mime_type.as_ref(), validate_mime_type)
116117
.add_struct_option("supplier", self.supplier.as_ref(), version)
118+
.add_field_option("author", self.author.as_ref(), validate_normalized_string)
119+
.add_field_option(
120+
"publisher",
121+
self.publisher.as_ref(),
122+
validate_normalized_string,
123+
)
124+
.add_field_option("group", self.group.as_ref(), validate_normalized_string)
125+
.add_field("name", self.name.as_ref(), validate_normalized_string)
126+
.add_field_option("version", self.version.as_ref(), validate_normalized_string)
127+
.add_field_option(
128+
"description",
129+
self.description.as_ref(),
130+
validate_normalized_string,
131+
)
132+
.add_enum_option("scope", self.scope.as_ref(), validate_scope)
133+
.add_struct_option("hashes", self.hashes.as_ref(), version)
134+
.add_struct_option("licenses", self.licenses.as_ref(), version)
135+
.add_field_option(
136+
"copyright",
137+
self.copyright.as_ref(),
138+
validate_normalized_string,
139+
)
140+
.add_field_option("cpe", self.cpe.as_ref(), validate_cpe)
141+
.add_field_option("purl", self.purl.as_ref(), validate_purl)
142+
.add_struct_option("swid", self.swid.as_ref(), version)
143+
.add_struct_option("pedigree", self.pedigree.as_ref(), version)
144+
.add_struct_option(
145+
"external_references",
146+
self.external_references.as_ref(),
147+
version,
148+
)
149+
.add_struct_option("properties", self.properties.as_ref(), version)
150+
.add_struct_option("components", self.components.as_ref(), version)
151+
.add_struct_option("evidence", self.evidence.as_ref(), version)
117152
.into()
118-
119-
/*
120-
if let Some(supplier) = &self.supplier {
121-
let context = context.with_struct("Component", "supplier");
122-
123-
results.push(supplier.validate_with_context(context));
124-
}
125-
126-
if let Some(author) = &self.author {
127-
let context = context.with_struct("Component", "author");
128-
129-
results.push(author.validate_with_context(context));
130-
}
131-
132-
if let Some(publisher) = &self.publisher {
133-
let context = context.with_struct("Component", "publisher");
134-
135-
results.push(publisher.validate_with_context(context));
136-
}
137-
138-
if let Some(group) = &self.group {
139-
let context = context.with_struct("Component", "group");
140-
141-
results.push(group.validate_with_context(context));
142-
}
143-
144-
let name_context = context.with_struct("Component", "name");
145-
146-
results.push(self.name.validate_with_context(name_context));
147-
148-
if let Some(version) = &self.version {
149-
let context = context.with_struct("Component", "version");
150-
151-
results.push(version.validate_with_context(context));
152-
}
153-
154-
if let Some(description) = &self.description {
155-
let context = context.with_struct("Component", "description");
156-
157-
results.push(description.validate_with_context(context));
158-
}
159-
160-
if let Some(scope) = &self.scope {
161-
let context = context.with_struct("Component", "scope");
162-
163-
results.push(scope.validate_with_context(context));
164-
}
165-
166-
if let Some(hashes) = &self.hashes {
167-
let context = context.with_struct("Component", "hashes");
168-
169-
results.push(hashes.validate_with_context(context));
170-
}
171-
172-
if let Some(licenses) = &self.licenses {
173-
let context = context.with_struct("Component", "licenses");
174-
175-
results.push(licenses.validate_with_context(context));
176-
}
177-
178-
if let Some(copyright) = &self.copyright {
179-
let context = context.with_struct("Component", "copyright");
180-
181-
results.push(copyright.validate_with_context(context));
182-
}
183-
184-
if let Some(cpe) = &self.cpe {
185-
let context = context.with_struct("Component", "cpe");
186-
187-
results.push(cpe.validate_with_context(context));
188-
}
189-
190-
if let Some(purl) = &self.purl {
191-
let context = context.with_struct("Component", "purl");
192-
193-
results.push(purl.validate_with_context(context));
194-
}
195-
196-
if let Some(swid) = &self.swid {
197-
let context = context.with_struct("Component", "swid");
198-
199-
results.push(swid.validate_with_context(context));
200-
}
201-
202-
if let Some(pedigree) = &self.pedigree {
203-
let context = context.with_struct("Component", "pedigree");
204-
205-
results.push(pedigree.validate_with_context(context));
206-
}
207-
208-
if let Some(external_references) = &self.external_references {
209-
let context = context.with_struct("Component", "external_references");
210-
211-
results.push(external_references.validate_with_context(context));
212-
}
213-
214-
if let Some(properties) = &self.properties {
215-
let context = context.with_struct("Component", "properties");
216-
217-
results.push(properties.validate_with_context(context));
218-
}
219-
220-
if let Some(components) = &self.components {
221-
let context = context.with_struct("Component", "components");
222-
223-
results.push(components.validate_with_context(context));
224-
}
225-
226-
if let Some(evidence) = &self.evidence {
227-
let context = context.with_struct("Component", "evidence");
228-
229-
results.push(evidence.validate_with_context(context));
230-
}
231-
232-
results
233-
.into_iter()
234-
.fold(ValidationResult::default(), |acc, result| acc.merge(result))
235-
236-
*/
237153
}
238154
}
239155

@@ -512,7 +428,7 @@ pub struct Copyright(pub String);
512428
pub struct CopyrightTexts(pub(crate) Vec<Copyright>);
513429

514430
impl Validate for CopyrightTexts {
515-
fn validate(&self, version: SpecVersion) -> ValidationResult {
431+
fn validate(&self, _version: SpecVersion) -> ValidationResult {
516432
ValidationContext::new()
517433
.add_list("inner", &self.0, validate_copyright)
518434
.into()

cyclonedx-bom/src/models/vulnerability_rating.rs

Lines changed: 26 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
use ordered_float::OrderedFloat;
2020

21-
use crate::external_models::normalized_string::NormalizedString;
21+
use crate::external_models::normalized_string::{validate_normalized_string, NormalizedString};
2222
use crate::models::vulnerability_source::VulnerabilitySource;
2323
use crate::validation::{Validate, ValidationContext, ValidationError, ValidationResult};
2424

@@ -63,32 +63,15 @@ impl VulnerabilityRating {
6363
// todo: how to decide what to validate, check this
6464
impl Validate for VulnerabilityRating {
6565
fn validate(&self, version: SpecVersion) -> ValidationResult {
66-
ValidationContext::new().into()
67-
/*
68-
let mut results: Vec<ValidationResult> = vec![];
69-
70-
if let Some(vulnerability_source) = &self.vulnerability_source {
71-
let context = context.with_struct("VulnerabilityRating", "vulnerability_source");
72-
73-
results.push(vulnerability_source.validate_with_context(context));
74-
}
75-
76-
if let Some(severity) = &self.severity {
77-
let context = context.with_struct("VulnerabilityRating", "severity");
78-
79-
results.push(severity.validate_with_context(context));
80-
}
81-
82-
if let Some(vector) = &self.vector {
83-
let context = context.with_struct("VulnerabilityRating", "vector");
84-
85-
results.push(vector.validate_with_context(context));
86-
}
87-
88-
results
89-
.into_iter()
90-
.fold(ValidationResult::default(), |acc, result| acc.merge(result))
91-
*/
66+
ValidationContext::new()
67+
.add_struct_option(
68+
"vulnerability_source",
69+
self.vulnerability_source.as_ref(),
70+
version,
71+
)
72+
.add_enum_option("severity", self.severity.as_ref(), validate_severity)
73+
.add_field_option("vector", self.vector.as_ref(), validate_normalized_string)
74+
.into()
9275
}
9376
}
9477

@@ -278,77 +261,26 @@ mod test {
278261
"inner",
279262
[(
280263
0,
281-
validation::r#struct(
282-
"vulnerability_source",
283-
vec![validation::field(
264+
vec![
265+
validation::r#struct(
266+
"vulnerability_source",
267+
vec![validation::field(
284268
"name",
285-
"NormalizedString contains invalid characters \\r \\n \\t or \\r\\n"
269+
"NormalizedString contains invalid characters \\r \\n \\t or \\r\\n",
270+
),
271+
validation::field(
272+
"url",
273+
"Uri does not conform to RFC 3986",
286274
)]
287-
)
275+
),
276+
validation::r#enum("severity", "Undefined severity"),
277+
validation::field(
278+
"vector",
279+
"NormalizedString contains invalid characters \\r \\n \\t or \\r\\n"
280+
)
281+
],
288282
)]
289283
))
290284
);
291-
292-
/*
293-
assert_eq!(
294-
validation_result,
295-
ValidationResult::Failed {
296-
reasons: vec![
297-
FailureReason {
298-
message:
299-
"NormalizedString contains invalid characters \\r \\n \\t or \\r\\n"
300-
.to_string(),
301-
context: ValidationContext(vec![
302-
ValidationPathComponent::Array { index: 0 },
303-
ValidationPathComponent::Struct {
304-
struct_name: "VulnerabilityRating".to_string(),
305-
field_name: "vulnerability_source".to_string()
306-
},
307-
ValidationPathComponent::Struct {
308-
struct_name: "VulnerabilitySource".to_string(),
309-
field_name: "name".to_string()
310-
},
311-
])
312-
},
313-
FailureReason {
314-
message: "Uri does not conform to RFC 3986".to_string(),
315-
context: ValidationContext(vec![
316-
ValidationPathComponent::Array { index: 0 },
317-
ValidationPathComponent::Struct {
318-
struct_name: "VulnerabilityRating".to_string(),
319-
field_name: "vulnerability_source".to_string()
320-
},
321-
ValidationPathComponent::Struct {
322-
struct_name: "VulnerabilitySource".to_string(),
323-
field_name: "url".to_string()
324-
},
325-
])
326-
},
327-
FailureReason {
328-
message: "Undefined severity".to_string(),
329-
context: ValidationContext(vec![
330-
ValidationPathComponent::Array { index: 0 },
331-
ValidationPathComponent::Struct {
332-
struct_name: "VulnerabilityRating".to_string(),
333-
field_name: "severity".to_string()
334-
}
335-
])
336-
},
337-
FailureReason {
338-
message:
339-
"NormalizedString contains invalid characters \\r \\n \\t or \\r\\n"
340-
.to_string(),
341-
context: ValidationContext(vec![
342-
ValidationPathComponent::Array { index: 0 },
343-
ValidationPathComponent::Struct {
344-
struct_name: "VulnerabilityRating".to_string(),
345-
field_name: "vector".to_string()
346-
},
347-
])
348-
},
349-
]
350-
}
351-
);
352-
*/
353285
}
354286
}

0 commit comments

Comments
 (0)