Skip to main content

zaun_community_protocol/command/membership/
validate.rs

1//! State-free checks of the membership and admission commands.
2
3use super::{Approve, Pending, PolicyGet, PolicyUpdate, Reject, Request, Restore, Suspend};
4use crate::command::object::admission_mask;
5use crate::command::{BodyError, Validate, check};
6use crate::limits::{PAGE_SMALL, PENDING_EXPIRY_MAX_MS, REASON_MAX, REQUEST_NOTE_MAX};
7
8impl Validate for Request {
9    fn validate(&self) -> Result<(), BodyError> {
10        self.profile.check()?;
11        check::opt_text("note", self.note.as_deref(), (0, REQUEST_NOTE_MAX))
12    }
13}
14
15impl Validate for Approve {
16    fn validate(&self) -> Result<(), BodyError> {
17        Ok(())
18    }
19}
20
21impl Validate for Reject {
22    fn validate(&self) -> Result<(), BodyError> {
23        check::opt_text("reason", self.reason.as_deref(), (0, REASON_MAX))
24    }
25}
26
27impl Validate for Suspend {
28    fn validate(&self) -> Result<(), BodyError> {
29        check::opt_text("reason", self.reason.as_deref(), (0, REASON_MAX))?;
30        self.until.map_or(Ok(()), |u| check::json_safe("until", u))
31    }
32}
33
34impl Validate for Restore {
35    fn validate(&self) -> Result<(), BodyError> {
36        Ok(())
37    }
38}
39
40impl Validate for Pending {
41    /// SPEC: §8.9 gives `limit ≤ 100`; §12's page-size row omits `membership.pending`.
42    fn validate(&self) -> Result<(), BodyError> {
43        check::page(self.limit, PAGE_SMALL)
44    }
45}
46
47impl Validate for PolicyGet {
48    fn validate(&self) -> Result<(), BodyError> {
49        Ok(())
50    }
51}
52
53impl Validate for PolicyUpdate {
54    /// SPEC: "any policy field" is read as "at least one". A non-empty `verification` is
55    /// `UNSUPPORTED_FEATURE` (reserved, §8.11). Durations and counts have no stated bounds
56    /// except `pending_expiry_s` (≤ 30 days, §12); the others must be below 2^53.
57    fn validate(&self) -> Result<(), BodyError> {
58        let p = self;
59        let untouched = p.level.is_keep()
60            && p.requires_approval.is_keep()
61            && p.waiting_period_s.is_keep()
62            && p.probation_period_s.is_keep()
63            && p.probation_activity_min.is_keep()
64            && p.newcomer_restrictions.is_keep()
65            && p.join_rate.is_keep()
66            && p.pending_expiry_s.is_keep()
67            && p.invite_creation.is_keep()
68            && p.verification.is_keep();
69        if untouched {
70            return Err(BodyError::bad("body", "no field to update"));
71        }
72        check::not_null("level", &p.level)?;
73        check::not_null("requires_approval", &p.requires_approval)?;
74        check::not_null("waiting_period_s", &p.waiting_period_s)?;
75        check::not_null("probation_period_s", &p.probation_period_s)?;
76        check::not_null("probation_activity_min", &p.probation_activity_min)?;
77        check::not_null("newcomer_restrictions", &p.newcomer_restrictions)?;
78        check::not_null("join_rate", &p.join_rate)?;
79        check::not_null("pending_expiry_s", &p.pending_expiry_s)?;
80        check::not_null("invite_creation", &p.invite_creation)?;
81        check::not_null("verification", &p.verification)?;
82        if let Some(s) = check::set(&p.waiting_period_s) {
83            check::json_safe("waiting_period_s", *s)?;
84        }
85        if let Some(s) = check::set(&p.probation_period_s) {
86            check::json_safe("probation_period_s", *s)?;
87        }
88        if let Some(s) = check::set(&p.pending_expiry_s) {
89            check::range("pending_expiry_s", *s, 1, PENDING_EXPIRY_MAX_MS / 1000)?;
90        }
91        if let Some(mask) = check::set(&p.newcomer_restrictions) {
92            admission_mask("newcomer_restrictions", *mask)?;
93        }
94        match check::set(&p.verification) {
95            Some(v) if !v.is_empty() => Err(BodyError::unsupported("verification")),
96            _ => Ok(()),
97        }
98    }
99}