jellyschema/validator/types/
hostname.rs1use lazy_static::lazy_static;
2use regex::Regex;
3use serde_json::Value;
4
5use crate::validator::{scope::ScopedSchema, state::ValidationState, types::validate_as_string_with_regex};
6
7lazy_static! {
8 static ref HOSTNAME_REGEX: Regex =
11 Regex::new(r"^(?i)[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?(?:\.[a-z0-9](?:[-0-9a-z]{0,61}[0-9a-z])?)*$").unwrap();
12}
13
14pub fn validate_as_hostname(scope: &ScopedSchema, data: &Value) -> ValidationState {
15 let mut state = validate_as_string_with_regex(scope, data, &HOSTNAME_REGEX);
16
17 if state.is_valid() {
18 let len = data
19 .as_str()
20 .expect("invalid validate_as_string_with_regex")
21 .chars()
22 .count();
23
24 if len > 255 {
25 state.push_error(scope.error("type", "'hostname' must not be longer than 255 characters"));
26 }
27 }
28
29 state
30}