pub fn coerce(property: &Value, raw: &str) -> Result<Value, String>Expand description
Convert a query-string value to the JSON type a property schema names: integers and booleans are parsed, a structured parameter is read as the JSON text the binding sent, and everything else stays a string.
The array and object cases are the exact inverse of
Request::bind, which puts a non-scalar on the query
string as JSON. Without them a GET route could be sent a list and could not read
one, which is a projection that only works in one direction — and the generic test that
replays every capability’s benchmark cases over the wire is where that showed.
use majordomus_cli::capability::schema::coerce;
use serde_json::json;
assert_eq!(coerce(&json!({ "type": "integer" }), "7").unwrap(), json!(7));
assert_eq!(coerce(&json!({ "type": "string" }), "7").unwrap(), json!("7"));
assert!(coerce(&json!({ "type": "boolean" }), "yes").is_err());
// a list parameter arrives as the JSON the binding wrote, nullable or not
let list = json!({ "type": ["array", "null"], "items": { "type": "string" } });
assert_eq!(coerce(&list, r#"["a","b"]"#).unwrap(), json!(["a", "b"]));
// and a value that is not that list is refused rather than passed on as a string
assert!(coerce(&list, "a,b").is_err());