Skip to main content

Validator

Trait Validator 

Source
pub trait Validator<T: ?Sized>: Send + Sync {
    type Target: ToOwned + ?Sized;

    // Required method
    fn execute_validation(
        &self,
        ctx: &mut ValidationCtx,
        val: Option<&Self::Target>,
    ) -> ValidationResult;

    // Provided methods
    fn schema(&self) -> Option<ValidatorSchema> { ... }
    fn check_consistency(&self) -> Result<(), Vec<ConsistencyError>> { ... }
    fn check_cel_programs_with(
        &self,
        _val: <Self::Target as ToOwned>::Owned,
    ) -> Result<(), Vec<CelError>> { ... }
    fn validate(&self, val: &Self::Target) -> Result<(), ValidationErrors> { ... }
    fn validate_option(
        &self,
        val: Option<&Self::Target>,
    ) -> Result<(), ValidationErrors> { ... }
    fn validate_with_ctx(
        &self,
        ctx: ValidationCtx,
        val: &Self::Target,
    ) -> Result<(), ValidationErrors> { ... }
    fn validate_option_with_ctx(
        &self,
        ctx: ValidationCtx,
        val: Option<&Self::Target>,
    ) -> Result<(), ValidationErrors> { ... }
}
Expand description

The trait implemented by the validators, which can be structs or functions used with FnValidator.

The generic type and the Target type are separated so that this can be used for wrapper types such as Sint32 and others.

The Target is the actual type that will be validated.

The only required method is execute_validation, all the other validation methods are automatically implemented.

Validators can optionally implement the schema method, which allows them to be turned into protobuf options for file generation.

Required Associated Types§

Source

type Target: ToOwned + ?Sized

The target of the validation.

Required Methods§

Source

fn execute_validation( &self, ctx: &mut ValidationCtx, val: Option<&Self::Target>, ) -> ValidationResult

Validates the Target, with customized settings.

Provided Methods§

Source

fn schema(&self) -> Option<ValidatorSchema>

Returns the optional schema representation for this validator.

If a schema representation is present, whenever a validator is used by a message or a oneof, its schema representation will be present in the generated protobuf files.

Source

fn check_consistency(&self) -> Result<(), Vec<ConsistencyError>>

Checks if the inputs of the validators are valid.

Source

fn check_cel_programs_with( &self, _val: <Self::Target as ToOwned>::Owned, ) -> Result<(), Vec<CelError>>

Available on crate feature cel only.

Tests the CEL programs of this validator with the given value.

§Panics

Panics if one of the CEL expressions failed to compile.

Source

fn validate(&self, val: &Self::Target) -> Result<(), ValidationErrors>

Validates the Target, with the fail_fast setting set to true.

Source

fn validate_option( &self, val: Option<&Self::Target>, ) -> Result<(), ValidationErrors>

Validates the Target, with the fail_fast setting set to true.

Source

fn validate_with_ctx( &self, ctx: ValidationCtx, val: &Self::Target, ) -> Result<(), ValidationErrors>

Validates the Target, with customized settings.

Source

fn validate_option_with_ctx( &self, ctx: ValidationCtx, val: Option<&Self::Target>, ) -> Result<(), ValidationErrors>

Validates the Target, with customized settings.

Implementors§

Source§

impl Validator<bool> for BoolValidator

Source§

impl Validator<Bytes> for BytesValidator

Source§

impl Validator<String> for StringValidator

Source§

impl Validator<Any> for AnyValidator

Source§

impl Validator<Duration> for DurationValidator

Source§

impl Validator<FieldMask> for FieldMaskValidator

Source§

impl Validator<Timestamp> for TimestampValidator

Source§

impl<F, T> Validator<T> for FnValidator<F, T>
where T: ToOwned + ?Sized + Send + Sync, F: Fn(&mut ValidationCtx, Option<&T>) -> ValidationResult + Send + Sync,

Source§

impl<K, V, M> Validator<M> for MapValidator<K, V>
where K: ProtoValidation + Send + Sync + AsProtoType, V: ProtoValidation + Send + Sync + AsProtoType, M: ProtoMap<K, V> + ToOwned, M::Target: Clone + Default, K::Stored: Sized + Clone + IntoCelKey + Into<Subscript>, V::Stored: Sized + Clone + TryIntoCel,

Source§

type Target = <M as ProtoMap<K, V>>::Target

Source§

impl<Num> Validator<Num> for FloatValidator<Num>
where Num: FloatWrapper,

Source§

type Target = Num

Source§

impl<Num> Validator<Num> for IntValidator<Num>
where Num: IntWrapper,

Source§

impl<T> Validator<Vec<T>> for RepeatedValidator<T>
where T: ProtoValidation + Send + Sync, T::Stored: TryIntoCel + Sized + Clone,

Source§

impl<T> Validator<T> for CelValidator
where T: ValidatedMessage + PartialEq + TryIntoCel + Default + Clone,

Source§

impl<T> Validator<T> for MessageValidator
where T: ValidatedMessage + PartialEq + TryIntoCel,

Source§

impl<T: ProtoEnum> Validator<T> for EnumValidator<T>

Source§

impl<T: ValidatedOneof> Validator<T> for OneofValidator