Skip to content

Commit 3d9fd18

Browse files
Zantierrlouf
authored andcommitted
parse objects and arrays in const, and support whitespace
1 parent b9fb25f commit 3d9fd18

File tree

2 files changed

+43
-28
lines changed

2 files changed

+43
-28
lines changed

src/json_schema/mod.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -583,19 +583,19 @@ mod tests {
583583
// Enum array
584584
(
585585
r#"{"title": "Foo", "enum": [[1,2],[3,4]], "type": "array"}"#,
586-
r#"(\[1,2\]|\[3,4\])"#,
587-
vec!["[1,2]", "[3,4]"], vec!["1", "[1,3]"],
586+
format!(r#"(\[{0}1{0},{0}2{0}\]|\[{0}3{0},{0}4{0}\])"#, WHITESPACE).as_str(),
587+
vec!["[1,2]", "[3,4]", "[1, 2 ]"], vec!["1", "[1,3]"],
588588
),
589589
// Enum object
590590
(
591-
r#"{"title": "Foo", "enum": [{"a":"b"}, {"c":"d"}], "type": "object"}"#,
592-
r#"(\{"a":"b"\}|\{"c":"d"\})"#,
593-
vec![r#"{"a":"b"}"#, r#"{"c":"d"}"#], vec!["a", r#"{"a":"c"}"#],
591+
r#"{"title": "Foo", "enum": [{"a":"b","c":"d"}, {"e":"f"}], "type": "object"}"#,
592+
format!(r#"(\{{{0}"a"{0}:{0}"b"{0},{0}"c"{0}:{0}"d"{0}\}}|\{{{0}"e"{0}:{0}"f"{0}\}})"#, WHITESPACE).as_str(),
593+
vec![r#"{"a":"b","c":"d"}"#, r#"{"e":"f"}"#, r#"{"a" : "b", "c": "d" }"#], vec!["a", r#"{"a":"b"}"#],
594594
),
595595
// Enum mix of types
596596
(
597597
r#"{"title": "Foo", "enum": [6, 5.3, "potato", true, null, [1,2], {"a":"b"}]}"#,
598-
r#"(6|5\.3|"potato"|true|null|\[1,2\]|\{"a":"b"\})"#,
598+
format!(r#"(6|5\.3|"potato"|true|null|\[{0}1{0},{0}2{0}\]|\{{{0}"a"{0}:{0}"b"{0}\}})"#, WHITESPACE).as_str(),
599599
vec!["6", "5.3", r#""potato""#, "true", "null"], vec!["none", "53"],
600600
),
601601
// ==========================================================

src/json_schema/parsing.rs

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -236,17 +236,7 @@ impl<'a> Parser<'a> {
236236
Some(Value::Array(enum_values)) => {
237237
let choices: Result<Vec<String>> = enum_values
238238
.iter()
239-
.map(|choice| match choice {
240-
Value::Null
241-
| Value::Bool(_)
242-
| Value::Number(_)
243-
| Value::String(_)
244-
| Value::Array(_)
245-
| Value::Object(_) => {
246-
let json_string = serde_json::to_string(choice)?;
247-
Ok(regex::escape(&json_string))
248-
}
249-
})
239+
.map(|choice| self.parse_const_value(choice))
250240
.collect();
251241

252242
let choices = choices?;
@@ -257,17 +247,42 @@ impl<'a> Parser<'a> {
257247
}
258248

259249
fn parse_const(&mut self, obj: &serde_json::Map<String, Value>) -> Result<String> {
260-
match obj.get("const") {
261-
Some(const_value) => match const_value {
262-
Value::Null | Value::Bool(_) | Value::Number(_) | Value::String(_) => {
263-
let json_string = serde_json::to_string(const_value)?;
264-
Ok(regex::escape(&json_string))
265-
}
266-
_ => Err(Error::UnsupportedConstDataType(Box::new(
267-
const_value.clone(),
268-
))),
269-
},
270-
None => Err(Error::ConstKeyNotFound),
250+
if let Some(const_value) = obj.get("const") {
251+
self.parse_const_value(const_value)
252+
} else {
253+
Err(Error::ConstKeyNotFound)
254+
}
255+
}
256+
257+
fn parse_const_value(&self, value: &Value) -> Result<String> {
258+
match value {
259+
Value::Array(array_values) => {
260+
let inner_regex = array_values
261+
.iter()
262+
.map(|value| self.parse_const_value(value))
263+
.collect::<Result<Vec<String>>>()?
264+
.join(format!("{0},{0}", self.whitespace_pattern).as_str());
265+
Ok(format!(r"\[{0}{inner_regex}{0}\]", self.whitespace_pattern))
266+
}
267+
Value::Object(obj) => {
268+
let inner_regex = obj
269+
.iter()
270+
.map(|(key, value)| {
271+
self.parse_const_value(value).map(|value_regex| {
272+
format!(r#""{key}"{0}:{0}{value_regex}"#, self.whitespace_pattern)
273+
})
274+
})
275+
.collect::<Result<Vec<String>>>()?
276+
.join(format!("{0},{0}", self.whitespace_pattern).as_str());
277+
Ok(format!(
278+
r"\{{{0}{inner_regex}{0}\}}",
279+
self.whitespace_pattern
280+
))
281+
}
282+
_ => {
283+
let json_string = serde_json::to_string(value)?;
284+
Ok(regex::escape(&json_string))
285+
}
271286
}
272287
}
273288

0 commit comments

Comments
 (0)