use crate::error::StixError as Error;
use base64::{engine::general_purpose, Engine};
use serde_json::Value;
pub fn validate_custom_property_name(name: &str) -> Result<(), Error> {
if name.len() < 3 {
return Err(Error::ValidationError(format!(
"Custom property name '{}' is too short; minimum length is 3",
name
)));
}
if name.len() > 250 {
return Err(Error::ValidationError(format!(
"Custom property name '{}' is too long; maximum length is 250",
name
)));
}
if !name
.chars()
.all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == '_')
{
return Err(Error::ValidationError(format!(
"Custom property name '{}' contains characters outside the allowed set (a-z, 0-9, _)",
name
)));
}
const RESERVED: &[&str] = &["severity", "username", "phone_number", "action"];
if RESERVED.contains(&name) {
return Err(Error::ValidationError(format!(
"Custom property name '{}' is reserved by STIX 2.1 and cannot be used as a custom property",
name
)));
}
Ok(())
}
pub fn validate_custom_property_suffix_value(name: &str, value: &Value) -> Result<(), Error> {
if name.ends_with("_hex") {
let Some(s) = value.as_str() else {
return Err(Error::ValidationError(format!(
"Custom property '{}' uses the _hex suffix and therefore must have a string value",
name
)));
};
if s.len() % 2 != 0 {
return Err(Error::ValidationError(format!(
"Custom property '{}' has an _hex value with an odd number of characters",
name
)));
}
if !s.chars().all(|c| matches!(c, '0'..='9' | 'a'..='f')) {
return Err(Error::ValidationError(format!(
"Custom property '{}' has an _hex value containing characters other than 0-9 and a-f",
name
)));
}
} else if name.ends_with("_bin") {
let Some(s) = value.as_str() else {
return Err(Error::ValidationError(format!(
"Custom property '{}' uses the _bin suffix and therefore must have a string value",
name
)));
};
if general_purpose::STANDARD.decode(s).is_err() {
return Err(Error::ValidationError(format!(
"Custom property '{}' has an _bin value that is not valid base64",
name
)));
}
}
Ok(())
}