Debian Codegraph @

src/keywords/if_.rs

raw symbols SCIP
1 use crate::{
2 compiler,
3 error::{no_error, ErrorIterator},
4 keywords::CompilationResult,
5 node::SchemaNode,
6 paths::LazyLocation,
7 validator::{EvaluationResult, Validate, ValidationContext},
8 ValidationError,
9 };
10 use serde_json::{Map, Value};
11
12 pub(crate) struct IfThenValidator {
13 schema: SchemaNode,
14 then_schema: SchemaNode,
15 }
16
17 impl IfThenValidator {
18 #[inline]
19 pub(crate) fn compile<'a>(
20 ctx: &compiler::Context,
21 schema: &'a Value,
22 then_schema: &'a Value,
23 ) -> CompilationResult<'a> {
24 Ok(Box::new(IfThenValidator {
25 schema: {
26 let ctx = ctx.new_at_location("if");
27 compiler::compile(&ctx, ctx.as_resource_ref(schema))?
28 },
29 then_schema: {
30 let ctx = ctx.new_at_location("then");
31 compiler::compile(&ctx, ctx.as_resource_ref(then_schema))?
32 },
33 }))
34 }
35 }
36
37 impl Validate for IfThenValidator {
38 fn is_valid(&self, instance: &Value, ctx: &mut ValidationContext) -> bool {
39 if self.schema.is_valid(instance, ctx) {
40 self.then_schema.is_valid(instance, ctx)
41 } else {
42 true
43 }
44 }
45
46 fn validate<'i>(
47 &self,
48 instance: &'i Value,
49 location: &LazyLocation,
50 ctx: &mut ValidationContext,
51 ) -> Result<(), ValidationError<'i>> {
52 if self.schema.is_valid(instance, ctx) {
53 self.then_schema.validate(instance, location, ctx)
54 } else {
55 Ok(())
56 }
57 }
58
59 #[allow(clippy::needless_collect)]
60 fn iter_errors<'i>(
61 &self,
62 instance: &'i Value,
63 location: &LazyLocation,
64 ctx: &mut ValidationContext,
65 ) -> ErrorIterator<'i> {
66 if self.schema.is_valid(instance, ctx) {
67 let errors: Vec<_> = self
68 .then_schema
69 .iter_errors(instance, location, ctx)
70 .collect();
71 ErrorIterator::from_iterator(errors.into_iter())
72 } else {
73 no_error()
74 }
75 }
76
77 fn evaluate(
78 &self,
79 instance: &Value,
80 location: &LazyLocation,
81 ctx: &mut ValidationContext,
82 ) -> EvaluationResult {
83 let if_node = self.schema.evaluate_instance(instance, location, ctx);
84 if if_node.valid {
85 let then_node = self.then_schema.evaluate_instance(instance, location, ctx);
86 EvaluationResult::from_children(vec![if_node, then_node])
87 } else {
88 EvaluationResult::valid_empty()
89 }
90 }
91 }
92
93 pub(crate) struct IfElseValidator {
94 schema: SchemaNode,
95 else_schema: SchemaNode,
96 }
97
98 impl IfElseValidator {
99 #[inline]
100 pub(crate) fn compile<'a>(
101 ctx: &compiler::Context,
102 schema: &'a Value,
103 else_schema: &'a Value,
104 ) -> CompilationResult<'a> {
105 Ok(Box::new(IfElseValidator {
106 schema: {
107 let ctx = ctx.new_at_location("if");
108 compiler::compile(&ctx, ctx.as_resource_ref(schema))?
109 },
110 else_schema: {
111 let ctx = ctx.new_at_location("else");
112 compiler::compile(&ctx, ctx.as_resource_ref(else_schema))?
113 },
114 }))
115 }
116 }
117
118 impl Validate for IfElseValidator {
119 fn is_valid(&self, instance: &Value, ctx: &mut ValidationContext) -> bool {
120 if self.schema.is_valid(instance, ctx) {
121 true
122 } else {
123 self.else_schema.is_valid(instance, ctx)
124 }
125 }
126
127 fn validate<'i>(
128 &self,
129 instance: &'i Value,
130 location: &LazyLocation,
131 ctx: &mut ValidationContext,
132 ) -> Result<(), ValidationError<'i>> {
133 if self.schema.is_valid(instance, ctx) {
134 Ok(())
135 } else {
136 self.else_schema.validate(instance, location, ctx)
137 }
138 }
139
140 #[allow(clippy::needless_collect)]
141 fn iter_errors<'i>(
142 &self,
143 instance: &'i Value,
144 location: &LazyLocation,
145 ctx: &mut ValidationContext,
146 ) -> ErrorIterator<'i> {
147 if self.schema.is_valid(instance, ctx) {
148 no_error()
149 } else {
150 let errors: Vec<_> = self
151 .else_schema
152 .iter_errors(instance, location, ctx)
153 .collect();
154 ErrorIterator::from_iterator(errors.into_iter())
155 }
156 }
157
158 fn evaluate(
159 &self,
160 instance: &Value,
161 location: &LazyLocation,
162 ctx: &mut ValidationContext,
163 ) -> EvaluationResult {
164 let if_node = self.schema.evaluate_instance(instance, location, ctx);
165 if if_node.valid {
166 EvaluationResult::from_children(vec![if_node])
167 } else {
168 let else_node = self.else_schema.evaluate_instance(instance, location, ctx);
169 EvaluationResult::from_children(vec![else_node])
170 }
171 }
172 }
173
174 pub(crate) struct IfThenElseValidator {
175 schema: SchemaNode,
176 then_schema: SchemaNode,
177 else_schema: SchemaNode,
178 }
179
180 impl IfThenElseValidator {
181 #[inline]
182 pub(crate) fn compile<'a>(
183 ctx: &compiler::Context,
184 schema: &'a Value,
185 then_schema: &'a Value,
186 else_schema: &'a Value,
187 ) -> CompilationResult<'a> {
188 Ok(Box::new(IfThenElseValidator {
189 schema: {
190 let ctx = ctx.new_at_location("if");
191 compiler::compile(&ctx, ctx.as_resource_ref(schema))?
192 },
193 then_schema: {
194 let ctx = ctx.new_at_location("then");
195 compiler::compile(&ctx, ctx.as_resource_ref(then_schema))?
196 },
197 else_schema: {
198 let ctx = ctx.new_at_location("else");
199 compiler::compile(&ctx, ctx.as_resource_ref(else_schema))?
200 },
201 }))
202 }
203 }
204
205 impl Validate for IfThenElseValidator {
206 fn is_valid(&self, instance: &Value, ctx: &mut ValidationContext) -> bool {
207 if self.schema.is_valid(instance, ctx) {
208 self.then_schema.is_valid(instance, ctx)
209 } else {
210 self.else_schema.is_valid(instance, ctx)
211 }
212 }
213
214 fn validate<'i>(
215 &self,
216 instance: &'i Value,
217 location: &LazyLocation,
218 ctx: &mut ValidationContext,
219 ) -> Result<(), ValidationError<'i>> {
220 if self.schema.is_valid(instance, ctx) {
221 self.then_schema.validate(instance, location, ctx)
222 } else {
223 self.else_schema.validate(instance, location, ctx)
224 }
225 }
226
227 #[allow(clippy::needless_collect)]
228 fn iter_errors<'i>(
229 &self,
230 instance: &'i Value,
231 location: &LazyLocation,
232 ctx: &mut ValidationContext,
233 ) -> ErrorIterator<'i> {
234 if self.schema.is_valid(instance, ctx) {
235 let errors: Vec<_> = self
236 .then_schema
237 .iter_errors(instance, location, ctx)
238 .collect();
239 ErrorIterator::from_iterator(errors.into_iter())
240 } else {
241 let errors: Vec<_> = self
242 .else_schema
243 .iter_errors(instance, location, ctx)
244 .collect();
245 ErrorIterator::from_iterator(errors.into_iter())
246 }
247 }
248
249 fn evaluate(
250 &self,
251 instance: &Value,
252 location: &LazyLocation,
253 ctx: &mut ValidationContext,
254 ) -> EvaluationResult {
255 let if_node = self.schema.evaluate_instance(instance, location, ctx);
256 if if_node.valid {
257 let then_node = self.then_schema.evaluate_instance(instance, location, ctx);
258 EvaluationResult::from_children(vec![if_node, then_node])
259 } else {
260 let else_node = self.else_schema.evaluate_instance(instance, location, ctx);
261 EvaluationResult::from_children(vec![else_node])
262 }
263 }
264 }
265
266 #[inline]
267 pub(crate) fn compile<'a>(
268 ctx: &compiler::Context,
269 parent: &'a Map<String, Value>,
270 schema: &'a Value,
271 ) -> Option<CompilationResult<'a>> {
272 let then = parent.get("then");
273 let else_ = parent.get("else");
274 match (then, else_) {
275 (Some(then_schema), Some(else_schema)) => Some(IfThenElseValidator::compile(
276 ctx,
277 schema,
278 then_schema,
279 else_schema,
280 )),
281 (None, Some(else_schema)) => Some(IfElseValidator::compile(ctx, schema, else_schema)),
282 (Some(then_schema), None) => Some(IfThenValidator::compile(ctx, schema, then_schema)),
283 (None, None) => None,
284 }
285 }
286
287 #[cfg(test)]
288 mod tests {
289 use crate::tests_util;
290 use serde_json::{json, Value};
291 use test_case::test_case;
292
293 #[test_case(&json!({"if": {"minimum": 0}, "else": {"multipleOf": 2}}), &json!(-1), "/else/multipleOf")]
294 #[test_case(&json!({"if": {"minimum": 0}, "then": {"multipleOf": 2}}), &json!(3), "/then/multipleOf")]
295 #[test_case(&json!({"if": {"minimum": 0}, "then": {"multipleOf": 2}, "else": {"multipleOf": 2}}), &json!(-1), "/else/multipleOf")]
296 #[test_case(&json!({"if": {"minimum": 0}, "then": {"multipleOf": 2}, "else": {"multipleOf": 2}}), &json!(3), "/then/multipleOf")]
297 fn location(schema: &Value, instance: &Value, expected: &str) {
298 tests_util::assert_schema_location(schema, instance, expected);
299 }
300 }