1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
use super::{AllowedType, Attribute};
use crate::definition::Type;
use crate::error::{Error, ValidationError};
use crate::validator::{Context, DocumentPath, State};
use std::collections::HashSet;
use serde_json;
#[derive(Debug)]
pub struct ConstValue {
name: String,
value: serde_json::Value,
}
impl ConstValue {
pub fn new(path: DocumentPath, ctx: &Context) -> Result<Self, Error> {
let obj = ctx.raw_definition();
match Type::new(obj, path.clone())? {
Type::Const => (),
typ => return Err(Error::ForbiddenType { path, typ }),
};
let value = match obj.get(ctx.name().as_str()) {
Some(value) => value.clone(),
None => {
return Err(Error::MissingAttribute {
path,
attr: ctx.name(),
})
}
};
Ok(ConstValue {
name: ctx.name(),
value,
})
}
pub fn allowed_types() -> HashSet<AllowedType> {
let mut set = HashSet::<AllowedType>::new();
set.insert(AllowedType::new(Type::Const, true));
set
}
pub fn build(
_: &mut State,
path: DocumentPath,
ctx: &Context,
) -> Result<Box<Attribute>, Error> {
Ok(Box::new(ConstValue::new(path, ctx)?))
}
}
impl Attribute for ConstValue {
fn validate(
&self,
_: &State,
path: Vec<String>,
input: &serde_json::Value,
) -> Result<(), ValidationError> {
if self.value != *input {
return Err(ValidationError::Failure {
rule: "value".to_string(),
path: path,
message: format!("Value must be {:?}.", self.value),
});
}
Ok(())
}
}