Skip to main content

valico/json_schema/validators/
required.rs

1use serde_json::Value;
2
3use super::super::errors;
4use super::super::scope;
5
6#[allow(missing_copy_implementations)]
7pub struct Required {
8    pub items: Vec<String>,
9}
10
11impl super::Validator for Required {
12    fn validate(
13        &self,
14        val: &Value,
15        path: &str,
16        _scope: &scope::Scope,
17        _: &super::ValidationState,
18    ) -> super::ValidationState {
19        let object = nonstrict_process!(val.as_object(), path);
20        let mut state = super::ValidationState::new();
21
22        for key in self.items.iter() {
23            if !object.contains_key(key) {
24                state.errors.push(Box::new(errors::Required {
25                    path: [path, key.as_ref()].join("/"),
26                }))
27            }
28        }
29
30        state
31    }
32}