Skip to main content

ftracker_identifiers/cnpj/
schema.rs

1//! [`JsonSchema`] implementation for [`Cnpj`].
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::Cnpj;
16
17impl JsonSchema for Cnpj {
18    fn schema_name() -> Cow<'static, str> {
19        Cow::Borrowed("Cnpj")
20    }
21
22    fn json_schema(_: &mut SchemaGenerator) -> Schema {
23        json_schema!({
24            "type": "string",
25            "format": "cnpj",
26            "minLength": 14,
27            "maxLength": 14,
28            "pattern": "^[A-Z0-9]{12}[0-9]{2}$",
29            "description": "Brazilian CNPJ (Cadastro Nacional da Pessoa Jurídica), unformatted, Módulo 11 checksum-valid."
30        })
31    }
32}
33
34#[cfg(test)]
35mod tests {
36    use super::super::Cnpj;
37    use schemars::schema_for;
38
39    #[test]
40    fn schema_is_a_pattern_constrained_string() {
41        let schema = schema_for!(Cnpj);
42        let json = serde_json::to_value(&schema).unwrap();
43
44        assert_eq!(json["type"], "string");
45        assert_eq!(json["format"], "cnpj");
46        assert_eq!(json["minLength"], 14);
47        assert_eq!(json["maxLength"], 14);
48        assert_eq!(json["pattern"], "^[A-Z0-9]{12}[0-9]{2}$");
49    }
50}