pub struct ValidationError { /* private fields */ }Expand description
A schema constraint that a payload broke, and the path to the value that broke it.
Built innermost-first: the check that failed creates it with an empty
path, and each enclosing field or array index is prepended as the error
unwinds, so Display prints a JSON path a peer’s error
message can be matched against.
Implementations§
Source§impl ValidationError
impl ValidationError
Sourcepub fn new(kind: ValidationErrorKind) -> Self
pub fn new(kind: ValidationErrorKind) -> Self
Sourcepub fn kind(&self) -> ValidationErrorKind
pub fn kind(&self) -> ValidationErrorKind
What was wrong with the value.
Examples found in repository?
examples/validate.rs (line 36)
9fn main() {
10 let mut request: AuthorizeRequest = AuthorizeRequest {
11 certificate: None,
12 custom_data: None,
13 id_token: IdToken {
14 additional_info: None,
15 custom_data: None,
16 // Bounded at `maxLength: 36` by the schema, so this is a
17 // `heapless::String<36>` and an over-long value can't be built
18 // in the first place -- nothing for `validate` to check.
19 id_token: heapless::String::try_from("ABC123").unwrap(),
20 r#type: IdTokenEnum::ISO14443,
21 },
22 iso15118_certificate_hash_data: None,
23 };
24
25 println!("conformant: {:?}", request.validate());
26
27 // `certificate` is `maxLength: 5500` in the schema, but 5500 is too
28 // large to inline as a `heapless::String`, so with `alloc` it is a
29 // plain growable `String`. The type accepts this; the spec does not.
30 request.certificate = Some("-".repeat(6000));
31
32 match request.validate() {
33 Ok(()) => println!("no violations"),
34 Err(error) => {
35 println!("rejected: {error}");
36 println!(" kind: {:?}", error.kind());
37 println!(" path: {:?}", error.path());
38
39 // Which `CALLERROR` a CSMS answers with. The code's spelling is
40 // version-specific (1.6J dropped an `r` from "occurrence"), so
41 // the classification is what's shared.
42 let code = match error.kind().constraint_class() {
43 ConstraintClass::Property => "PropertyConstraintViolation",
44 ConstraintClass::Occurrence => "OccurrenceConstraintViolation",
45 };
46 println!(" answer: {code}");
47 }
48 }
49
50 // `minItems: 1` is the other half: no collection type can say "at least
51 // one", so an empty required array is only caught here.
52 request.certificate = None;
53 request.iso15118_certificate_hash_data = Some(heapless::Vec::new());
54
55 if let Err(error) = request.validate() {
56 println!("rejected: {error}");
57 }
58}Sourcepub fn path(&self) -> &[PathSegment]
pub fn path(&self) -> &[PathSegment]
The path from the validated payload’s root to the failing value, outermost segment first. Empty when the payload itself is what failed.
Examples found in repository?
examples/validate.rs (line 37)
9fn main() {
10 let mut request: AuthorizeRequest = AuthorizeRequest {
11 certificate: None,
12 custom_data: None,
13 id_token: IdToken {
14 additional_info: None,
15 custom_data: None,
16 // Bounded at `maxLength: 36` by the schema, so this is a
17 // `heapless::String<36>` and an over-long value can't be built
18 // in the first place -- nothing for `validate` to check.
19 id_token: heapless::String::try_from("ABC123").unwrap(),
20 r#type: IdTokenEnum::ISO14443,
21 },
22 iso15118_certificate_hash_data: None,
23 };
24
25 println!("conformant: {:?}", request.validate());
26
27 // `certificate` is `maxLength: 5500` in the schema, but 5500 is too
28 // large to inline as a `heapless::String`, so with `alloc` it is a
29 // plain growable `String`. The type accepts this; the spec does not.
30 request.certificate = Some("-".repeat(6000));
31
32 match request.validate() {
33 Ok(()) => println!("no violations"),
34 Err(error) => {
35 println!("rejected: {error}");
36 println!(" kind: {:?}", error.kind());
37 println!(" path: {:?}", error.path());
38
39 // Which `CALLERROR` a CSMS answers with. The code's spelling is
40 // version-specific (1.6J dropped an `r` from "occurrence"), so
41 // the classification is what's shared.
42 let code = match error.kind().constraint_class() {
43 ConstraintClass::Property => "PropertyConstraintViolation",
44 ConstraintClass::Occurrence => "OccurrenceConstraintViolation",
45 };
46 println!(" answer: {code}");
47 }
48 }
49
50 // `minItems: 1` is the other half: no collection type can say "at least
51 // one", so an empty required array is only caught here.
52 request.certificate = None;
53 request.iso15118_certificate_hash_data = Some(heapless::Vec::new());
54
55 if let Err(error) = request.validate() {
56 println!("rejected: {error}");
57 }
58}Sourcepub fn path_truncated(&self) -> bool
pub fn path_truncated(&self) -> bool
Whether the path lost its outermost segments to
MAX_PATH_DEPTH. The innermost ones – the part that says what
actually failed – are always kept.
Trait Implementations§
Source§impl Clone for ValidationError
impl Clone for ValidationError
Source§fn clone(&self) -> ValidationError
fn clone(&self) -> ValidationError
Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for ValidationError
impl Debug for ValidationError
Source§impl Display for ValidationError
impl Display for ValidationError
Source§impl Error for ValidationError
impl Error for ValidationError
1.30.0 · Source§fn source(&self) -> Option<&(dyn Error + 'static)>
fn source(&self) -> Option<&(dyn Error + 'static)>
Returns the lower-level source of this error, if any. Read more
1.0.0 · Source§fn description(&self) -> &str
fn description(&self) -> &str
👎Deprecated since 1.42.0:
use the Display impl or to_string()
Source§impl PartialEq for ValidationError
impl PartialEq for ValidationError
impl StructuralPartialEq for ValidationError
Auto Trait Implementations§
impl Freeze for ValidationError
impl RefUnwindSafe for ValidationError
impl Send for ValidationError
impl Sync for ValidationError
impl Unpin for ValidationError
impl UnsafeUnpin for ValidationError
impl UnwindSafe for ValidationError
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more