Debian Codegraph @

src/properties.rs

raw symbols SCIP
1 use crate::{
2 compiler, node::SchemaNode, paths::Location, thread::ThreadBound, validator::Validate as _,
3 };
4 use ahash::AHashMap;
5 use serde_json::{Map, Value};
6
7 use crate::ValidationError;
8
9 pub(crate) type FancyRegexValidators = Vec<(fancy_regex::Regex, SchemaNode)>;
10 pub(crate) type RegexValidators = Vec<(regex::Regex, SchemaNode)>;
11
12 /// A value that can look up property validators by name.
13 pub(crate) trait PropertiesValidatorsMap: ThreadBound {
14 fn get_validator(&self, property: &str) -> Option<&SchemaNode>;
15 fn get_key_validator(&self, property: &str) -> Option<(&String, &SchemaNode)>;
16 }
17
18 // We're defining two different property validator map implementations, one for small map sizes and
19 // one for large map sizes, to optimize the performance depending on the number of properties
20 // present.
21 //
22 // Implementors should use `compile_dynamic_prop_map_validator!` for building their validator maps
23 // at runtime, as it wraps up all of the logic to choose the right map size and then build and
24 // compile the validator.
25 pub(crate) type SmallValidatorsMap = Vec<(String, SchemaNode)>;
26 pub(crate) type BigValidatorsMap = AHashMap<String, SchemaNode>;
27
28 impl PropertiesValidatorsMap for SmallValidatorsMap {
29 #[inline]
30 fn get_validator(&self, property: &str) -> Option<&SchemaNode> {
31 for (prop, node) in self {
32 if prop == property {
33 return Some(node);
34 }
35 }
36 None
37 }
38 #[inline]
39 fn get_key_validator(&self, property: &str) -> Option<(&String, &SchemaNode)> {
40 for (prop, node) in self {
41 if prop == property {
42 return Some((prop, node));
43 }
44 }
45 None
46 }
47 }
48
49 impl PropertiesValidatorsMap for BigValidatorsMap {
50 #[inline]
51 fn get_validator(&self, property: &str) -> Option<&SchemaNode> {
52 self.get(property)
53 }
54
55 #[inline]
56 fn get_key_validator(&self, property: &str) -> Option<(&String, &SchemaNode)> {
57 self.get_key_value(property)
58 }
59 }
60
61 pub(crate) fn compile_small_map<'a>(
62 ctx: &compiler::Context,
63 map: &'a Map<String, Value>,
64 ) -> Result<SmallValidatorsMap, ValidationError<'a>> {
65 let mut properties = Vec::with_capacity(map.len());
66 let kctx = ctx.new_at_location("properties");
67 for (key, subschema) in map {
68 let pctx = kctx.new_at_location(key.as_str());
69 properties.push((
70 key.clone(),
71 compiler::compile(&pctx, pctx.as_resource_ref(subschema))?,
72 ));
73 }
74 Ok(properties)
75 }
76
77 pub(crate) fn compile_big_map<'a>(
78 ctx: &compiler::Context,
79 map: &'a Map<String, Value>,
80 ) -> Result<BigValidatorsMap, ValidationError<'a>> {
81 let mut properties = AHashMap::with_capacity(map.len());
82 let kctx = ctx.new_at_location("properties");
83 for (key, subschema) in map {
84 let pctx = kctx.new_at_location(key.as_str());
85 properties.insert(
86 key.clone(),
87 compiler::compile(&pctx, pctx.as_resource_ref(subschema))?,
88 );
89 }
90 Ok(properties)
91 }
92
93 pub(crate) fn are_properties_valid<M, F>(
94 prop_map: &M,
95 props: &Map<String, Value>,
96 ctx: &mut crate::validator::ValidationContext,
97 check: F,
98 ) -> bool
99 where
100 M: PropertiesValidatorsMap,
101 F: Fn(&Value, &mut crate::validator::ValidationContext) -> bool,
102 {
103 for (property, instance) in props {
104 if let Some(validator) = prop_map.get_validator(property) {
105 if !validator.is_valid(instance, ctx) {
106 return false;
107 }
108 } else if !check(instance, ctx) {
109 return false;
110 }
111 }
112 true
113 }
114
115 /// Create a vector of pattern-validators pairs.
116 #[inline]
117 pub(crate) fn compile_fancy_regex_patterns<'a>(
118 ctx: &compiler::Context,
119 obj: &'a Map<String, Value>,
120 ) -> Result<FancyRegexValidators, ValidationError<'a>> {
121 let kctx = ctx.new_at_location("patternProperties");
122 let mut compiled_patterns = Vec::with_capacity(obj.len());
123 for (pattern, subschema) in obj {
124 let pctx = kctx.new_at_location(pattern.as_str());
125 let compiled_pattern = ctx.get_or_compile_regex(pattern).map_err(|()| {
126 ValidationError::format(Location::new(), kctx.location().clone(), subschema, "regex")
127 })?;
128 let node = compiler::compile(&pctx, pctx.as_resource_ref(subschema))?;
129 compiled_patterns.push(((*compiled_pattern).clone(), node));
130 }
131 Ok(compiled_patterns)
132 }
133
134 #[inline]
135 pub(crate) fn compile_regex_patterns<'a>(
136 ctx: &compiler::Context,
137 obj: &'a Map<String, Value>,
138 ) -> Result<RegexValidators, ValidationError<'a>> {
139 let kctx = ctx.new_at_location("patternProperties");
140 let mut compiled_patterns = Vec::with_capacity(obj.len());
141 for (pattern, subschema) in obj {
142 let pctx = kctx.new_at_location(pattern.as_str());
143 let compiled_pattern = ctx.get_or_compile_standard_regex(pattern).map_err(|()| {
144 ValidationError::format(Location::new(), kctx.location().clone(), subschema, "regex")
145 })?;
146 let node = compiler::compile(&pctx, pctx.as_resource_ref(subschema))?;
147 compiled_patterns.push(((*compiled_pattern).clone(), node));
148 }
149 Ok(compiled_patterns)
150 }
151
152 macro_rules! compile_dynamic_prop_map_validator {
153 ($validator:tt, $properties:ident, $( $arg:expr ),* $(,)*) => {{
154 if let Value::Object(map) = $properties {
155 if map.len() < 40 {
156 Some($validator::<SmallValidatorsMap>::compile(
157 map, $($arg, )*
158 ))
159 } else {
160 Some($validator::<BigValidatorsMap>::compile(
161 map, $($arg, )*
162 ))
163 }
164 } else {
165 Some(Err(ValidationError::custom(
166 Location::new(),
167 Location::new(),
168 $properties,
169 "Unexpected type",
170 )))
171 }
172 }};
173 }
174
175 pub(crate) use compile_dynamic_prop_map_validator;