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§
Required Methods§
Sourcefn execute_validation(
&self,
ctx: &mut ValidationCtx,
val: Option<&Self::Target>,
) -> ValidationResult
fn execute_validation( &self, ctx: &mut ValidationCtx, val: Option<&Self::Target>, ) -> ValidationResult
Validates the Target, with customized settings.
Provided Methods§
Sourcefn schema(&self) -> Option<ValidatorSchema>
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.
Sourcefn check_consistency(&self) -> Result<(), Vec<ConsistencyError>>
fn check_consistency(&self) -> Result<(), Vec<ConsistencyError>>
Checks if the inputs of the validators are valid.
Sourcefn check_cel_programs_with(
&self,
_val: <Self::Target as ToOwned>::Owned,
) -> Result<(), Vec<CelError>>
Available on crate feature cel only.
fn check_cel_programs_with( &self, _val: <Self::Target as ToOwned>::Owned, ) -> Result<(), Vec<CelError>>
cel only.Tests the CEL programs of this validator with the given value.
§Panics
Panics if one of the CEL expressions failed to compile.
Sourcefn validate(&self, val: &Self::Target) -> Result<(), ValidationErrors>
fn validate(&self, val: &Self::Target) -> Result<(), ValidationErrors>
Validates the Target, with the fail_fast setting set to true.
Sourcefn validate_option(
&self,
val: Option<&Self::Target>,
) -> Result<(), ValidationErrors>
fn validate_option( &self, val: Option<&Self::Target>, ) -> Result<(), ValidationErrors>
Validates the Target, with the fail_fast setting set to true.
Sourcefn validate_with_ctx(
&self,
ctx: ValidationCtx,
val: &Self::Target,
) -> Result<(), ValidationErrors>
fn validate_with_ctx( &self, ctx: ValidationCtx, val: &Self::Target, ) -> Result<(), ValidationErrors>
Validates the Target, with customized settings.
Sourcefn validate_option_with_ctx(
&self,
ctx: ValidationCtx,
val: Option<&Self::Target>,
) -> Result<(), ValidationErrors>
fn validate_option_with_ctx( &self, ctx: ValidationCtx, val: Option<&Self::Target>, ) -> Result<(), ValidationErrors>
Validates the Target, with customized settings.