stixflayer 0.1.0

Fast STIX 2.1 parsing, validation, and serialization in Rust
Documentation
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
//! A custom STIX Error type, with its associated functions.
use std::num::ParseIntError;

use jiff::Error as JiffError;
use strum::ParseError;
use thiserror::Error;

use crate::types::Identifier;

pub mod classify;
pub mod envelope;
pub mod helpers;

pub use classify::{classify_serde_error, json_kind, non_object_json_message};
pub use envelope::{ErrorEntry, ErrorEnvelope};
pub use helpers::{add_error, return_multiple_errors};

/// Custom Error type for rust-stix
///
/// Every variant yields at least one structured [`ErrorEntry`] via
/// [`StixError::errors`]; variants with natural structure produce rich entries
/// (see `to_entry`), all others fall back to a catch-all entry whose `kind` is
/// the variant name in snake_case. Kind strings are cross-language API surface:
/// adding variants is additive, renaming kinds is breaking and avoided.
#[derive(Debug, Clone, Error, strum::EnumDiscriminants)]
#[strum_discriminants(derive(strum::AsRefStr))]
#[strum_discriminants(strum(serialize_all = "snake_case"))]
pub enum StixError {
    // For retunning multiple errors at once, e.g. during validation
    #[error("Validation errors: {0:?}")]
    ValidationErrors(Vec<StixError>),
    // Basic parsing or validation errors
    #[error("DateTime error: {0}")]
    DateTimeError(JiffError),
    #[error("Empty Lists and dictionaries are prohibted in STIX")]
    EmptyList,
    #[error("The hash value '{hash_string}' is not a valid {hash_type} hash (detected format: {hash_identity}).")]
    InvalidHash {
        hash_type: String,
        hash_identity: String,
        hash_string: String,
    },
    #[error("Invalid UUID in object id: {message}")]
    InvalidUuid { message: String },
    #[error("{object} of type {object_type} do not have a '{field}' field.")]
    IllegalBuilderProperty {
        object: String,
        object_type: String,
        field: String,
    },
    #[error("A Relationship Object of type {relationship} cannot exist from a {source_type} to a {target_type}")]
    InvalidRelationship {
        relationship: String,
        source_type: String,
        target_type: String,
    },
    #[error("Object of type {object_type} must have the '{property}' property set.")]
    MissingBuilderProperty {
        object_type: String,
        property: String,
    },
    #[error("Could not parse String {0} as a valid hex")]
    ParseHexError(String),
    #[error("Could not parse String {0} as a valid STIX 2.1 Identifier")]
    ParseIdentifierError(String),
    #[error("Error parsing integer: {0}")]
    ParseIntegerError(ParseIntError),
    #[error("Could not parse timestamp {0} as a valid STIX 2.1 Timestamp")]
    ParseTimestampError(String),
    #[error("Failed to parse STIX Pattern {0}: {1}")]
    ParsePatternError(String, String),
    // General validation error
    #[error("STIX validation error: {0}")]
    ValidationError(String),
    /// An object-level check (SDO/SCO/SRO) failed because of an inner error.
    ///
    /// `context` names the object and the failed check; the display message
    /// embeds the inner message without repeating the `STIX validation error:`
    /// prefix, and the inner error's structured entries are preserved so
    /// field-level detail (e.g. vocabulary context) survives nesting.
    #[error("STIX validation error: {context}: {}", .source.embedded_message())]
    InvalidNestedObject {
        context: String,
        source: Box<StixError>,
    },
    #[error("STIX validation error: Value '{value}' is not a valid {vocabulary} value.")]
    InvalidVocabValue { value: String, vocabulary: String },
    // Extension errors
    #[error("Extension key is not a known predefined extension")]
    UnknownExtension,
    #[error("Wrong extension type for object")]
    WrongExtension,
    // Versioning errors
    #[error("Cannot version this object: {0}")]
    UnableToVersion(String),
    #[error("{0} is not valid version timestamp: {1}")]
    BadVersion(String, JiffError),
    // Customization errors
    #[error("Custom objects must have an extension with an appropriate new_object extension type")]
    CustomMissingExtension,
    // (De)serialization and JSON parsing related errors
    #[error("JSON nulls are not allowed in STIX 2.1")]
    JsonNull,
    #[error("Unexpected JSON format")]
    UnexpectedJsonFormat,
    #[error("Unknown fields in JSON: {0:?}")]
    UnknownFields(Vec<String>),
    #[error("Unrecognized STIX Object or Object Type")]
    UnrecognizedObject(ParseError),
    // `serde_json:Error`s are converted to Strings during error mapping because that Error type does not `impl Clone`
    #[error("Serialization error: {0}")]
    SerializationError(String),
    #[error("Deserialization error: {0}")]
    DeserializationError(String),
    // Structured (de)serialization errors, produced by classify_serde_error
    // and the collect-all validation flow. These carry the STIX object type
    // so bindings can render actionable messages without re-parsing strings.
    #[error("`{object_type}` is missing required property '{property}'")]
    MissingProperty {
        object_type: String,
        property: String,
    },
    #[error("`{object_type}` has unknown properties: {fields:?}")]
    UnknownProperties {
        object_type: String,
        fields: Vec<String>,
    },
    #[error("`{object_type}` has an invalid value for property '{property}': expected {expected}, got {got}")]
    InvalidPropertyType {
        object_type: String,
        property: String,
        expected: String,
        got: String,
    },
    // Datastore errors
    #[error("Data source not found")]
    MissingDataSource,
    #[error("Data sink not found")]
    MissingDataSink,
    #[error("Data type {0} is not supported in rust-stix")]
    UnsupportedDataType(String),
    // Filesystem access errors
    #[error("File I/O error: {0}")]
    IoError(String),
    #[error("File {0} not found")]
    PathNotFound(String),
    #[error("Object {0} not in filesystem")]
    ObjectNotFound(Identifier),
}

