Skip to main content

tango/models/
validate.rs

1//! Types for `POST /api/validate/`.
2
3use serde::{Deserialize, Serialize};
4use serde_json::Value;
5use std::collections::HashMap;
6
7/// Identifier type the validator should check.
8#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
9#[serde(rename_all = "lowercase")]
10pub enum ValidateInputType {
11    /// Contract PIID.
12    Piid,
13    /// Solicitation number.
14    Solicitation,
15    /// UEI (Unique Entity Identifier).
16    Uei,
17}
18
19/// Request body for [`Client::validate`](crate::Client::validate).
20#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
21pub struct ValidateInput {
22    /// Identifier type to check.
23    #[serde(rename = "type")]
24    pub kind: ValidateInputType,
25    /// Identifier value to validate.
26    pub value: String,
27}
28
29/// Response from [`Client::validate`](crate::Client::validate).
30#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq)]
31pub struct ValidateResult {
32    /// Outcome label: `"valid"`, `"invalid"`, or `"low_confidence"`.
33    pub result: String,
34    /// Echoed identifier type.
35    #[serde(default, rename = "type", skip_serializing_if = "Option::is_none")]
36    pub kind: Option<String>,
37    /// Echoed identifier value.
38    #[serde(default, skip_serializing_if = "Option::is_none")]
39    pub value: Option<String>,
40    /// Structured failure reasons when `result` is non-valid.
41    #[serde(default, skip_serializing_if = "Vec::is_empty")]
42    pub errors: Vec<Value>,
43    /// Forward-compatible bucket for any unrecognized fields the server adds.
44    #[serde(flatten)]
45    pub extra: HashMap<String, Value>,
46}
47
48#[cfg(test)]
49mod tests {
50    use super::*;
51    use serde_json::json;
52
53    #[test]
54    fn validate_result_captures_unknown_fields_via_extra() {
55        let body = json!({
56            "result": "valid",
57            "type": "uei",
58            "value": "ABC123DEF456",
59            "future_field": "x"
60        });
61        let r: ValidateResult = serde_json::from_value(body).expect("decode");
62        assert_eq!(r.result, "valid");
63        assert_eq!(r.kind.as_deref(), Some("uei"));
64        assert!(r.extra.contains_key("future_field"));
65    }
66
67    #[test]
68    fn validate_input_type_round_trips() {
69        let v = serde_json::to_value(ValidateInputType::Uei).unwrap();
70        assert_eq!(v, json!("uei"));
71        let back: ValidateInputType = serde_json::from_value(json!("piid")).unwrap();
72        assert_eq!(back, ValidateInputType::Piid);
73    }
74}