Debian Codegraph @

src/keywords/properties.rs

raw symbols SCIP
1 use crate::{
2 compiler,
3 error::{no_error, ErrorIterator, ValidationError},
4 evaluation::Annotations,
5 keywords::CompilationResult,
6 node::SchemaNode,
7 paths::{LazyLocation, Location},
8 types::JsonType,
9 validator::{EvaluationResult, Validate, ValidationContext},
10 };
11 use serde_json::{Map, Value};
12
13 pub(crate) struct PropertiesValidator {
14 pub(crate) properties: Vec<(String, SchemaNode)>,
15 }
16
17 impl PropertiesValidator {
18 #[inline]
19 pub(crate) fn compile<'a>(ctx: &compiler::Context, schema: &'a Value) -> CompilationResult<'a> {
20 match schema {
21 Value::Object(map) => {
22 let ctx = ctx.new_at_location("properties");
23 let mut properties = Vec::with_capacity(map.len());
24 for (key, subschema) in map {
25 let ctx = ctx.new_at_location(key.as_str());
26 properties.push((
27 key.clone(),
28 compiler::compile(&ctx, ctx.as_resource_ref(subschema))?,
29 ));
30 }
31 Ok(Box::new(PropertiesValidator { properties }))
32 }
33 _ => Err(ValidationError::single_type_error(
34 Location::new(),
35 ctx.location().clone(),
36 schema,
37 JsonType::Object,
38 )),
39 }
40 }
41 }
42
43 impl Validate for PropertiesValidator {
44 fn is_valid(&self, instance: &Value, ctx: &mut ValidationContext) -> bool {
45 if let Value::Object(item) = instance {
46 for (name, node) in &self.properties {
47 if let Some(prop) = item.get(name) {
48 if !node.is_valid(prop, ctx) {
49 return false;
50 }
51 }
52 }
53 true
54 } else {
55 true
56 }
57 }
58
59 fn validate<'i>(
60 &self,
61 instance: &'i Value,
62 location: &LazyLocation,
63 ctx: &mut ValidationContext,
64 ) -> Result<(), ValidationError<'i>> {
65 if let Value::Object(item) = instance {
66 for (name, node) in &self.properties {
67 if let Some(item) = item.get(name) {
68 node.validate(item, &location.push(name), ctx)?;
69 }
70 }
71 }
72 Ok(())
73 }
74
75 #[allow(clippy::needless_collect)]
76 fn iter_errors<'i>(
77 &self,
78 instance: &'i Value,
79 location: &LazyLocation,
80 ctx: &mut ValidationContext,
81 ) -> ErrorIterator<'i> {
82 if let Value::Object(item) = instance {
83 let mut errors = Vec::new();
84 for (name, node) in &self.properties {
85 if let Some(prop) = item.get(name) {
86 let instance_path = location.push(name.as_str());
87 errors.extend(node.iter_errors(prop, &instance_path, ctx));
88 }
89 }
90 ErrorIterator::from_iterator(errors.into_iter())
91 } else {
92 no_error()
93 }
94 }
95
96 fn evaluate(
97 &self,
98 instance: &Value,
99 location: &LazyLocation,
100 ctx: &mut ValidationContext,
101 ) -> EvaluationResult {
102 if let Value::Object(props) = instance {
103 let mut matched_props = Vec::with_capacity(props.len());
104 let mut children = Vec::new();
105 for (prop_name, node) in &self.properties {
106 if let Some(prop) = props.get(prop_name) {
107 let path = location.push(prop_name.as_str());
108 matched_props.push(prop_name.clone());
109 children.push(node.evaluate_instance(prop, &path, ctx));
110 }
111 }
112 let mut application = EvaluationResult::from_children(children);
113 application.annotate(Annotations::new(Value::from(matched_props)));
114 application
115 } else {
116 EvaluationResult::valid_empty()
117 }
118 }
119 }
120
121 #[inline]
122 pub(crate) fn compile<'a>(
123 ctx: &compiler::Context,
124 parent: &'a Map<String, Value>,
125 schema: &'a Value,
126 ) -> Option<CompilationResult<'a>> {
127 match parent.get("additionalProperties") {
128 // This type of `additionalProperties` validator handles `properties` logic
129 Some(Value::Bool(false) | Value::Object(_)) => None,
130 _ => Some(PropertiesValidator::compile(ctx, schema)),
131 }
132 }
133
134 #[cfg(test)]
135 mod tests {
136 use crate::tests_util;
137 use serde_json::json;
138
139 #[test]
140 fn location() {
141 tests_util::assert_schema_location(
142 &json!({"properties": {"foo": {"properties": {"bar": {"required": ["spam"]}}}}}),
143 &json!({"foo": {"bar": {}}}),
144 "/properties/foo/properties/bar/required",
145 );
146 }
147 }