Skip to main content

ValidationError

Struct ValidationError 

Source
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

Source

pub fn new(kind: ValidationErrorKind) -> Self

A violation at the root, with no path yet. Prepend context with in_field / in_index.

Source

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}
Source

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}
Source

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.

Source

pub fn in_field(self, name: &'static str) -> Self

Records that this violation was found inside property name (its wire name, camelCase).

Source

pub fn in_index(self, index: usize) -> Self

Records that this violation was found at index index of the array currently outermost in the path.

Trait Implementations§

Source§

impl Clone for ValidationError

Source§

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)

Performs copy-assignment from source. Read more
Source§

impl Debug for ValidationError

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for ValidationError

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Error for ValidationError

1.30.0 · Source§

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

👎Deprecated since 1.42.0:

use the Display impl or to_string()

1.0.0 · Source§

fn cause(&self) -> Option<&dyn Error>

👎Deprecated since 1.33.0:

replaced by Error::source, which can support downcasting

Source§

fn provide<'a>(&'a self, request: &mut Request<'a>)

🔬This is a nightly-only experimental API. (error_generic_member_access)
Provides type-based access to context intended for error reports. Read more
Source§

impl PartialEq for ValidationError

Source§

fn eq(&self, other: &ValidationError) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for ValidationError

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.