use serde_json::{Map, Value};
const OBJECT_SCHEMA_KEYWORDS: &[&str] = &[
"$schema",
"$id",
"$defs",
"definitions",
"title",
"description",
"required",
"additionalProperties",
"patternProperties",
"propertyNames",
"minProperties",
"maxProperties",
"unevaluatedProperties",
"dependentRequired",
"dependentSchemas",
"allOf",
"anyOf",
"oneOf",
"not",
"if",
"then",
"else",
"examples",
];
pub(super) fn is_full_object_schema(schema: &Map<String, Value>) -> bool {
if !schema.contains_key("type") {
return false;
}
schema.contains_key("properties")
|| (schema.get("type").and_then(Value::as_str) == Some("object")
&& schema
.keys()
.all(|key| key == "type" || OBJECT_SCHEMA_KEYWORDS.contains(&key.as_str())))
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
fn is_full(schema: Value) -> bool {
is_full_object_schema(schema.as_object().expect("test schema is an object"))
}
#[test]
fn type_with_properties_is_full_whatever_the_type_says() {
assert!(is_full(json!({"type": "object", "properties": {}})));
assert!(is_full(json!({"type": "string", "properties": "x"})));
}
#[test]
fn an_object_type_with_only_keywords_is_full() {
assert!(is_full(json!({"type": "object"})));
for keyword in OBJECT_SCHEMA_KEYWORDS {
assert!(
is_full(json!({"type": "object", *keyword: "x"})),
"{keyword}"
);
}
}
#[test]
fn a_property_map_is_not_full() {
assert!(!is_full(json!({})));
assert!(!is_full(json!({"query": "string"})));
assert!(!is_full(json!({"type": "string"})));
assert!(!is_full(json!({"type": "string", "title": "string"})));
assert!(!is_full(json!({"type": "object", "query": "string"})));
assert!(!is_full(
json!({"type": {"type": "string"}, "title": "string"})
));
assert!(!is_full(
json!({"type": "object", "title": "string", "query": "string"})
));
}
#[test]
fn a_map_without_type_is_never_full() {
assert!(!is_full(json!({"required": ["a"], "title": "T"})));
assert!(!is_full(json!({"properties": {"type": "string"}})));
}
}