Debian Codegraph @

src/keywords/const_.rs

raw symbols SCIP
1 use crate::{
2 compiler,
3 error::ValidationError,
4 ext::cmp,
5 keywords::CompilationResult,
6 paths::Location,
7 validator::{Validate, ValidationContext},
8 };
9 use serde_json::{Map, Number, Value};
10
11 use crate::paths::LazyLocation;
12
13 struct ConstArrayValidator {
14 value: Vec<Value>,
15 location: Location,
16 }
17 impl ConstArrayValidator {
18 #[inline]
19 pub(crate) fn compile(value: &[Value], location: Location) -> CompilationResult<'_> {
20 Ok(Box::new(ConstArrayValidator {
21 value: value.to_vec(),
22 location,
23 }))
24 }
25 }
26 impl Validate for ConstArrayValidator {
27 fn validate<'i>(
28 &self,
29 instance: &'i Value,
30 location: &LazyLocation,
31 ctx: &mut ValidationContext,
32 ) -> Result<(), ValidationError<'i>> {
33 if self.is_valid(instance, ctx) {
34 Ok(())
35 } else {
36 Err(ValidationError::constant_array(
37 self.location.clone(),
38 location.into(),
39 instance,
40 &self.value,
41 ))
42 }
43 }
44
45 #[inline]
46 fn is_valid(&self, instance: &Value, _ctx: &mut ValidationContext) -> bool {
47 if let Value::Array(instance_value) = instance {
48 cmp::equal_arrays(&self.value, instance_value)
49 } else {
50 false
51 }
52 }
53 }
54
55 struct ConstBooleanValidator {
56 value: bool,
57 location: Location,
58 }
59 impl ConstBooleanValidator {
60 #[inline]
61 pub(crate) fn compile<'a>(value: bool, location: Location) -> CompilationResult<'a> {
62 Ok(Box::new(ConstBooleanValidator { value, location }))
63 }
64 }
65 impl Validate for ConstBooleanValidator {
66 fn validate<'i>(
67 &self,
68 instance: &'i Value,
69 location: &LazyLocation,
70 ctx: &mut ValidationContext,
71 ) -> Result<(), ValidationError<'i>> {
72 if self.is_valid(instance, ctx) {
73 Ok(())
74 } else {
75 Err(ValidationError::constant_boolean(
76 self.location.clone(),
77 location.into(),
78 instance,
79 self.value,
80 ))
81 }
82 }
83
84 #[inline]
85 fn is_valid(&self, instance: &Value, _ctx: &mut ValidationContext) -> bool {
86 if let Value::Bool(instance_value) = instance {
87 &self.value == instance_value
88 } else {
89 false
90 }
91 }
92 }
93
94 struct ConstNullValidator {
95 location: Location,
96 }
97 impl ConstNullValidator {
98 #[inline]
99 pub(crate) fn compile<'a>(location: Location) -> CompilationResult<'a> {
100 Ok(Box::new(ConstNullValidator { location }))
101 }
102 }
103 impl Validate for ConstNullValidator {
104 fn validate<'i>(
105 &self,
106 instance: &'i Value,
107 location: &LazyLocation,
108 ctx: &mut ValidationContext,
109 ) -> Result<(), ValidationError<'i>> {
110 if self.is_valid(instance, ctx) {
111 Ok(())
112 } else {
113 Err(ValidationError::constant_null(
114 self.location.clone(),
115 location.into(),
116 instance,
117 ))
118 }
119 }
120 #[inline]
121 fn is_valid(&self, instance: &Value, _ctx: &mut ValidationContext) -> bool {
122 instance.is_null()
123 }
124 }
125
126 struct ConstNumberValidator {
127 // This is saved in order to ensure that the error message is not altered by precision loss
128 original_value: Number,
129 location: Location,
130 }
131
132 impl ConstNumberValidator {
133 #[inline]
134 pub(crate) fn compile(original_value: &Number, location: Location) -> CompilationResult<'_> {
135 Ok(Box::new(ConstNumberValidator {
136 original_value: original_value.clone(),
137 location,
138 }))
139 }
140 }
141
142 impl Validate for ConstNumberValidator {
143 fn validate<'i>(
144 &self,
145 instance: &'i Value,
146 location: &LazyLocation,
147 ctx: &mut ValidationContext,
148 ) -> Result<(), ValidationError<'i>> {
149 if self.is_valid(instance, ctx) {
150 Ok(())
151 } else {
152 Err(ValidationError::constant_number(
153 self.location.clone(),
154 location.into(),
155 instance,
156 &self.original_value,
157 ))
158 }
159 }
160
161 #[inline]
162 fn is_valid(&self, instance: &Value, _ctx: &mut ValidationContext) -> bool {
163 if let Value::Number(item) = instance {
164 crate::ext::cmp::equal_numbers(item, &self.original_value)
165 } else {
166 false
167 }
168 }
169 }
170
171 pub(crate) struct ConstObjectValidator {
172 value: Map<String, Value>,
173 location: Location,
174 }
175
176 impl ConstObjectValidator {
177 #[inline]
178 pub(crate) fn compile(value: &Map<String, Value>, location: Location) -> CompilationResult<'_> {
179 Ok(Box::new(ConstObjectValidator {
180 value: value.clone(),
181 location,
182 }))
183 }
184 }
185
186 impl Validate for ConstObjectValidator {
187 fn validate<'i>(
188 &self,
189 instance: &'i Value,
190 location: &LazyLocation,
191 ctx: &mut ValidationContext,
192 ) -> Result<(), ValidationError<'i>> {
193 if self.is_valid(instance, ctx) {
194 Ok(())
195 } else {
196 Err(ValidationError::constant_object(
197 self.location.clone(),
198 location.into(),
199 instance,
200 &self.value,
201 ))
202 }
203 }
204
205 #[inline]
206 fn is_valid(&self, instance: &Value, _ctx: &mut ValidationContext) -> bool {
207 if let Value::Object(item) = instance {
208 cmp::equal_objects(&self.value, item)
209 } else {
210 false
211 }
212 }
213 }
214
215 pub(crate) struct ConstStringValidator {
216 value: String,
217 location: Location,
218 }
219
220 impl ConstStringValidator {
221 #[inline]
222 pub(crate) fn compile(value: &str, location: Location) -> CompilationResult<'_> {
223 Ok(Box::new(ConstStringValidator {
224 value: value.to_string(),
225 location,
226 }))
227 }
228 }
229
230 impl Validate for ConstStringValidator {
231 fn validate<'i>(
232 &self,
233 instance: &'i Value,
234 location: &LazyLocation,
235 ctx: &mut ValidationContext,
236 ) -> Result<(), ValidationError<'i>> {
237 if self.is_valid(instance, ctx) {
238 Ok(())
239 } else {
240 Err(ValidationError::constant_string(
241 self.location.clone(),
242 location.into(),
243 instance,
244 &self.value,
245 ))
246 }
247 }
248
249 #[inline]
250 fn is_valid(&self, instance: &Value, _ctx: &mut ValidationContext) -> bool {
251 if let Value::String(item) = instance {
252 &self.value == item
253 } else {
254 false
255 }
256 }
257 }
258
259 #[inline]
260 pub(crate) fn compile<'a>(
261 ctx: &compiler::Context,
262 _: &'a Map<String, Value>,
263 schema: &'a Value,
264 ) -> Option<CompilationResult<'a>> {
265 let location = ctx.location().join("const");
266 match schema {
267 Value::Array(items) => Some(ConstArrayValidator::compile(items, location)),
268 Value::Bool(item) => Some(ConstBooleanValidator::compile(*item, location)),
269 Value::Null => Some(ConstNullValidator::compile(location)),
270 Value::Number(item) => Some(ConstNumberValidator::compile(item, location)),
271 Value::Object(map) => Some(ConstObjectValidator::compile(map, location)),
272 Value::String(string) => Some(ConstStringValidator::compile(string, location)),
273 }
274 }
275
276 #[cfg(test)]
277 mod tests {
278 use crate::tests_util;
279 use serde_json::{json, Value};
280 use test_case::test_case;
281
282 #[test_case(&json!({"const": 1}), &json!(2), "/const")]
283 #[test_case(&json!({"const": null}), &json!(3), "/const")]
284 #[test_case(&json!({"const": false}), &json!(4), "/const")]
285 #[test_case(&json!({"const": []}), &json!(5), "/const")]
286 #[test_case(&json!({"const": {}}), &json!(6), "/const")]
287 #[test_case(&json!({"const": ""}), &json!(7), "/const")]
288 fn location(schema: &Value, instance: &Value, expected: &str) {
289 tests_util::assert_schema_location(schema, instance, expected);
290 }
291
292 // Tests for arbitrary-precision const validation
293 #[cfg(feature = "arbitrary-precision")]
294 mod arbitrary_precision {
295 use crate::tests_util;
296 use serde_json::Value;
297 use test_case::test_case;
298
299 fn parse_json(json: &str) -> Value {
300 serde_json::from_str(json).unwrap()
301 }
302
303 #[test_case(r#"{"const": 18446744073709551617}"#, "18446744073709551617", true; "large int exact match")]
304 #[test_case(r#"{"const": 18446744073709551617}"#, "18446744073709551616", false; "large int different by one")]
305 #[test_case(r#"{"const": 18446744073709551617}"#, "18446744073709551618", false; "large int different by one above")]
306 #[test_case(r#"{"const": -9223372036854775809}"#, "-9223372036854775809", true; "large negative int match")]
307 #[test_case(r#"{"const": -9223372036854775809}"#, "-9223372036854775808", false; "large negative int different")]
308 #[test_case(r#"{"const": 0.1}"#, "0.1", true; "decimal exact match")]
309 #[test_case(r#"{"const": 0.1}"#, "0.10000000000000001", false; "decimal precision difference")]
310 fn const_arbitrary_precision(schema_json: &str, instance_json: &str, expected_valid: bool) {
311 let schema = parse_json(schema_json);
312 let instance = parse_json(instance_json);
313 if expected_valid {
314 tests_util::is_valid(&schema, &instance);
315 } else {
316 tests_util::is_not_valid(&schema, &instance);
317 }
318 }
319 }
320 }