Skip to main content

Violation

Struct Violation 

Source
pub struct Violation { /* private fields */ }
Expand description

One rule violation without the rejected value.

§Examples

use qubit_validator::{ValidatorId, Violation, ViolationCode};

let violation = Violation::new(
    ValidatorId::new("text.required"),
    ViolationCode::new("text.blank"),
);
assert_eq!(violation.code().as_str(), "text.blank");

Implementations§

Source§

impl Violation

Source

pub fn new(rule_id: ValidatorId, code: ViolationCode) -> Self

Creates a safe violation at the root path.

§Parameters
  • rule_id: Stable identifier of the rule that rejected the value.
  • code: Program-declared code describing the violation.
§Returns

A violation with an empty path and no parameters.

Source

pub const fn rule_id(&self) -> ValidatorId

Returns the stable rule identifier.

§Returns

The identifier assigned by the bound validator.

Source

pub const fn code(&self) -> ViolationCode

Returns the stable violation code.

§Returns

The program-declared code for this violation.

Examples found in repository?
examples/local_registry.rs (line 170)
150fn main() -> Result<(), Box<dyn Error>> {
151    let registration = ValidatorRegistration::new(
152        ValidatorId::new("text.non_blank"),
153        &DESCRIPTOR,
154        RegistrationSource::new(env!("CARGO_PKG_NAME"), module_path!(), file!(), line!()),
155    );
156    let registry = ValidatorRegistry::from_registrations([registration])?;
157
158    assert!(registry.get("text.non_blank").is_some());
159    let validator = registry.bind("text.non_blank", InputType::Text, &[])?;
160    let context = BoundValidationContext::new(&[]);
161
162    let valid = validator.validate(ValidationValue::Text("Ada"), &context)?;
163    assert_eq!(valid, ValidationOutcome::Valid);
164
165    let invalid = validator.validate(ValidationValue::Text("   "), &context)?;
166    let ValidationOutcome::Invalid(violations) = invalid else {
167        panic!("blank text must be invalid");
168    };
169    assert_eq!(violations.len(), 1);
170    assert_eq!(violations[0].code(), ViolationCode::new("text.blank"));
171
172    assert_dependency_specs_come_from_signature()?;
173    assert_parameters_are_consumed_once()?;
174
175    #[cfg(feature = "inventory")]
176    {
177        let global = ValidatorRegistry::try_global()?;
178        assert!(global.get("text.non_blank.global").is_some());
179    }
180
181    Ok(())
182}
Source

pub const fn path(&self) -> &ValidationPath

Returns the structured path.

The path contains no raw rejected value or map key.

§Returns

The structured path to the rejected value.

Source

pub const fn params(&self) -> &BTreeMap<&'static str, ViolationParam>

Returns structured parameters supplied by the caller.

The type restricts their representation but does not prove provenance; callers remain responsible for keeping rejected input and sensitive values derived from it out of this collection.

§Returns

The named parameters associated with this violation.

Source

pub fn with_path(self, path: ValidationPath) -> Self

Replaces the structured location of the rejected value.

§Parameters
  • path: Path containing only declared fields and opaque positions.
§Returns

The violation with path stored in place of its previous path.

Source

pub fn with_param(self, name: &'static str, value: ViolationParam) -> Self

Adds or replaces a structured parameter by its declared name.

The type restricts representation but cannot prove provenance. Supply only program-declared tokens or rule configuration values, never rejected input or sensitive values derived from it.

§Parameters
  • name: Static program-declared parameter name.
  • value: Scalar or static token associated with the name, subject to the caller’s provenance and redaction responsibility.
§Returns

The violation with the named parameter inserted or replaced.

Trait Implementations§

Source§

impl Clone for Violation

Source§

fn clone(&self) -> Self

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 Violation

Source§

fn fmt(&self, formatter: &mut Formatter<'_>) -> FmtResult

Formats stable identifiers without exposing path labels or parameters.

§Parameters
  • formatter: Destination and formatting options for the debug output.
Source§

impl Display for Violation

Source§

fn fmt(&self, formatter: &mut Formatter<'_>) -> FmtResult

Formats only the stable violation code.

§Parameters
  • formatter: Destination and formatting options for the display output.
Source§

impl Eq for Violation

Source§

impl PartialEq for Violation

Source§

fn eq(&self, other: &Self) -> 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 Violation

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 = !

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

fn try_from(value: U) -> Result<T, !>

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.