Skip to main content

Validation

Trait Validation 

Source
pub trait Validation {
    type Error: Error;

    // Required method
    fn visit_validation<T>(
        &self,
        handle_validation_error: Box<dyn FnMut(Self::Error) -> Result<(), T> + '_>,
    ) -> Result<(), T>;

    // Provided methods
    fn is_valid(&self) -> bool { ... }
    fn validation_errors(&self) -> Vec<Self::Error> { ... }
    fn check_validation(&self) -> Result<(), Self::Error> { ... }
}
Expand description

A trait to check if a geometry is valid and report the reason(s) of invalidity.

use geo::algorithm::Validation;
use geo::wkt;

let valid_polygon = wkt!(POLYGON((0. 0., 1. 1., 1. 0., 0. 0.)));
assert!(valid_polygon.is_valid());

let invalid_polygon = wkt!(POLYGON((0. 0., 1. 1.),(3. 3., 3. 4.,4. 4.)));
assert!(!invalid_polygon.is_valid());

// Get the first validation error, as a `Result`
let validation_error = invalid_polygon.check_validation().unwrap_err();
use geo::algorithm::validation::{InvalidPolygon, RingRole};
assert_eq!(validation_error, InvalidPolygon::TooFewPointsInRing(RingRole::Exterior));

// Get a human readable error
let text = validation_error.to_string();
assert_eq!(text, "exterior ring must have at least 3 distinct points");
// Get all validation errors
let all_validation_errors = invalid_polygon.validation_errors();
assert_eq!(all_validation_errors.len(), 2);
assert_eq!(all_validation_errors[0].to_string(), "exterior ring must have at least 3 distinct points");
assert_eq!(all_validation_errors[1].to_string(), "interior ring at index 0 is not contained within the polygon's exterior");

Required Associated Types§

Required Methods§

Source

fn visit_validation<T>( &self, handle_validation_error: Box<dyn FnMut(Self::Error) -> Result<(), T> + '_>, ) -> Result<(), T>

Visit the validation of the geometry.

The closure handle_validation_error is called for each validation error.

Provided Methods§

Source

fn is_valid(&self) -> bool

Check if the geometry is valid.

Source

fn validation_errors(&self) -> Vec<Self::Error>

Return the reason(s) of invalidity of the geometry.

Though we try to return all problems with a geometry, it’s possible that previous errors will obscure subsequent errors. For example, a MultiPolygon requires all its elements to be valid and non-overlapping. If one of the individual polygons is invalid, we can’t guarantee the correctness of their “overlap” check which assumes valid input. Therefore, you should re-validate after attempting to correct any validation errors.

Source

fn check_validation(&self) -> Result<(), Self::Error>

Return the first reason of invalidity of the geometry.

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§