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
impl Violation
Sourcepub fn new(rule_id: ValidatorId, code: ViolationCode) -> Self
pub fn new(rule_id: ValidatorId, code: ViolationCode) -> Self
Sourcepub const fn rule_id(&self) -> ValidatorId
pub const fn rule_id(&self) -> ValidatorId
Sourcepub const fn code(&self) -> ViolationCode
pub const fn code(&self) -> ViolationCode
Examples found in repository?
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}Sourcepub const fn path(&self) -> &ValidationPath
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.
Sourcepub const fn params(&self) -> &BTreeMap<&'static str, ViolationParam>
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.
Sourcepub fn with_path(self, path: ValidationPath) -> Self
pub fn with_path(self, path: ValidationPath) -> Self
Sourcepub fn with_param(self, name: &'static str, value: ViolationParam) -> Self
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.