1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
use crate::json::JsValue; use super::SchemaNode; impl SchemaNode { pub fn for_literal(literal_value: &JsValue) -> SchemaNode { match *literal_value { JsValue::Null => SchemaNode::null().into(), JsValue::Bool(_) => SchemaNode::boolean().into(), JsValue::Number(_) => SchemaNode::number().into(), JsValue::String(_) => SchemaNode::string().into(), JsValue::Array(_) => SchemaNode::array(SchemaNode::any()).into(), JsValue::Object(ref props) => { props.into_iter().fold( SchemaNode::object(), |acc, (key, value)| { acc.add_property(key, Self::for_literal(value)).add_required(key) } ).into() }, } } }