Debian Codegraph @

src/keywords/property_names.rs

raw symbols SCIP
1 use crate::{
2 compiler,
3 error::{no_error, ErrorIterator, ValidationError},
4 keywords::CompilationResult,
5 node::SchemaNode,
6 paths::{LazyLocation, Location},
7 validator::{EvaluationResult, Validate, ValidationContext},
8 };
9 use serde_json::{Map, Value};
10
11 pub(crate) struct PropertyNamesObjectValidator {
12 node: SchemaNode,
13 }
14
15 impl PropertyNamesObjectValidator {
16 #[inline]
17 pub(crate) fn compile<'a>(ctx: &compiler::Context, schema: &'a Value) -> CompilationResult<'a> {
18 let ctx = ctx.new_at_location("propertyNames");
19 Ok(Box::new(PropertyNamesObjectValidator {
20 node: compiler::compile(&ctx, ctx.as_resource_ref(schema))?,
21 }))
22 }
23 }
24
25 impl Validate for PropertyNamesObjectValidator {
26 fn is_valid(&self, instance: &Value, ctx: &mut ValidationContext) -> bool {
27 if let Value::Object(item) = &instance {
28 item.keys().all(move |key| {
29 let wrapper = Value::String(key.clone());
30 self.node.is_valid(&wrapper, ctx)
31 })
32 } else {
33 true
34 }
35 }
36
37 fn validate<'i>(
38 &self,
39 instance: &'i Value,
40 location: &LazyLocation,
41 ctx: &mut ValidationContext,
42 ) -> Result<(), ValidationError<'i>> {
43 if let Value::Object(item) = &instance {
44 for key in item.keys() {
45 let wrapper = Value::String(key.clone());
46 match self.node.validate(&wrapper, location, ctx) {
47 Ok(()) => {}
48 Err(error) => {
49 return Err(ValidationError::property_names(
50 error.schema_path().clone(),
51 location.into(),
52 instance,
53 error.to_owned(),
54 ))
55 }
56 }
57 }
58 }
59 Ok(())
60 }
61
62 fn iter_errors<'i>(
63 &self,
64 instance: &'i Value,
65 location: &LazyLocation,
66 ctx: &mut ValidationContext,
67 ) -> ErrorIterator<'i> {
68 if let Value::Object(item) = &instance {
69 let mut errors = Vec::new();
70 for key in item.keys() {
71 let wrapper = Value::String(key.clone());
72 errors.extend(self.node.iter_errors(&wrapper, location, ctx).map(|error| {
73 ValidationError::property_names(
74 error.schema_path().clone(),
75 location.into(),
76 instance,
77 error.to_owned(),
78 )
79 }));
80 }
81 ErrorIterator::from_iterator(errors.into_iter())
82 } else {
83 no_error()
84 }
85 }
86
87 fn evaluate(
88 &self,
89 instance: &Value,
90 location: &LazyLocation,
91 ctx: &mut ValidationContext,
92 ) -> EvaluationResult {
93 if let Value::Object(item) = instance {
94 let mut children = Vec::with_capacity(item.len());
95 for key in item.keys() {
96 let wrapper = Value::String(key.clone());
97 children.push(self.node.evaluate_instance(&wrapper, location, ctx));
98 }
99 EvaluationResult::from_children(children)
100 } else {
101 EvaluationResult::valid_empty()
102 }
103 }
104 }
105
106 pub(crate) struct PropertyNamesBooleanValidator {
107 location: Location,
108 }
109
110 impl PropertyNamesBooleanValidator {
111 #[inline]
112 pub(crate) fn compile<'a>(ctx: &compiler::Context) -> CompilationResult<'a> {
113 let location = ctx.location().join("propertyNames");
114 Ok(Box::new(PropertyNamesBooleanValidator { location }))
115 }
116 }
117
118 impl Validate for PropertyNamesBooleanValidator {
119 fn is_valid(&self, instance: &Value, _ctx: &mut ValidationContext) -> bool {
120 if let Value::Object(item) = instance {
121 if !item.is_empty() {
122 return false;
123 }
124 }
125 true
126 }
127
128 fn validate<'i>(
129 &self,
130 instance: &'i Value,
131 location: &LazyLocation,
132 ctx: &mut ValidationContext,
133 ) -> Result<(), ValidationError<'i>> {
134 if self.is_valid(instance, ctx) {
135 Ok(())
136 } else {
137 Err(ValidationError::false_schema(
138 self.location.clone(),
139 location.into(),
140 instance,
141 ))
142 }
143 }
144 }
145
146 #[inline]
147 pub(crate) fn compile<'a>(
148 ctx: &compiler::Context,
149 _: &'a Map<String, Value>,
150 schema: &'a Value,
151 ) -> Option<CompilationResult<'a>> {
152 match schema {
153 Value::Object(_) => Some(PropertyNamesObjectValidator::compile(ctx, schema)),
154 Value::Bool(false) => Some(PropertyNamesBooleanValidator::compile(ctx)),
155 _ => None,
156 }
157 }
158
159 #[cfg(test)]
160 mod tests {
161 use crate::tests_util;
162 use serde_json::{json, Value};
163 use test_case::test_case;
164
165 #[test_case(&json!({"propertyNames": false}), &json!({"foo": 1}), "/propertyNames")]
166 #[test_case(&json!({"propertyNames": {"minLength": 2}}), &json!({"f": 1}), "/propertyNames/minLength")]
167 fn location(schema: &Value, instance: &Value, expected: &str) {
168 tests_util::assert_schema_location(schema, instance, expected);
169 }
170 }