Skip to main content

ftracker_identifiers/isin/
schema.rs

1//! [`JsonSchema`] implementation for [`Isin`].
2//!
3//! Enabled by the `schemars` feature.
4
5#[cfg(not(feature = "std"))]
6extern crate alloc;
7
8#[cfg(not(feature = "std"))]
9use alloc::borrow::Cow;
10#[cfg(feature = "std")]
11use std::borrow::Cow;
12
13use schemars::{JsonSchema, Schema, SchemaGenerator, json_schema};
14
15use super::Isin;
16
17impl JsonSchema for Isin {
18    fn schema_name() -> Cow<'static, str> {
19        Cow::Borrowed("Isin")
20    }
21
22    fn json_schema(_: &mut SchemaGenerator) -> Schema {
23        json_schema!({
24            "type": "string",
25            "format": "isin",
26            "minLength": 12,
27            "maxLength": 12,
28            "pattern": "^[A-Z]{2}[A-Z0-9]{9}[0-9]$",
29            "description": "ISIN (International Securities Identification Number, ISO 6166), Luhn checksum-valid."
30        })
31    }
32}
33
34#[cfg(test)]
35mod tests {
36    use super::super::Isin;
37    use schemars::schema_for;
38
39    #[test]
40    fn schema_is_a_pattern_constrained_string() {
41        let schema = schema_for!(Isin);
42        let json = serde_json::to_value(&schema).unwrap();
43
44        assert_eq!(json["type"], "string");
45        assert_eq!(json["format"], "isin");
46        assert_eq!(json["minLength"], 12);
47        assert_eq!(json["maxLength"], 12);
48        assert_eq!(json["pattern"], "^[A-Z]{2}[A-Z0-9]{9}[0-9]$");
49    }
50}