pub struct Validator { /* private fields */ }Expand description
Main data validator with comprehensive validation capabilities
Implementations§
Source§impl Validator
impl Validator
Sourcepub fn new(config: ValidationConfig) -> Result<Self, CoreError>
pub fn new(config: ValidationConfig) -> Result<Self, CoreError>
Create a new validator with configuration
§Arguments
config- Validation configuration settings
§Returns
A new Validator instance or an error if initialization fails
§Example
use scirs2_core::validation::data::{Validator, ValidationConfig};
let mut config = ValidationConfig::default();
config.max_depth = 10;
config.strict_mode = true;
let validator = Validator::new(config)?;Sourcepub fn validate(
&self,
data: &JsonValue,
schema: &ValidationSchema,
) -> Result<ValidationResult, CoreError>
pub fn validate( &self, data: &JsonValue, schema: &ValidationSchema, ) -> Result<ValidationResult, CoreError>
Validate JSON data against a schema
This method performs comprehensive validation of JSON data against a predefined schema, including type checking, constraint validation, and custom rules.
§Arguments
data- The JSON data to validateschema- The validation schema to apply
§Returns
A ValidationResult containing the validation outcome and any errors/warnings
§Example
use scirs2_core::validation::data::{Validator, ValidationSchema, DataType, Constraint, ValidationConfig};
let validator = Validator::new(ValidationConfig::default())?;
let schema = ValidationSchema::new()
.require_field("email", DataType::String)
.add_constraint("email", Constraint::Pattern("^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$".to_string()));
let data = serde_json::json!({
"email": "user@example.com"
});
let result = validator.validate(&data, &schema)?;
assert!(result.is_valid());Sourcepub fn validate_ndarray<S, D>(
&self,
array: &ArrayBase<S, D>,
constraints: &ArrayValidationConstraints,
config: &ValidationConfig,
) -> Result<ValidationResult, CoreError>
pub fn validate_ndarray<S, D>( &self, array: &ArrayBase<S, D>, constraints: &ArrayValidationConstraints, config: &ValidationConfig, ) -> Result<ValidationResult, CoreError>
Validate ndarray with comprehensive checks
Performs validation on scientific arrays including shape validation, numeric quality checks, statistical constraints, and performance characteristics.
§Arguments
array- The ndarray to validateconstraints- Validation constraints to applyconfig- Validation configuration
§Type Parameters
S- Storage type (must implementData)D- Dimension typeS::Elem- Element type (must be a floating-point type)
§Returns
A ValidationResult with detailed validation information
§Example
use scirs2_core::validation::data::{Validator, ValidationConfig, ArrayValidationConstraints};
use ::ndarray::Array2;
let validator = Validator::new(ValidationConfig::default())?;
let data = Array2::from_shape_vec((3, 3), vec![
1.0, 2.0, 3.0,
4.0, 5.0, 6.0,
7.0, 8.0, 9.0
])?;
let constraints = ArrayValidationConstraints::new()
.withshape(vec![3, 3])
.check_numeric_quality();
let result = validator.validate_ndarray(&data, &constraints, &ValidationConfig::default())?;
assert!(result.is_valid());Sourcepub fn generate_quality_report<S, D>(
&self,
array: &ArrayBase<S, D>,
fieldname: &str,
) -> Result<DataQualityReport, CoreError>
pub fn generate_quality_report<S, D>( &self, array: &ArrayBase<S, D>, fieldname: &str, ) -> Result<DataQualityReport, CoreError>
Generate comprehensive data quality report
Analyzes an array and generates a detailed quality report including completeness, accuracy, consistency, and statistical properties.
§Arguments
array- The array to analyzefieldname- Name of the field for reporting
§Returns
A DataQualityReport with quality metrics and recommendations
§Example
use scirs2_core::validation::data::{Validator, ValidationConfig};
use ::ndarray::Array1;
let validator = Validator::new(ValidationConfig::default())?;
let data = Array1::from_vec(vec![1.0, 2.0, 3.0, 4.0, 5.0]);
let report = validator.generate_quality_report(&data, "measurements")?;
println!("Quality score: {}", report.quality_score);
println!("Completeness: {}", report.metrics.completeness);Sourcepub fn add_custom_rule(
&mut self,
name: String,
rule: Box<dyn ValidationRule + Send + Sync>,
)
pub fn add_custom_rule( &mut self, name: String, rule: Box<dyn ValidationRule + Send + Sync>, )
Add a custom validation rule
Registers a custom validation rule that can be referenced in schemas.
§Arguments
name- Unique name for the rulerule- The validation rule implementation
§Example
use scirs2_core::validation::data::{Validator, ValidationConfig, ValidationRule};
use serde_json::Value as JsonValue;
struct EmailRule;
impl ValidationRule for EmailRule {
fn validate(&self, value: &JsonValue, fieldpath: &str) -> Result<(), String> {
if let Some(email) = value.as_str() {
if email.contains('@') {
Ok(())
} else {
Err(format!("{fieldpath}: invalid email format"))
}
} else {
Err(format!("{fieldpath}: expected string"))
}
}
fn name(&self) -> &str { "email" }
fn description(&self) -> &str { "Validates email format" }
}
let mut validator = Validator::new(ValidationConfig::default())?;
validator.add_custom_rule("email".to_string(), Box::new(EmailRule));Sourcepub fn clear_cache(&self) -> Result<(), CoreError>
pub fn clear_cache(&self) -> Result<(), CoreError>
Clear validation cache
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Validator
impl !RefUnwindSafe for Validator
impl Send for Validator
impl Sync for Validator
impl Unpin for Validator
impl !UnwindSafe for Validator
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CheckedAs for T
impl<T> CheckedAs for T
Source§fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
Source§impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
Source§fn checked_cast_from(src: Src) -> Option<Dst>
fn checked_cast_from(src: Src) -> Option<Dst>
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> OverflowingAs for T
impl<T> OverflowingAs for T
Source§fn overflowing_as<Dst>(self) -> (Dst, bool)where
T: OverflowingCast<Dst>,
fn overflowing_as<Dst>(self) -> (Dst, bool)where
T: OverflowingCast<Dst>,
Source§impl<Src, Dst> OverflowingCastFrom<Src> for Dstwhere
Src: OverflowingCast<Dst>,
impl<Src, Dst> OverflowingCastFrom<Src> for Dstwhere
Src: OverflowingCast<Dst>,
Source§fn overflowing_cast_from(src: Src) -> (Dst, bool)
fn overflowing_cast_from(src: Src) -> (Dst, bool)
Source§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Source§impl<T> SaturatingAs for T
impl<T> SaturatingAs for T
Source§fn saturating_as<Dst>(self) -> Dstwhere
T: SaturatingCast<Dst>,
fn saturating_as<Dst>(self) -> Dstwhere
T: SaturatingCast<Dst>,
Source§impl<Src, Dst> SaturatingCastFrom<Src> for Dstwhere
Src: SaturatingCast<Dst>,
impl<Src, Dst> SaturatingCastFrom<Src> for Dstwhere
Src: SaturatingCast<Dst>,
Source§fn saturating_cast_from(src: Src) -> Dst
fn saturating_cast_from(src: Src) -> Dst
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.