valico/json_schema/keywords/
required.rs
1use serde_json::{Value};
2
3use super::super::schema;
4use super::super::validators;
5
6#[allow(missing_copy_implementations)]
7pub struct Required;
8impl super::Keyword for Required {
9 fn compile(&self, def: &Value, ctx: &schema::WalkContext) -> super::KeywordResult {
10 let required = keyword_key_exists!(def, "required");
11
12 if required.is_array() {
13 let required = required.as_array().unwrap();
14
15 if required.len() == 0 {
16 return Err(schema::SchemaError::Malformed {
17 path: ctx.fragment.join("/"),
18 detail: "This array MUST have at least one element.".to_string()
19 })
20 }
21
22 let mut items = vec![];
23 for item in required.iter() {
24 if item.is_string() {
25 items.push(item.as_str().unwrap().to_string())
26 } else {
27 return Err(schema::SchemaError::Malformed {
28 path: ctx.fragment.join("/"),
29 detail: "The values of `required` MUST be strings".to_string()
30 })
31 }
32 }
33
34 Ok(Some(Box::new(validators::Required {
35 items: items
36 })))
37 } else {
38 Err(schema::SchemaError::Malformed {
39 path: ctx.fragment.join("/"),
40 detail: "The value of this keyword MUST be an array.".to_string()
41 })
42 }
43 }
44}
45
46#[cfg(test)] use super::super::scope;
47#[cfg(test)] use jsonway;
48#[cfg(test)] use super::super::builder;
49
50#[test]
51fn validate() {
52 let mut scope = scope::Scope::new();
53 let schema = scope.compile_and_return(builder::schema(|s| {
54 s.required(vec!["prop1".to_string(), "prop2".to_string()]);
55 }).into_json(), true).ok().unwrap();
56
57 assert_eq!(schema.validate(&jsonway::object(|obj| {
58 obj.set("prop1", 0);
59 }).unwrap()).is_valid(), false);
60
61 assert_eq!(schema.validate(&jsonway::object(|obj| {
62 obj.set("prop2", 0);
63 }).unwrap()).is_valid(), false);
64
65 assert_eq!(schema.validate(&jsonway::object(|obj| {
66 obj.set("prop1", 0);
67 obj.set("prop2", 0);
68 }).unwrap()).is_valid(), true);
69}
70
71#[test]
72fn malformed() {
73 let mut scope = scope::Scope::new();
74
75 assert!(scope.compile_and_return(jsonway::object(|schema| {
76 schema.array("required", |_| {});
77 }).unwrap(), true).is_err());
78
79 assert!(scope.compile_and_return(jsonway::object(|schema| {
80 schema.array("required", |required| {
81 required.push(1)
82 });
83 }).unwrap(), true).is_err());
84}