pub enum JsonApiDocument {
Error(DocumentError),
Data(DocumentData),
}
Expand description
An enum that defines the possible composition of a JSON:API document - one which contains error
or
data
- but not both. Rely on Rust’s type system to handle this basic validation instead of
running validators on parsed documents
Variants§
Error(DocumentError)
Data(DocumentData)
Implementations§
Source§impl JsonApiDocument
Top-level JSON-API Document
An “error” document can be valid, just as a “data” document can be valid
impl JsonApiDocument
Top-level JSON-API Document An “error” document can be valid, just as a “data” document can be valid
Sourcepub fn is_valid(&self) -> bool
pub fn is_valid(&self) -> bool
This function returns false
if the JsonApiDocument
contains any violations of the
specification. See DocumentValidationError
The spec dictates that the document must have least one of data
, errors
or meta
.
Of these, data
and errors
must not co-exist.
The optional field included
may only be present if the data
field is present too.
Sourcepub fn validate(&self) -> Option<Vec<DocumentValidationError>>
pub fn validate(&self) -> Option<Vec<DocumentValidationError>>
This function returns a Vec
with identified specification violations enumerated in
DocumentValidationError
// Simulate an error where `included` has data but `data` does not
use jsonapi::api::*;
use std::str::FromStr;
let serialized = r#"{
"id":"1",
"type":"post",
"attributes":{
"title": "Rails is Omakase",
"likes": 250
},
"relationships":{},
"links" :{}
}"#;
let resource = Resource::from_str(&serialized);
let data = DocumentData {
data: None,
included: Some(vec![resource.unwrap()]),
..Default::default()
};
let doc = JsonApiDocument::Data(data);
match doc.validate() {
Some(errors) => {
assert!(
errors.contains(
&DocumentValidationError::IncludedWithoutData
)
)
}
None => assert!(false)
}
Trait Implementations§
Source§impl Clone for JsonApiDocument
impl Clone for JsonApiDocument
Source§fn clone(&self) -> JsonApiDocument
fn clone(&self) -> JsonApiDocument
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl Debug for JsonApiDocument
impl Debug for JsonApiDocument
Source§impl<'de> Deserialize<'de> for JsonApiDocument
impl<'de> Deserialize<'de> for JsonApiDocument
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl FromStr for JsonApiDocument
impl FromStr for JsonApiDocument
Source§fn from_str(s: &str) -> Result<Self>
fn from_str(s: &str) -> Result<Self>
Instantiate from string
use jsonapi::api::JsonApiDocument;
use std::str::FromStr;
let serialized = r#"{
"data" : [
{ "id":"1", "type":"post", "attributes":{}, "relationships":{}, "links" :{} },
{ "id":"2", "type":"post", "attributes":{}, "relationships":{}, "links" :{} },
{ "id":"3", "type":"post", "attributes":{}, "relationships":{}, "links" :{} }
]
}"#;
let doc = JsonApiDocument::from_str(&serialized);
assert_eq!(doc.is_ok(), true);