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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
mod error;
mod schema;
use either::Either;
use indexmap::IndexMap;
use schema::SchemaOrSchemaArray;
use std::{cell::RefCell, rc::Rc};
use jsona::{
dom::{KeyOrIndex, Keys, Node},
parser,
};
use serde::de::DeserializeOwned;
pub use error::Error;
pub use schema::Schema;
pub type Result<T> = std::result::Result<T, Error>;
pub fn from_str(value: &str) -> Result<Schema> {
let parse = parser::parse(value);
if !parse.errors.is_empty() {
return Err(Error::invalid_file(Keys::default()));
}
let node = parse.into_dom();
from_node(&node)
}
pub fn from_node(node: &Node) -> Result<Schema> {
if node.validate().is_err() {
return Err(Error::invalid_file(Keys::default()));
}
let scope = Scope {
node: node.clone(),
keys: Default::default(),
defs: Default::default(),
};
let mut schema = parse_node(scope.clone())?;
let defs = scope.defs.take();
if !defs.is_empty() {
schema.defs = Some(defs);
}
Ok(schema)
}
#[derive(Debug, Clone)]
struct Scope {
node: Node,
keys: Keys,
defs: Rc<RefCell<IndexMap<String, Schema>>>,
}
impl Scope {
fn spawn(&self, key: KeyOrIndex, node: Node) -> Self {
Self {
node,
keys: self.keys.clone().join(key),
defs: self.defs.clone(),
}
}
}
fn parse_node(scope: Scope) -> Result<Schema> {
let mut def_value = String::new();
if let Some(def) = parse_str_annotation(&scope, "@def")? {
let mut defs = scope.defs.borrow_mut();
if defs.contains_key(&def) {
return Err(Error::conflict_def(scope.keys.clone(), &def));
}
defs.insert(def.clone(), Default::default());
def_value = def;
} else if let Some(ref_value) = parse_str_annotation(&scope, "@ref")? {
let defs = scope.defs.borrow();
if !defs.contains_key(&ref_value) {
return Err(Error::unknown_def(scope.keys.clone(), &ref_value));
}
return Ok(Schema {
ref_value: Some(format!("#/$defs/{}", ref_value)),
..Default::default()
});
}
let mut schema: Schema = parse_object_annotation(&scope, "@schema")?.unwrap_or_default();
set_node_type(&scope, &mut schema);
if let Some(describe) = parse_str_annotation(&scope, "@describe")? {
schema.description = Some(describe);
}
if exist_annotation(&scope, "@example") {
schema.examples = Some(vec![scope.node.to_plain_json()])
}
let schema_type = schema.schema_type.as_ref().unwrap();
if schema_type == "object" {
if let Node::Object(obj) = &scope.node {
for (key, child) in obj.value().read().kv_iter() {
let child_scope = scope.spawn(key.into(), child.clone());
let key = key.value();
let pattern = parse_str_annotation(&child_scope, "@pattern")?;
let child_schema = parse_node(child_scope.clone())?;
if let Some(pattern) = pattern {
let props = schema.pattern_properties.get_or_insert(Default::default());
if props.contains_key(key) {
return Err(Error::conflict_pattern(
child_scope.keys.join(KeyOrIndex::annotation("@pattern")),
&pattern,
));
}
props.insert(pattern, child_schema);
} else {
let required = exist_annotation(&child_scope, "@required");
let props = schema.properties.get_or_insert(Default::default());
props.insert(key.to_string(), child_schema);
if required {
schema
.required
.get_or_insert(Default::default())
.push(key.to_string());
}
}
}
} else {
return Err(Error::mismatch_type(scope.keys.clone()));
}
} else if schema_type == "array" && scope.node.is_array() {
if let Node::Array(arr) = &scope.node {
let arr = arr.value().read();
if arr.len() > 0 && schema.items.is_none() {
let compound = parse_str_annotation(&scope, "@compound")?;
match compound {
Some(compound) => {
schema.schema_type = None;
let mut schemas = vec![];
for (i, child) in arr.iter().enumerate() {
let child_scope = scope.spawn(i.into(), child.clone());
schemas.push(parse_node(child_scope)?);
}
match compound.as_str() {
"anyOf" => schema.any_of = Some(schemas),
"oneOf" => schema.one_of = Some(schemas),
"allOf" => schema.all_of = Some(schemas),
_ => {
return Err(Error::invalid_value(
scope.keys.join(KeyOrIndex::annotation("@compound")),
));
}
}
}
None => {
if arr.len() == 1 {
let child_scope = scope.spawn(0_usize.into(), arr[0].clone());
schema.items = Some(SchemaOrSchemaArray {
value: Either::Left(parse_node(child_scope)?.into()),
})
} else {
let mut schemas = vec![];
for (i, child) in arr.iter().enumerate() {
let child_scope = scope.spawn(i.into(), child.clone());
schemas.push(parse_node(child_scope)?);
}
schema.items = Some(SchemaOrSchemaArray {
value: Either::Right(schemas),
})
}
}
}
}
} else {
return Err(Error::mismatch_type(scope.keys.clone()));
}
} else if schema_type == "any" {
schema.schema_type = None;
}
if !def_value.is_empty() {
scope.defs.borrow_mut().insert(def_value.clone(), schema);
return Ok(Schema {
ref_value: Some(format!("#/$defs/{}", def_value)),
..Default::default()
});
}
Ok(schema)
}
fn exist_annotation(scope: &Scope, name: &str) -> bool {
let key = KeyOrIndex::annotation(name);
scope.node.get(&key).is_some()
}
fn parse_object_annotation<T: DeserializeOwned>(scope: &Scope, name: &str) -> Result<Option<T>> {
let key = KeyOrIndex::annotation(name);
let value = scope
.node
.try_get_as_object(&key)
.map_err(|_| Error::mismatch_type(scope.keys.clone().join(key.clone())))?;
if let Some(value) = value {
let value: Node = value.into();
let value = value.to_plain_json();
let schema = serde_json::from_value(value)
.map_err(|_| Error::invalid_value(scope.keys.clone().join(key)))?;
Ok(Some(schema))
} else {
Ok(None)
}
}
fn parse_str_annotation(scope: &Scope, name: &str) -> Result<Option<String>> {
let key = KeyOrIndex::annotation(name);
let value = scope
.node
.try_get_as_string(&key)
.map_err(|_| Error::mismatch_type(scope.keys.clone().join(key)))?;
Ok(value.map(|v| v.value().to_string()))
}
fn set_node_type(scope: &Scope, schema: &mut Schema) {
if schema.schema_type.is_none() {
let value = match &scope.node {
Node::Null(_) => "null",
Node::Bool(_) => "boolean",
Node::Number(_) => "number",
Node::String(_) => "string",
Node::Array(_) => "array",
Node::Object(_) => "object",
};
schema.schema_type = Some(value.to_string());
}
}