liveactivity/
validation.rs1use crate::{Error, Result, layout, models::*};
2use serde::de::DeserializeOwned;
3use serde_json::Value;
4
5pub(crate) const MAX_REVISION: u64 = 9_007_199_254_740_991;
6pub(crate) const MAX_TIMESTAMP: u64 = 8_640_000_000_000_000;
7
8pub trait Validate {
10 fn validate(&self) -> Result<()>;
12}
13
14pub(crate) fn parse<T: DeserializeOwned + Validate>(value: Value) -> Result<T> {
15 let input: T = serde_json::from_value(value).map_err(|_| Error::invalid())?;
16 input.validate()?;
17 Ok(input)
18}
19
20pub fn parse_request<T: DeserializeOwned + Validate>(command: &str, input: Value) -> Result<T> {
24 match command {
25 "start_layout" | "update_layout" | "end_layout" => layout::parse(command, input),
26 _ => parse(input),
27 }
28}
29
30pub fn validate_request(command: &str, input: Value) -> Result<()> {
33 match command {
34 "start" => parse::<StartInput>(input).map(|_| ()),
35 "update" => parse::<UpdateInput>(input).map(|_| ()),
36 "end" => parse::<EndInput>(input).map(|_| ()),
37 "start_layout" => layout::parse::<layout::LayoutStartInput>(command, input).map(|_| ()),
38 "update_layout" => layout::parse::<layout::LayoutUpdateInput>(command, input).map(|_| ()),
39 "end_layout" => layout::parse::<layout::LayoutEndInput>(command, input).map(|_| ()),
40 _ => Err(Error::invalid()),
41 }
42}
43
44pub(crate) fn text(value: &str, max: usize) -> bool {
45 let blank = |c: char| matches!(c, '\u{0009}'..='\u{000d}' | ' ' | '\u{00a0}' | '\u{1680}' | '\u{2000}'..='\u{200a}' | '\u{2028}' | '\u{2029}' | '\u{202f}' | '\u{205f}' | '\u{3000}' | '\u{feff}');
47 value.chars().count() <= max && value.chars().any(|c| !blank(c))
48}
49
50pub(crate) fn file_name(name: &str) -> bool {
52 let bytes = name.as_bytes();
53 bytes.len() <= 64
54 && bytes.first().is_some_and(u8::is_ascii_alphanumeric)
55 && bytes
56 .iter()
57 .all(|c| c.is_ascii_alphanumeric() || b"._-".contains(c))
58}
59
60pub(crate) fn require(valid: bool) -> Result<()> {
61 if valid { Ok(()) } else { Err(Error::invalid()) }
62}
63
64impl Validate for Snapshot {
65 fn validate(&self) -> Result<()> {
66 require(
67 self.revision <= MAX_REVISION && text(&self.title, 120) && text(&self.status, 240),
68 )?;
69 require(self.stale_at.is_none_or(|value| value <= MAX_TIMESTAMP))?;
70 match (&self.progress, &self.timer) {
71 (Some(value), None) => require(value.is_finite() && (0.0..=1.0).contains(value)),
72 (None, Some(timer)) => {
73 require(timer.started_at < timer.ends_at && timer.ends_at <= MAX_TIMESTAMP)
74 }
75 _ => Err(Error::invalid()),
76 }
77 }
78}
79
80impl Validate for StartInput {
81 fn validate(&self) -> Result<()> {
82 let key = self.activity_key.as_bytes();
83 require(
84 !key.is_empty()
85 && key.len() <= 128
86 && key[0].is_ascii_alphanumeric()
87 && key
88 .iter()
89 .all(|c| c.is_ascii_alphanumeric() || b"._:-".contains(c)),
90 )?;
91 require(self.schema_version == 1)?;
92 self.snapshot.validate()?;
93 require(matches!(
94 (&self.template, &self.snapshot.progress),
95 (Template::Progress, Some(_)) | (Template::Timer, None)
96 ))
97 }
98}
99
100impl Validate for UpdateInput {
101 fn validate(&self) -> Result<()> {
102 require(text(&self.id, 256))?;
103 self.snapshot.validate()
104 }
105}
106
107impl Validate for EndInput {
108 fn validate(&self) -> Result<()> {
109 require(text(&self.id, 256))?;
110 match self.policy {
111 EndPolicy::After { at } => require(at <= MAX_TIMESTAMP),
112 _ => Ok(()),
113 }
114 }
115}