Skip to main content

zaun_community_protocol/command/invite/
validate.rs

1//! State-free checks of the invitation commands.
2
3use super::{Create, List, Redeem, Revoke, Uses};
4use crate::command::{BodyError, Validate, check};
5use crate::limits::{INVITE_EXPIRY_MAX_MS, INVITE_MAX_USES, PAGE_SMALL, ROLES_PER_MEMBER};
6
7impl Validate for Create {
8    /// SPEC: `expires_in_s: 0` would create an already-expired invitation; 1 s is the
9    /// minimum. `pre_approved` with `requires_approval` is a bad request (the spec says they
10    /// conflict without naming the code).
11    fn validate(&self) -> Result<(), BodyError> {
12        check::range("max_uses", self.max_uses, 0, INVITE_MAX_USES)?;
13        if let Some(secs) = self.expires_in_s {
14            check::range("expires_in_s", secs, 1, INVITE_EXPIRY_MAX_MS / 1000)?;
15        }
16        if let Some(roles) = &self.default_role_ids {
17            check::count("default_role_ids", roles.len(), ROLES_PER_MEMBER)?;
18            check::not_everyone("default_role_ids", roles)?;
19            check::unique("default_role_ids", roles)?;
20        }
21        if self.pre_approved == Some(true) && self.requires_approval == Some(true) {
22            return Err(BodyError::bad(
23                "pre_approved",
24                "conflicts with requires_approval",
25            ));
26        }
27        self.admission_overrides.map_or(Ok(()), |o| o.check())
28    }
29}
30
31impl Validate for Redeem {
32    fn validate(&self) -> Result<(), BodyError> {
33        self.profile.check()
34    }
35}
36
37impl Validate for Revoke {
38    fn validate(&self) -> Result<(), BodyError> {
39        Ok(())
40    }
41}
42
43impl Validate for List {
44    fn validate(&self) -> Result<(), BodyError> {
45        check::page(self.limit, PAGE_SMALL)
46    }
47}
48
49impl Validate for Uses {
50    fn validate(&self) -> Result<(), BodyError> {
51        check::page(self.limit, PAGE_SMALL)
52    }
53}