pub struct Document { /* private fields */ }Expand description
A request document, parsed for validation rather than for sending.
Validation works on the raw document, not on a typed Request, for two reasons. A typed parse
stops at the first problem and reports it without saying which question it is in; and any
ordinary parse silently keeps the last of two identical keys, so a repeated question id could
never be reported. A Document keeps the whole value whatever its shape, and remembers every
duplicate key it saw.
It implements Deserialize and does its work in a single pass, asking only that the format
be self-describing. Document::from_json_str covers JSON; a caller reading another such
format, YAML for instance, deserialises a Document with that format’s own deserializer.
use jev_client::validate::Document;
let document = Document::from_json_str(r#"{"state": "s", "state": "again"}"#).unwrap();
assert_eq!(document.value()["state"], "again");
assert_eq!(document.duplicate_keys().len(), 1);Implementations§
Source§impl Document
impl Document
Sourcepub fn from_json_str(text: &str) -> Result<Self, Error>
pub fn from_json_str(text: &str) -> Result<Self, Error>
Parses JSON text.
§Errors
Fails only when the text is not JSON at all. A well-formed document of the wrong shape parses, and validation then explains what is wrong with it.
Sourcepub const fn from_value(value: Value) -> Self
pub const fn from_value(value: Value) -> Self
Wraps a value that has already been parsed. Duplicate keys, if there were any, are already lost and cannot be reported.
Sourcepub const fn value_mut(&mut self) -> &mut Value
pub const fn value_mut(&mut self) -> &mut Value
Mutable access to the value, for a caller that fills in defaults (such as the model) before validating.
Sourcepub fn duplicate_keys(&self) -> &[DuplicateKey]
pub fn duplicate_keys(&self) -> &[DuplicateKey]
Every key that was repeated within one object, in document order.