valico/json_schema/validators/
content_media.rs1use serde_json::Value;
2
3use super::super::errors;
4use super::super::scope;
5
6use super::super::keywords::content_media::{ContentEncoding, ContentMediaType};
7
8#[allow(missing_copy_implementations)]
9pub struct ContentMedia {
10 pub type_: Option<ContentMediaType>,
11 pub encoding: Option<ContentEncoding>,
12}
13
14impl super::Validator for ContentMedia {
15 fn validate(
16 &self,
17 val: &Value,
18 path: &str,
19 _scope: &scope::Scope,
20 _: &super::ValidationState,
21 ) -> super::ValidationState {
22 let decoded_val = if self.encoding.is_some() && val.is_string() {
23 let v = self
24 .encoding
25 .as_ref()
26 .unwrap()
27 .decode_val(val.as_str().unwrap());
28 if v.is_err() {
29 return val_error!(errors::Format {
30 path: path.to_string(),
31 detail: v.err().unwrap(),
32 });
33 }
34 Some(Value::String(v.ok().unwrap()))
35 } else {
36 None
37 };
38
39 let val_ = if decoded_val.is_some() {
40 decoded_val.as_ref().unwrap()
41 } else {
42 val
43 };
44
45 if self.type_.is_some()
46 && val_.is_string()
47 && !self
48 .type_
49 .as_ref()
50 .unwrap()
51 .validate(val_.as_str().unwrap())
52 {
53 return val_error!(errors::Format {
54 path: path.to_string(),
55 detail: "".to_string(),
56 });
57 }
58
59 super::ValidationState::new()
60 }
61}