1pub mod param_utils {
2 use anyhow::Ok;
3
4 use super::property_util;
5
6 const VALID_CHARS: [char; 4] = ['_', '-', '.', ':'];
7 const TENANT_MAX_LEN: usize = 128;
8
9 pub fn check_tenant(tenant: &Option<String>) -> anyhow::Result<()> {
10 if let Some(t) = tenant {
11 if !t.is_empty() {
12 if !is_valid(t.trim()) {
13 return Err(anyhow::anyhow!("invalid tenant"));
14 }
15 if t.len() > TENANT_MAX_LEN {
16 return Err(anyhow::anyhow!("Too long tenant, over 128"));
17 }
18 }
19 }
20 Ok(())
21 }
22 pub fn check_param(
23 data_id: &Option<String>,
24 group: &Option<String>,
25 datum_id: &Option<String>,
26 content: &Option<String>,
27 ) -> anyhow::Result<()> {
28 match data_id {
29 Some(data_id) => {
30 if data_id.is_empty() || !is_valid(data_id.trim()) {
31 return Err(anyhow::anyhow!("invalid dataId : {}", data_id));
32 }
33 }
34 None => return Err(anyhow::anyhow!("invalid dataId : ")),
35 }
36 match group {
37 Some(group) => {
38 if group.is_empty() || !is_valid(group) {
39 return Err(anyhow::anyhow!("invalid group : {}", group));
40 }
41 }
42 None => return Err(anyhow::anyhow!("invalid group : ")),
43 }
44 match datum_id {
45 Some(datum_id) => {
46 if datum_id.is_empty() || !is_valid(datum_id) {
47 return Err(anyhow::anyhow!("invalid datumId : {}", datum_id));
48 }
49 }
50 None => return Err(anyhow::anyhow!("invalid datumId : ")),
51 }
52 match content {
53 Some(content) => {
54 if content.is_empty() {
55 return Err(anyhow::anyhow!("content is blank : {}", content));
56 } else if content.len() > property_util::get_max_content() {
57 return Err(anyhow::anyhow!(
58 "invalid content, over {}",
59 property_util::get_max_content()
60 ));
61 }
62 }
63 None => return Err(anyhow::anyhow!("content is blank : ")),
64 }
65
66 Ok(())
67 }
68
69 fn is_valid_char(ch: char) -> bool {
70 VALID_CHARS.contains(&ch)
71 }
72
73 pub fn is_valid(param: &str) -> bool {
74 if param.is_empty() {
75 return false;
76 }
77 for ch in param.chars() {
78 if !ch.is_alphanumeric() && !is_valid_char(ch) {
79 return false;
80 }
81 }
82 true
83 }
84}
85pub mod property_util {
86 use crate::common::AppSysConfig;
87 pub struct ConfigProperty {
88 max_content: usize,
89 }
90
91 impl Default for ConfigProperty {
92 fn default() -> Self {
93 ConfigProperty::new()
94 }
95 }
96 impl ConfigProperty {
97 pub fn new() -> Self {
98 let sys_config = AppSysConfig::init_from_env();
99 let max_content = sys_config.config_max_content;
100 Self { max_content }
101 }
102 }
103 pub fn get_max_content() -> usize {
104 ConfigProperty::new().max_content
105 }
106}