valico/json_schema/validators/
unique_items.rs
1use serde_json::{Value};
2
3use super::super::errors;
4use super::super::scope;
5
6#[allow(missing_copy_implementations)]
7pub struct UniqueItems;
8impl super::Validator for UniqueItems {
9 fn validate(&self, val: &Value, path: &str, _scope: &scope::Scope) -> super::ValidationState {
10 let array = nonstrict_process!(val.as_array(), path);
11
12 let mut unique = true;
15 'main: for (idx, item_i) in array.iter().enumerate() {
16 for item_j in array[..idx].iter() {
17 if item_i == item_j {
18 unique = false;
19 break 'main;
20 }
21 }
22
23 for item_j in array[(idx + 1)..].iter() {
24 if item_i == item_j {
25 unique = false;
26 break 'main;
27 }
28 }
29 }
30
31 if unique {
32 super::ValidationState::new()
33 } else {
34 val_error!(
35 errors::UniqueItems {
36 path: path.to_string()
37 }
38 )
39 }
40 }
41}