struct_validation_core

Struct ValidationError

Source
pub struct ValidationError {
    pub field: String,
    pub message: String,
}
Expand description

Represents an error that occurs during validation of a struct’s field.

Each ValidationError contains the name of the field that failed validation and an associated error message explaining the reason for the failure.

Fields§

§field: String

The name of the field that failed validation.

§message: String

A message describing the validation error.

Implementations§

Source§

impl ValidationError

Source

pub fn new(field: &str, message: &str) -> Self

Creates a new ValidationError for a specific field with a given message.

§Arguments
  • field - The name of the field that failed validation.
  • message - A description of the validation error.
§Examples
use struct_validation_core::ValidationError;

let error = ValidationError::new("username", "must not be empty");
assert_eq!(error.field, "username");
assert_eq!(error.message, "must not be empty");
Source

pub fn add_prefix(&mut self, prefix: &str)

Adds a prefix to the field name, separated by a dot.

This can be useful for nested validation errors where the field is part of a larger struct.

§Arguments
  • prefix - The prefix to add to the field name.
§Examples
use struct_validation_core::ValidationError;

let mut error = ValidationError::new("username", "must not be empty");
error.add_prefix("user");
assert_eq!(error.field, "user.username");

Trait Implementations§

Source§

impl Debug for ValidationError

Source§

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

Formats the value using the given formatter. Read more

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> 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, 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.