mas_handlers/admin/
schema.rs

1// Copyright 2024 New Vector Ltd.
2// Copyright 2024 The Matrix.org Foundation C.I.C.
3//
4// SPDX-License-Identifier: AGPL-3.0-only
5// Please see LICENSE in the repository root for full details.
6
7//! Common schema definitions
8
9use schemars::{
10    JsonSchema,
11    r#gen::SchemaGenerator,
12    schema::{InstanceType, Metadata, Schema, SchemaObject, StringValidation},
13};
14
15/// A type to use for schema definitions of ULIDs
16///
17/// Use with `#[schemars(with = "crate::admin::schema::Ulid")]`
18pub struct Ulid;
19
20impl JsonSchema for Ulid {
21    fn schema_name() -> String {
22        "ULID".to_owned()
23    }
24
25    fn json_schema(_gen: &mut SchemaGenerator) -> Schema {
26        SchemaObject {
27            instance_type: Some(InstanceType::String.into()),
28
29            metadata: Some(Box::new(Metadata {
30                title: Some("ULID".into()),
31                description: Some("A ULID as per https://github.com/ulid/spec".into()),
32                examples: vec![
33                    "01ARZ3NDEKTSV4RRFFQ69G5FAV".into(),
34                    "01J41912SC8VGAQDD50F6APK91".into(),
35                ],
36                ..Metadata::default()
37            })),
38
39            string: Some(Box::new(StringValidation {
40                pattern: Some(r"^[0123456789ABCDEFGHJKMNPQRSTVWXYZ]{26}$".into()),
41                ..StringValidation::default()
42            })),
43
44            ..SchemaObject::default()
45        }
46        .into()
47    }
48}
49
50/// A type to use for schema definitions of device IDs
51///
52/// Use with `#[schemars(with = "crate::admin::schema::Device")]`
53pub struct Device;
54
55impl JsonSchema for Device {
56    fn schema_name() -> String {
57        "DeviceID".to_owned()
58    }
59
60    fn json_schema(_gen: &mut SchemaGenerator) -> Schema {
61        SchemaObject {
62            instance_type: Some(InstanceType::String.into()),
63
64            metadata: Some(Box::new(Metadata {
65                title: Some("Device ID".into()),
66                examples: vec!["AABBCCDDEE".into(), "FFGGHHIIJJ".into()],
67                ..Metadata::default()
68            })),
69
70            string: Some(Box::new(StringValidation {
71                pattern: Some(r"^[A-Za-z0-9._~!$&'()*+,;=:&/-]+$".into()),
72                ..StringValidation::default()
73            })),
74
75            ..SchemaObject::default()
76        }
77        .into()
78    }
79}