1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
//! Legacy `dependencies` keyword (Draft 4/6/7).
//!
//! WHY: Before Draft 2019-09, `dependencies` combined what are now
//! `dependentRequired` (array of required property names) and
//! `dependentSchemas` (sub-schema validation) into a single keyword.
//! This validator handles both forms for backward compatibility.
use alloc::boxed::Box;
use alloc::collections::BTreeMap;
use alloc::string::String;
use alloc::vec::Vec;
use serde_json::Value;
use crate::error::{ErrorIterator, ValidationError, ValidationErrorBuilder, ValidationErrorKind};
use crate::node::SchemaNode;
use crate::paths::{LazyLocation, Location};
use super::{Validate, ValidationContext};
/// Either a list of required properties or a sub-schema.
pub enum Dependency {
Required(Vec<String>),
Schema(SchemaNode),
}
/// Validates legacy `dependencies` keyword.
pub struct DependenciesValidator {
deps: BTreeMap<String, Dependency>,
}
impl DependenciesValidator {
/// Create with pre-compiled dependencies.
#[must_use]
pub fn new(deps: BTreeMap<String, Dependency>) -> Self {
Self { deps }
}
}
impl Validate for DependenciesValidator {
fn is_valid(&self, instance: &Value, ctx: &mut ValidationContext) -> bool {
if let Value::Object(obj) = instance {
for (prop, dep) in &self.deps {
if obj.contains_key(prop) {
match dep {
Dependency::Required(missing_props) => {
for m in missing_props {
if !obj.contains_key(m) {
return false;
}
}
}
Dependency::Schema(schema) => {
if !schema.is_valid(instance, ctx) {
return false;
}
}
}
}
}
}
true
}
fn validate(
&self,
instance: &Value,
instance_path: &LazyLocation<'_>,
ctx: &mut ValidationContext,
) -> Result<(), ValidationError> {
if let Value::Object(obj) = instance {
for (prop, dep) in &self.deps {
if obj.contains_key(prop) {
match dep {
Dependency::Required(missing_props) => {
let missing: Vec<String> = missing_props
.iter()
.filter(|m| !obj.contains_key(*m))
.cloned()
.collect();
if !missing.is_empty() {
return Err(ValidationErrorBuilder::new(
instance_path.materialize(),
Location::new(),
)
.build(
ValidationErrorKind::DependentRequired {
property: prop.clone(),
missing,
},
));
}
}
Dependency::Schema(schema) => {
schema.validate(instance, instance_path, ctx)?;
}
}
}
}
}
Ok(())
}
fn iter_errors(
&self,
instance: &Value,
instance_path: &LazyLocation<'_>,
ctx: &mut ValidationContext,
) -> ErrorIterator {
let mut errors: Vec<ValidationError> = Vec::new();
if let Value::Object(obj) = instance {
for (prop, dep) in &self.deps {
if obj.contains_key(prop) {
match dep {
Dependency::Required(missing_props) => {
let missing: Vec<String> = missing_props
.iter()
.filter(|m| !obj.contains_key(*m))
.cloned()
.collect();
if !missing.is_empty() {
errors.push(
ValidationErrorBuilder::new(
instance_path.materialize(),
Location::new(),
)
.build(
ValidationErrorKind::DependentRequired {
property: prop.clone(),
missing,
},
),
);
}
}
Dependency::Schema(schema) => {
for e in schema.iter_errors(instance, instance_path, ctx) {
errors.push(e);
}
}
}
}
}
}
Box::new(errors.into_iter())
}
}