Skip to main content

zaun_community_protocol/command/interaction/
validate.rs

1//! State-free checks of the interaction commands (§8.14).
2
3use super::{
4    Create, CreateData, Followup, OriginalDelete, OriginalEdit, Respond, ResponseData, ResponseType,
5};
6use crate::command::object::{InteractionType, OptionValue, OptionValueKind, check_modal, flags};
7use crate::command::{BodyError, Validate, check};
8use crate::limits::{
9    APP_CHOICE_TEXT, APP_COMMAND_OPTIONS, AUTOCOMPLETE_CHOICES, COMPONENT_CUSTOM_ID, MODAL_ITEMS,
10    SELECT_OPTIONS, TEXT_INPUT_MAX,
11};
12
13fn custom_id(value: &str) -> Result<(), BodyError> {
14    let (min, max) = COMPONENT_CUSTOM_ID;
15    if value.len() > max {
16        return Err(BodyError::too_large("data.custom_id", max));
17    }
18    if value.len() < min {
19        return Err(BodyError::bad("data.custom_id", "is empty"));
20    }
21    Ok(())
22}
23
24/// Option values: ≤ 25 per level, strings ≤ 6 000, at most one focused overall.
25fn options(list: &[OptionValue], focused: &mut usize) -> Result<(), BodyError> {
26    check::count("data.options", list.len(), APP_COMMAND_OPTIONS)?;
27    for o in list {
28        *focused += usize::from(o.focused);
29        if let Some(OptionValueKind::String(s)) = &o.value {
30            check::text("data.options.value", s, (0, 6_000))?;
31        }
32        options(&o.options, focused)?;
33    }
34    Ok(())
35}
36
37impl Validate for Create {
38    fn validate(&self) -> Result<(), BodyError> {
39        match (&self.data, self.kind) {
40            (CreateData::Command { options: list, .. }, kind) => {
41                let mut focused = 0;
42                options(list, &mut focused)?;
43                let want = usize::from(kind == InteractionType::Autocomplete);
44                if focused != want {
45                    return Err(BodyError::bad(
46                        "data.options",
47                        "exactly one focused option for autocomplete, none otherwise",
48                    ));
49                }
50                Ok(())
51            }
52            (
53                CreateData::Component {
54                    custom_id: id,
55                    values,
56                    ..
57                },
58                _,
59            ) => {
60                custom_id(id)?;
61                check::count("data.values", values.len(), SELECT_OPTIONS)?;
62                check::unique("data.values", values)
63            }
64            (
65                CreateData::Modal {
66                    custom_id: id,
67                    components,
68                    ..
69                },
70                _,
71            ) => {
72                custom_id(id)?;
73                check::count("data.components", components.len(), MODAL_ITEMS.1)?;
74                check::unique(
75                    "data.components.custom_id",
76                    components.iter().map(|c| &c.custom_id),
77                )?;
78                for c in components {
79                    custom_id(&c.custom_id)?;
80                    check::opt_text(
81                        "data.components.value",
82                        c.value.as_deref(),
83                        (0, TEXT_INPUT_MAX as usize),
84                    )?;
85                    check::count("data.components.values", c.values.len(), SELECT_OPTIONS)?;
86                }
87                Ok(())
88            }
89        }
90    }
91}
92
93impl Validate for Respond {
94    fn validate(&self) -> Result<(), BodyError> {
95        match &self.data {
96            ResponseData::Message(body) => body.check(self.kind == ResponseType::ChannelMessage),
97            ResponseData::Deferred { flags: f } => match f.unwrap_or(0) & !flags::EPHEMERAL {
98                0 => Ok(()),
99                _ => Err(BodyError::bad(
100                    "data.flags",
101                    "only EPHEMERAL on a deferred response",
102                )),
103            },
104            ResponseData::None => Ok(()),
105            ResponseData::Autocomplete { choices } => {
106                check::count("data.choices", choices.len(), AUTOCOMPLETE_CHOICES)?;
107                choices
108                    .iter()
109                    .try_for_each(|c| check::text("data.choices.name", &c.name, APP_CHOICE_TEXT))
110            }
111            ResponseData::Modal {
112                custom_id: id,
113                title,
114                components,
115            } => check_modal(id, title, components),
116        }
117    }
118}
119
120impl Validate for Followup {
121    fn validate(&self) -> Result<(), BodyError> {
122        self.body.check(true)
123    }
124}
125
126impl Validate for OriginalEdit {
127    fn validate(&self) -> Result<(), BodyError> {
128        self.body.check(false)
129    }
130}
131
132impl Validate for OriginalDelete {
133    fn validate(&self) -> Result<(), BodyError> {
134        Ok(())
135    }
136}