Trait Validator

Source
pub trait Validator<'a> {
    type Item;

    // Required method
    fn validate<L: Logger>(&self, item: &'a Self::Item, l: &mut L) -> bool;

    // Provided method
    fn chain<V2>(self, v2: V2) -> ChainValidator<'a, Self, V2, Self::Item>
       where Self: Sized,
             V2: Validator<'a, Item = Self::Item> { ... }
}
Available on crate feature validate only.
Expand description

Trait for build item validators (for ex. validate X.509 structure)

See X509StructureValidator for a default implementation, validating the DER structure of a X.509 Certificate.

See implementors of the Logger trait for methods to collect or handle warnings and errors.

§Examples

Collecting warnings and errors to Vec:

use x509_parser::certificate::X509Certificate;
use x509_parser::validate::*;

#[cfg(feature = "validate")]
fn validate_certificate(x509: &X509Certificate<'_>) -> Result<(), &'static str> {
    let mut logger = VecLogger::default();
    println!("  Subject: {}", x509.subject());
    // validate and print warnings and errors to stderr
    let ok = X509StructureValidator.validate(&x509, &mut logger);
    print!("Structure validation status: ");
    if ok {
        println!("Ok");
    } else {
        println!("FAIL");
    }
    for warning in logger.warnings() {
        eprintln!("  [W] {}", warning);
    }
    for error in logger.errors() {
        eprintln!("  [E] {}", error);
    }
    println!();
    if !logger.errors().is_empty() {
        return Err("validation failed");
    }
    Ok(())
}

Required Associated Types§

Source

type Item

The item to validate

Required Methods§

Source

fn validate<L: Logger>(&self, item: &'a Self::Item, l: &mut L) -> bool

Attempts to validate current item.

Returns true if item was validated.

Call l.warn() if a non-fatal error was encountered, and l.err() if the error is fatal. These functions receive a description of the error.

Provided Methods§

Source

fn chain<V2>(self, v2: V2) -> ChainValidator<'a, Self, V2, Self::Item>
where Self: Sized, V2: Validator<'a, Item = Self::Item>,

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§