| 1 | use crate::{ |
| 2 | compiler, |
| 3 | error::ValidationError, |
| 4 | ext::cmp, |
| 5 | keywords::CompilationResult, |
| 6 | paths::{LazyLocation, Location}, |
| 7 | types::{JsonType, JsonTypeSet}, |
| 8 | validator::{Validate, ValidationContext}, |
| 9 | }; |
| 10 | use serde_json::{Map, Value}; |
| 11 | |
| 12 | #[derive(Debug)] |
| 13 | pub(crate) struct EnumValidator { |
| 14 | options: Value, |
| 15 | // Types that occur in items |
| 16 | types: JsonTypeSet, |
| 17 | items: Vec<Value>, |
| 18 | location: Location, |
| 19 | } |
| 20 | |
| 21 | impl EnumValidator { |
| 22 | #[inline] |
| 23 | pub(crate) fn compile<'a>( |
| 24 | schema: &'a Value, |
| 25 | items: &'a [Value], |
| 26 | location: Location, |
| 27 | ) -> CompilationResult<'a> { |
| 28 | let mut types = JsonTypeSet::empty(); |
| 29 | for item in items { |
| 30 | types = types.insert(JsonType::from(item)); |
| 31 | } |
| 32 | Ok(Box::new(EnumValidator { |
| 33 | options: schema.clone(), |
| 34 | items: items.to_vec(), |
| 35 | types, |
| 36 | location, |
| 37 | })) |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | impl Validate for EnumValidator { |
| 42 | fn validate<'i>( |
| 43 | &self, |
| 44 | instance: &'i Value, |
| 45 | location: &LazyLocation, |
| 46 | ctx: &mut ValidationContext, |
| 47 | ) -> Result<(), ValidationError<'i>> { |
| 48 | if self.is_valid(instance, ctx) { |
| 49 | Ok(()) |
| 50 | } else { |
| 51 | Err(ValidationError::enumeration( |
| 52 | self.location.clone(), |
| 53 | location.into(), |
| 54 | instance, |
| 55 | &self.options, |
| 56 | )) |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | fn is_valid(&self, instance: &Value, _ctx: &mut ValidationContext) -> bool { |
| 61 | // If the input value type is not in the types present among the enum options, then there |
| 62 | // is no reason to compare it against all items - we know that |
| 63 | // there are no items with such type at all |
| 64 | if self.types.contains_value_type(instance) { |
| 65 | self.items.iter().any(|item| cmp::equal(instance, item)) |
| 66 | } else { |
| 67 | false |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | #[derive(Debug)] |
| 73 | pub(crate) struct SingleValueEnumValidator { |
| 74 | value: Value, |
| 75 | options: Value, |
| 76 | location: Location, |
| 77 | } |
| 78 | |
| 79 | impl SingleValueEnumValidator { |
| 80 | #[inline] |
| 81 | pub(crate) fn compile<'a>( |
| 82 | schema: &'a Value, |
| 83 | value: &'a Value, |
| 84 | location: Location, |
| 85 | ) -> CompilationResult<'a> { |
| 86 | Ok(Box::new(SingleValueEnumValidator { |
| 87 | options: schema.clone(), |
| 88 | value: value.clone(), |
| 89 | location, |
| 90 | })) |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | impl Validate for SingleValueEnumValidator { |
| 95 | fn validate<'i>( |
| 96 | &self, |
| 97 | instance: &'i Value, |
| 98 | location: &LazyLocation, |
| 99 | ctx: &mut ValidationContext, |
| 100 | ) -> Result<(), ValidationError<'i>> { |
| 101 | if self.is_valid(instance, ctx) { |
| 102 | Ok(()) |
| 103 | } else { |
| 104 | Err(ValidationError::enumeration( |
| 105 | self.location.clone(), |
| 106 | location.into(), |
| 107 | instance, |
| 108 | &self.options, |
| 109 | )) |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | fn is_valid(&self, instance: &Value, _ctx: &mut ValidationContext) -> bool { |
| 114 | cmp::equal(&self.value, instance) |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | #[inline] |
| 119 | pub(crate) fn compile<'a>( |
| 120 | ctx: &compiler::Context, |
| 121 | _: &'a Map<String, Value>, |
| 122 | schema: &'a Value, |
| 123 | ) -> Option<CompilationResult<'a>> { |
| 124 | if let Value::Array(items) = schema { |
| 125 | let location = ctx.location().join("enum"); |
| 126 | if items.len() == 1 { |
| 127 | let value = items.iter().next().expect("Vec is not empty"); |
| 128 | Some(SingleValueEnumValidator::compile(schema, value, location)) |
| 129 | } else { |
| 130 | Some(EnumValidator::compile(schema, items, location)) |
| 131 | } |
| 132 | } else { |
| 133 | Some(Err(ValidationError::single_type_error( |
| 134 | Location::new(), |
| 135 | ctx.location().clone(), |
| 136 | schema, |
| 137 | JsonType::Array, |
| 138 | ))) |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | #[cfg(test)] |
| 143 | mod tests { |
| 144 | use crate::tests_util; |
| 145 | use serde_json::{json, Value}; |
| 146 | use test_case::test_case; |
| 147 | |
| 148 | #[test_case(&json!({"enum": [1]}), &json!(2), "/enum")] |
| 149 | #[test_case(&json!({"enum": [1, 3]}), &json!(2), "/enum")] |
| 150 | fn location(schema: &Value, instance: &Value, expected: &str) { |
| 151 | tests_util::assert_schema_location(schema, instance, expected); |
| 152 | } |
| 153 | } |