| 1 | use crate::{ |
| 2 | compiler, |
| 3 | error::{no_error, ErrorIterator, ValidationError}, |
| 4 | keywords::CompilationResult, |
| 5 | paths::{LazyLocation, Location}, |
| 6 | types::JsonType, |
| 7 | validator::{Validate, ValidationContext}, |
| 8 | }; |
| 9 | use serde_json::{Map, Value}; |
| 10 | |
| 11 | pub(crate) struct RequiredValidator { |
| 12 | required: Vec<String>, |
| 13 | location: Location, |
| 14 | } |
| 15 | |
| 16 | impl RequiredValidator { |
| 17 | #[inline] |
| 18 | pub(crate) fn compile(items: &[Value], location: Location) -> CompilationResult<'_> { |
| 19 | let mut required = Vec::with_capacity(items.len()); |
| 20 | for item in items { |
| 21 | match item { |
| 22 | Value::String(string) => required.push(string.clone()), |
| 23 | _ => { |
| 24 | return Err(ValidationError::single_type_error( |
| 25 | Location::new(), |
| 26 | location, |
| 27 | item, |
| 28 | JsonType::String, |
| 29 | )) |
| 30 | } |
| 31 | } |
| 32 | } |
| 33 | Ok(Box::new(RequiredValidator { required, location })) |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | impl Validate for RequiredValidator { |
| 38 | fn is_valid(&self, instance: &Value, _ctx: &mut ValidationContext) -> bool { |
| 39 | if let Value::Object(item) = instance { |
| 40 | if item.len() < self.required.len() { |
| 41 | return false; |
| 42 | } |
| 43 | self.required |
| 44 | .iter() |
| 45 | .all(|property_name| item.contains_key(property_name)) |
| 46 | } else { |
| 47 | true |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | fn validate<'i>( |
| 52 | &self, |
| 53 | instance: &'i Value, |
| 54 | location: &LazyLocation, |
| 55 | _ctx: &mut ValidationContext, |
| 56 | ) -> Result<(), ValidationError<'i>> { |
| 57 | if let Value::Object(item) = instance { |
| 58 | for property_name in &self.required { |
| 59 | if !item.contains_key(property_name) { |
| 60 | return Err(ValidationError::required( |
| 61 | self.location.clone(), |
| 62 | location.into(), |
| 63 | instance, |
| 64 | // Value enum is needed for proper string escaping |
| 65 | Value::String(property_name.clone()), |
| 66 | )); |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | Ok(()) |
| 71 | } |
| 72 | fn iter_errors<'i>( |
| 73 | &self, |
| 74 | instance: &'i Value, |
| 75 | location: &LazyLocation, |
| 76 | _ctx: &mut ValidationContext, |
| 77 | ) -> ErrorIterator<'i> { |
| 78 | if let Value::Object(item) = instance { |
| 79 | let mut errors = vec![]; |
| 80 | for property_name in &self.required { |
| 81 | if !item.contains_key(property_name) { |
| 82 | errors.push(ValidationError::required( |
| 83 | self.location.clone(), |
| 84 | location.into(), |
| 85 | instance, |
| 86 | // Value enum is needed for proper string escaping |
| 87 | Value::String(property_name.clone()), |
| 88 | )); |
| 89 | } |
| 90 | } |
| 91 | if !errors.is_empty() { |
| 92 | return ErrorIterator::from_iterator(errors.into_iter()); |
| 93 | } |
| 94 | } |
| 95 | no_error() |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | pub(crate) struct SingleItemRequiredValidator { |
| 100 | value: String, |
| 101 | location: Location, |
| 102 | } |
| 103 | |
| 104 | impl SingleItemRequiredValidator { |
| 105 | #[inline] |
| 106 | pub(crate) fn compile(value: &str, location: Location) -> CompilationResult<'_> { |
| 107 | Ok(Box::new(SingleItemRequiredValidator { |
| 108 | value: value.to_string(), |
| 109 | location, |
| 110 | })) |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | impl Validate for SingleItemRequiredValidator { |
| 115 | fn validate<'i>( |
| 116 | &self, |
| 117 | instance: &'i Value, |
| 118 | location: &LazyLocation, |
| 119 | ctx: &mut ValidationContext, |
| 120 | ) -> Result<(), ValidationError<'i>> { |
| 121 | if !self.is_valid(instance, ctx) { |
| 122 | return Err(ValidationError::required( |
| 123 | self.location.clone(), |
| 124 | location.into(), |
| 125 | instance, |
| 126 | // Value enum is needed for proper string escaping |
| 127 | Value::String(self.value.clone()), |
| 128 | )); |
| 129 | } |
| 130 | Ok(()) |
| 131 | } |
| 132 | |
| 133 | fn is_valid(&self, instance: &Value, _ctx: &mut ValidationContext) -> bool { |
| 134 | if let Value::Object(item) = instance { |
| 135 | if item.is_empty() { |
| 136 | return false; |
| 137 | } |
| 138 | item.contains_key(&self.value) |
| 139 | } else { |
| 140 | true |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | #[inline] |
| 146 | pub(crate) fn compile<'a>( |
| 147 | ctx: &compiler::Context, |
| 148 | _: &'a Map<String, Value>, |
| 149 | schema: &'a Value, |
| 150 | ) -> Option<CompilationResult<'a>> { |
| 151 | let location = ctx.location().join("required"); |
| 152 | compile_with_path(schema, location) |
| 153 | } |
| 154 | |
| 155 | #[inline] |
| 156 | pub(crate) fn compile_with_path( |
| 157 | schema: &Value, |
| 158 | location: Location, |
| 159 | ) -> Option<CompilationResult<'_>> { |
| 160 | // IMPORTANT: If this function will ever return `None`, adjust `dependencies.rs` accordingly |
| 161 | match schema { |
| 162 | Value::Array(items) => { |
| 163 | if items.len() == 1 { |
| 164 | let item = &items[0]; |
| 165 | if let Value::String(item) = item { |
| 166 | Some(SingleItemRequiredValidator::compile(item, location)) |
| 167 | } else { |
| 168 | Some(Err(ValidationError::single_type_error( |
| 169 | Location::new(), |
| 170 | location, |
| 171 | item, |
| 172 | JsonType::String, |
| 173 | ))) |
| 174 | } |
| 175 | } else { |
| 176 | Some(RequiredValidator::compile(items, location)) |
| 177 | } |
| 178 | } |
| 179 | _ => Some(Err(ValidationError::single_type_error( |
| 180 | Location::new(), |
| 181 | location, |
| 182 | schema, |
| 183 | JsonType::Array, |
| 184 | ))), |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | #[cfg(test)] |
| 189 | mod tests { |
| 190 | use crate::tests_util; |
| 191 | use serde_json::{json, Value}; |
| 192 | use test_case::test_case; |
| 193 | |
| 194 | #[test_case(&json!({"required": ["a"]}), &json!({}), "/required")] |
| 195 | #[test_case(&json!({"required": ["a", "b"]}), &json!({}), "/required")] |
| 196 | fn location(schema: &Value, instance: &Value, expected: &str) { |
| 197 | tests_util::assert_schema_location(schema, instance, expected); |
| 198 | } |
| 199 | } |