impl StixError {
    /// Returns the envelope category for this error.
    ///
    /// Categories are: `validation`, `deserialization`, `stix`.
    pub fn envelope_category(&self) -> &'static str {
        match self {
            StixError::ValidationErrors(_)
            | StixError::MissingProperty { .. }
            | StixError::UnknownProperties { .. }
            | StixError::InvalidPropertyType { .. }
            | StixError::ValidationError(_)
            | StixError::InvalidNestedObject { .. }
            | StixError::InvalidVocabValue { .. }
            | StixError::ParsePatternError(_, _)
            | StixError::WrongExtension
            | StixError::CustomMissingExtension
            | StixError::UnknownExtension => "validation",
            StixError::DeserializationError(_)
            | StixError::SerializationError(_)
            | StixError::UnexpectedJsonFormat
            | StixError::JsonNull => "deserialization",
            _ => "stix",
        }
    }

    /// Returns this error's message without the leading `STIX validation error: `
    /// prefix, so a larger validation message can embed it without repeating
    /// the prefix. Errors that never carry the prefix are returned unchanged.
    pub fn embedded_message(&self) -> String {
        let message = self.to_string();
        message
            .strip_prefix("STIX validation error: ")
            .unwrap_or(&message)
            .to_string()
    }

    /// Converts a structured validation variant into an [`ErrorEntry`].
    fn to_entry(&self) -> Option<ErrorEntry> {
        match self {
            StixError::MissingProperty { property, .. } => Some(ErrorEntry {
                kind: "missing_property".to_string(),
                message: self.to_string(),
                property: Some(property.clone()),
                ..Default::default()
            }),
            StixError::UnknownProperties { fields, .. } => Some(ErrorEntry {
                kind: "unknown_property".to_string(),
                message: self.to_string(),
                properties: Some(fields.clone()),
                ..Default::default()
            }),
            StixError::InvalidPropertyType {
                property,
                expected,
                got,
                ..
            } => Some(ErrorEntry {
                kind: "invalid_property_type".to_string(),
                message: self.to_string(),
                property: Some(property.clone()),
                expected: Some(expected.clone()),
                got: Some(got.clone()),
                ..Default::default()
            }),
            StixError::InvalidNestedObject { source, .. } => source.to_entry().map(|mut entry| {
                // Keep the composed, context-aware message on the entry while
                // preserving the inner error's structured fields.
                entry.message = self.to_string();
                entry
            }),
            StixError::InvalidVocabValue { value, vocabulary } => Some(ErrorEntry {
                kind: "invalid_vocab_value".to_string(),
                message: self.to_string(),
                value: Some(value.clone()),
                vocabulary: Some(vocabulary.clone()),
                ..Default::default()
            }),
            StixError::InvalidHash {
                hash_type,
                hash_identity,
                hash_string,
            } => Some(ErrorEntry {
                kind: "invalid_hash".to_string(),
                message: self.to_string(),
                value: Some(hash_string.clone()),
                expected: Some(hash_type.clone()),
                got: Some(hash_identity.clone()),
                ..Default::default()
            }),
            StixError::ParseTimestampError(timestamp) => Some(ErrorEntry {
                kind: "invalid_timestamp".to_string(),
                message: self.to_string(),
                value: Some(timestamp.clone()),
                ..Default::default()
            }),
            StixError::ParseIdentifierError(identifier) => Some(ErrorEntry {
                kind: "invalid_identifier".to_string(),
                message: self.to_string(),
                value: Some(identifier.clone()),
                ..Default::default()
            }),
            StixError::ParsePatternError(pattern, detail) => Some(ErrorEntry {
                kind: "invalid_pattern".to_string(),
                message: self.to_string(),
                value: Some(pattern.clone()),
                got: Some(detail.clone()),
                ..Default::default()
            }),
            _ => None,
        }
    }

    /// Catch-all entry for variants without a rich structured form.
    ///
    /// Guarantees the total `.errors` contract: the `kind` is the variant name
    /// in snake_case and `message` is the display string.
    fn catch_all_entry(&self) -> ErrorEntry {
        ErrorEntry {
            kind: StixErrorDiscriminants::from(self).as_ref().to_string(),
            message: self.to_string(),
            ..Default::default()
        }
    }

    /// Human-readable summary for the envelope top-level `message` field.
    pub fn envelope_message(&self) -> String {
        match self {
            StixError::ValidationErrors(errors) => match errors.len() {
                0 => "Validation error".to_string(),
                1 => errors[0].to_string(),
                n => format!("{n} validation errors occurred"),
            },
            _ => self.to_string(),
        }
    }

    /// Human-readable message suitable for Python exception display.
    ///
    /// Unlike [`Self::envelope_message`], multiple validation errors are joined
    /// into a single readable sentence so callers see every issue in `str(e)`.
    pub fn display_message(&self) -> String {
        match self {
            StixError::ValidationErrors(errors) => match errors.len() {
                0 => "Validation error".to_string(),
                1 => errors[0].to_string(),
                _ => errors
                    .iter()
                    .map(|e| e.to_string())
                    .collect::<Vec<_>>()
                    .join("; "),
            },
            _ => self.to_string(),
        }
    }

    /// Returns the structured error entries for this error.
    ///
    /// For `ValidationErrors`, this flattens the entries of each inner error.
    /// Every other error yields at least one entry: a rich entry when the
    /// variant has natural structure, otherwise a catch-all entry
    /// ([`StixError::catch_all_entry`]).
    pub fn errors(&self) -> Vec<ErrorEntry> {
        match self {
            StixError::ValidationErrors(errors) if !errors.is_empty() => {
                errors.iter().flat_map(|e| e.errors()).collect()
            }
            _ => {
                let mut entries: Vec<ErrorEntry> = self.to_entry().into_iter().collect();
                if entries.is_empty() {
                    entries.push(self.catch_all_entry());
                }
                entries
            }
        }
    }

    /// Serializes this error as the cross-language JSON envelope.
    pub fn to_json(&self) -> serde_json::Value {
        serde_json::to_value(ErrorEnvelope {
            error: self.envelope_category().to_string(),
            message: self.envelope_message(),
            errors: self.errors(),
        })
        .expect("ErrorEnvelope serializes to JSON")
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde::Deserialize;
    use serde_path_to_error as serde_path;

    #[derive(Debug, Deserialize)]
    struct Probe {
        #[serde(rename = "name")]
        _name: String,
    }

    fn classify(json: &str) -> StixError {
        let result: Result<Probe, _> =
            serde_path::deserialize(&mut serde_json::Deserializer::from_str(json));
        classify_serde_error(result.unwrap_err(), "attack-pattern")
    }

    #[test]
    fn missing_field_becomes_missing_property() {
        let error = classify("{}");
        match error {
            StixError::MissingProperty {
                object_type,
                property,
            } => {
                assert_eq!(object_type, "attack-pattern");
                assert_eq!(property, "name");
            }
            other => panic!("expected MissingProperty, got {other:?}"),
        }
    }

    #[test]
    fn invalid_type_becomes_invalid_property_type() {
        let error = classify(r#"{"name": 1}"#);
        match error {
            StixError::InvalidPropertyType {
                object_type,
                property,
                expected,
                got,
            } => {
                assert_eq!(object_type, "attack-pattern");
                assert_eq!(property, "name");
                assert_eq!(expected, "a string");
                assert_eq!(got, "integer `1`");
            }
            other => panic!("expected InvalidPropertyType, got {other:?}"),
        }
    }

    #[test]
    fn syntax_error_keeps_position_message() {
        let error = classify("not json");
        match error {
            StixError::DeserializationError(message) => {
                assert!(message.contains("expected ident"), "message: {message}");
                assert!(message.contains("line 1"), "position kept: {message}");
            }
            other => panic!("expected DeserializationError, got {other:?}"),
        }
    }

    #[test]
    fn display_renders_structured_variants() {
        let error = classify("{}");
        assert_eq!(
            error.to_string(),
            "`attack-pattern` is missing required property 'name'"
        );
    }

    #[test]
    fn return_multiple_errors_joins_into_validation_errors() {
        assert!(return_multiple_errors(vec![]).is_ok());
        let single = return_multiple_errors(vec![StixError::EmptyList]).unwrap_err();
        assert!(matches!(single, StixError::EmptyList));
        let multiple =
            return_multiple_errors(vec![StixError::EmptyList, StixError::EmptyList]).unwrap_err();
        assert!(matches!(multiple, StixError::ValidationErrors(_)));
    }

    #[test]
    fn missing_property_envelope() {
        let error = classify("{}");
        let envelope = error.to_json();
        assert_eq!(envelope["error"], "validation");
        assert_eq!(
            envelope["message"],
            "`attack-pattern` is missing required property 'name'"
        );
        let entries = envelope["errors"].as_array().unwrap();
        assert_eq!(entries.len(), 1);
        assert_eq!(entries[0]["kind"], "missing_property");
        assert_eq!(entries[0]["property"], "name");
        assert!(entries[0]["expected"].is_null());
    }

    #[test]
    fn invalid_property_type_envelope() {
        let error = classify(r#"{"name": 1}"#);
        let envelope = error.to_json();
        assert_eq!(envelope["error"], "validation");
        let entries = envelope["errors"].as_array().unwrap();
        assert_eq!(entries.len(), 1);
        assert_eq!(entries[0]["kind"], "invalid_property_type");
        assert_eq!(entries[0]["property"], "name");
        assert_eq!(entries[0]["expected"], "a string");
        assert_eq!(entries[0]["got"], "integer `1`");
    }

    #[test]
    fn syntax_deserialization_envelope_keeps_position() {
        let error = classify("not json");
        let envelope = error.to_json();
        assert_eq!(envelope["error"], "deserialization");
        assert!(
            envelope["message"].as_str().unwrap().contains("line 1"),
            "position kept: {}",
            envelope["message"]
        );
        // Total contract: even syntax failures carry a catch-all entry.
        let entries = envelope["errors"].as_array().unwrap();
        assert_eq!(entries.len(), 1);
        assert_eq!(entries[0]["kind"], "deserialization_error");
        assert!(entries[0]["message"].as_str().unwrap().contains("line 1"));
    }

    #[test]
    fn validation_errors_flatten_into_envelope() {
        let error = StixError::ValidationErrors(vec![
            StixError::MissingProperty {
                object_type: "attack-pattern".to_string(),
                property: "name".to_string(),
            },
            StixError::InvalidPropertyType {
                object_type: "attack-pattern".to_string(),
                property: "name".to_string(),
                expected: "a string".to_string(),
                got: "integer `1`".to_string(),
            },
        ]);
        let envelope = error.to_json();
        assert_eq!(envelope["error"], "validation");
        assert_eq!(envelope["message"], "2 validation errors occurred");
        let entries = envelope["errors"].as_array().unwrap();
        assert_eq!(entries.len(), 2);
        assert_eq!(entries[0]["kind"], "missing_property");
        assert_eq!(entries[1]["kind"], "invalid_property_type");
    }

    #[test]
    fn unknown_properties_envelope() {
        let error = StixError::UnknownProperties {
            object_type: "attack-pattern".to_string(),
            fields: vec!["foo".to_string(), "bar".to_string()],
        };
        let envelope = error.to_json();
        assert_eq!(envelope["error"], "validation");
        let entries = envelope["errors"].as_array().unwrap();
        assert_eq!(entries.len(), 1);
        assert_eq!(entries[0]["kind"], "unknown_property");
        let props: Vec<String> = serde_json::from_value(entries[0]["properties"].clone()).unwrap();
        assert_eq!(props, vec!["foo", "bar"]);
    }

    #[test]
    fn generic_stix_error_envelope() {
        let error = StixError::EmptyList;
        let envelope = error.to_json();
        assert_eq!(envelope["error"], "stix");
        assert_eq!(
            envelope["message"],
            "Empty Lists and dictionaries are prohibted in STIX"
        );
        // Total contract: every variant yields at least one entry.
        let entries = envelope["errors"].as_array().unwrap();
        assert_eq!(entries.len(), 1);
        assert_eq!(entries[0]["kind"], "empty_list");
        assert_eq!(entries[0]["message"], envelope["message"]);
    }

    /// Constructs every `StixError` variant so the total `.errors` contract can
    /// be asserted exhaustively. A new variant must be added here, keeping the
    /// contract honest.
    fn all_variants() -> Vec<StixError> {
        use crate::types::Identifier;
        vec![
            StixError::ValidationErrors(vec![StixError::EmptyList]),
            StixError::ValidationErrors(vec![]),
            StixError::DateTimeError("bad".parse::<jiff::Timestamp>().unwrap_err()),
            StixError::EmptyList,
            StixError::InvalidHash {
                hash_type: "MD5".to_string(),
                hash_identity: "unknown".to_string(),
                hash_string: "d41d8cd98f00b204e9800998ecf8427e".to_string(),
            },
            StixError::InvalidUuid {
                message: "bad".to_string(),
            },
            StixError::IllegalBuilderProperty {
                object: "AttackPattern".to_string(),
                object_type: "attack-pattern".to_string(),
                field: "bogus".to_string(),
            },
            StixError::InvalidRelationship {
                relationship: "indicates".to_string(),
                source_type: "identity".to_string(),
                target_type: "identity".to_string(),
            },
            StixError::MissingBuilderProperty {
                object_type: "attack-pattern".to_string(),
                property: "name".to_string(),
            },
            StixError::ParseHexError("zz".to_string()),
            StixError::ParseIdentifierError("nope".to_string()),
            StixError::ParseIntegerError("x".parse::<u32>().unwrap_err()),
            StixError::ParseTimestampError("not-a-time".to_string()),
            StixError::ParsePatternError("AND OR".to_string(), "syntax error".to_string()),
            StixError::ValidationError("bad value".to_string()),
            StixError::InvalidNestedObject {
                context: "Domain Object x is not a valid y".to_string(),
                source: Box::new(StixError::EmptyList),
            },
            StixError::InvalidVocabValue {
                value: "unknown".to_string(),
                vocabulary: "test-vocab".to_string(),
            },
            StixError::UnknownExtension,
            StixError::WrongExtension,
            StixError::UnableToVersion("no timestamps".to_string()),
            StixError::BadVersion(
                "attack-pattern--12345678-1234-5678-1234-567812345678".to_string(),
                "bad".parse::<jiff::Timestamp>().unwrap_err(),
            ),
            StixError::CustomMissingExtension,
            StixError::JsonNull,
            StixError::UnexpectedJsonFormat,
            StixError::UnknownFields(vec!["bogus".to_string()]),
            StixError::UnrecognizedObject(strum::ParseError::VariantNotFound),
            StixError::SerializationError("cannot serialize".to_string()),
            StixError::DeserializationError("cannot deserialize".to_string()),
            StixError::MissingProperty {
                object_type: "attack-pattern".to_string(),
                property: "name".to_string(),
            },
            StixError::UnknownProperties {
                object_type: "attack-pattern".to_string(),
                fields: vec!["bogus".to_string()],
            },
            StixError::InvalidPropertyType {
                object_type: "attack-pattern".to_string(),
                property: "name".to_string(),
                expected: "a string".to_string(),
                got: "integer `1`".to_string(),
            },
            StixError::MissingDataSource,
            StixError::MissingDataSink,
            StixError::UnsupportedDataType("redis".to_string()),
            StixError::IoError("permission denied".to_string()),
            StixError::PathNotFound("/tmp/nope".to_string()),
            StixError::ObjectNotFound(Identifier::new_test("file")),
        ]
    }

    #[test]
    fn every_variant_yields_at_least_one_entry() {
        for error in all_variants() {
            let entries = error.errors();
            assert!(
                !entries.is_empty(),
                "variant {:?} produced no entries",
                error
            );
            for entry in &entries {
                assert!(!entry.kind.is_empty(), "empty kind for {error:?}");
                assert!(!entry.message.is_empty(), "empty message for {error:?}");
            }
        }
    }

    #[test]
    fn catch_all_kinds_are_snake_case_variant_names() {
        assert_eq!(StixError::EmptyList.errors()[0].kind, "empty_list");
        assert_eq!(
            StixError::ParseHexError("zz".to_string()).errors()[0].kind,
            "parse_hex_error"
        );
        assert_eq!(StixError::JsonNull.errors()[0].kind, "json_null");
        // Rich variants keep their documented kinds.
        assert_eq!(
            StixError::InvalidVocabValue {
                value: "x".to_string(),
                vocabulary: "v".to_string(),
            }
            .errors()[0]
                .kind,
            "invalid_vocab_value"
        );
    }

    #[test]
    fn rich_entries_carry_natural_structure() {
        // InvalidHash: value/expected/got mirror declared vs detected type.
        let hash = StixError::InvalidHash {
            hash_type: "MD5".to_string(),
            hash_identity: "unknown".to_string(),
            hash_string: "d41d8cd98f00b204e9800998ecf8427e".to_string(),
        };
        let entry = &hash.errors()[0];
        assert_eq!(entry.kind, "invalid_hash");
        assert_eq!(
            entry.value.as_deref(),
            Some("d41d8cd98f00b204e9800998ecf8427e")
        );
        assert_eq!(entry.expected.as_deref(), Some("MD5"));
        assert_eq!(entry.got.as_deref(), Some("unknown"));
        assert!(
            !hash.to_string().contains("and it is:"),
            "old wording removed: {}",
            hash
        );
        assert!(hash.to_string().contains("is not a valid MD5 hash"));

        let timestamp = StixError::ParseTimestampError("not-a-time".to_string());
        let entry = &timestamp.errors()[0];
        assert_eq!(entry.kind, "invalid_timestamp");
        assert_eq!(entry.value.as_deref(), Some("not-a-time"));

        let identifier = StixError::ParseIdentifierError("nope".to_string());
        let entry = &identifier.errors()[0];
        assert_eq!(entry.kind, "invalid_identifier");
        assert_eq!(entry.value.as_deref(), Some("nope"));

        let pattern = StixError::ParsePatternError("AND OR".to_string(), "syntax".to_string());
        let entry = &pattern.errors()[0];
        assert_eq!(entry.kind, "invalid_pattern");
        assert_eq!(entry.value.as_deref(), Some("AND OR"));
        assert_eq!(entry.got.as_deref(), Some("syntax"));
    }

    #[test]
    fn envelope_has_no_version_field() {
        let error = classify("{}");
        let envelope = error.to_json();
        assert!(envelope.get("v").is_none());
        assert!(envelope.get("version").is_none());
    }
}