use serde_json::Value;
fn named(action: &Value) -> impl Iterator<Item = &str> {
let listed = action.get("enum").and_then(Value::as_array);
action
.get("const")
.into_iter()
.chain(listed.into_iter().flatten())
.filter_map(Value::as_str)
}
fn action_of(schema: &Value) -> Option<&Value> {
schema.get("properties")?.get("action")
}
fn names_any(schema: &Value, refused: &[&str]) -> bool {
match schema {
Value::Object(object) => {
action_of(schema).is_some_and(|action| named(action).any(|id| refused.contains(&id)))
|| object.values().any(|value| names_any(value, refused))
}
Value::Array(values) => values.iter().any(|value| names_any(value, refused)),
_ => false,
}
}
fn names_none(schema: &Value) -> bool {
action_of(schema)
.and_then(|action| action.get("enum"))
.and_then(Value::as_array)
.is_some_and(Vec::is_empty)
}
fn take_named(schema: &mut Value, refused: &[&str]) {
match schema {
Value::Object(object) => {
if let Some(action) = object
.get_mut("properties")
.and_then(|properties| properties.get_mut("action"))
.and_then(Value::as_object_mut)
{
let is_refused = |id: &Value| id.as_str().is_some_and(|id| refused.contains(&id));
if action.get("const").is_some_and(is_refused) {
action.remove("const");
action.insert("enum".to_owned(), Value::Array(Vec::new()));
} else if let Some(Value::Array(ids)) = action.get_mut("enum") {
ids.retain(|id| !is_refused(id));
}
}
for (key, value) in object.iter_mut() {
take_named(value, refused);
if let ("oneOf" | "anyOf", Value::Array(alternatives)) = (key.as_str(), value) {
alternatives.retain(|alternative| !names_none(alternative));
}
}
}
Value::Array(values) => {
for value in values {
take_named(value, refused);
}
}
_ => {}
}
}
pub(crate) fn without_named(schema: &Value, refused: &[&str]) -> Option<Value> {
if !names_any(schema, refused) {
return None;
}
let mut cut = schema.clone();
take_named(&mut cut, refused);
Some(cut)
}
#[cfg(test)]
mod tests {
use super::without_named;
use serde_json::json;
fn typed_edit() -> serde_json::Value {
json!({"type": "object", "required": ["op"], "properties": {"op": {"oneOf": [
{
"type": "object",
"required": ["action"],
"properties": {"action": {"const": "retitle"}, "title": {"type": "string"}},
},
{
"type": "object",
"required": ["action", "args"],
"properties": {
"action": {"type": "string", "enum": ["post", "pin", "unpin"]},
"args": {"type": "object"},
},
"additionalProperties": false,
},
]}}})
}
#[test]
fn a_carrier_names_no_action_its_caller_is_refused_and_everything_else_as_before() {
let schema = typed_edit();
assert_eq!(without_named(&schema, &[]), None);
assert_eq!(
without_named(&schema, &["archive"]),
None,
"it names none of them: the schema it shares stays"
);
let cut = without_named(&schema, &["pin", "unpin"]).expect("it names both");
let mut expected = typed_edit();
expected["properties"]["op"]["oneOf"][1]["properties"]["action"]["enum"] = json!(["post"]);
assert_eq!(
cut, expected,
"the two ids are gone and nothing else changed"
);
let cut = without_named(&schema, &["retitle", "pin"]).expect("it names both");
assert_eq!(
cut["properties"]["op"]["oneOf"],
json!([{
"type": "object",
"required": ["action", "args"],
"properties": {
"action": {"type": "string", "enum": ["post", "unpin"]},
"args": {"type": "object"},
},
"additionalProperties": false,
}])
);
let group = json!({"type": "array", "items": {"oneOf": [
{"type": "object", "properties": {
"action": {"type": "string", "enum": ["post", "pin"]},
"args": {"type": "object"},
}},
{"type": "object", "properties": {
"action": {"const": "apply"},
"args": {"type": "object"},
}},
]}});
let cut = without_named(&group, &["pin"]).expect("it names it");
assert_eq!(
cut["items"]["oneOf"][0]["properties"]["action"]["enum"],
json!(["post"])
);
assert_eq!(cut["items"]["oneOf"][1], group["items"]["oneOf"][1]);
}
}