Skip to main content

Validator

Struct Validator 

Source
pub struct Validator { /* private fields */ }
Expand description

Checks input against a rule set.

use rainier_validation::{Rule, Validator};
use serde_json::json;

let validator = Validator::new()
    .rules("email", vec![Rule::Required, Rule::Email])
    .rules("age", vec![Rule::Integer, Rule::Min(18.0)]);

let errors = validator.validate(&json!({ "email": "not-an-email" })).unwrap_err();
assert_eq!(errors.first("email"), Some("The email field must be a valid email address."));
assert!(!errors.has("age"), "an absent optional field does not fail");

Implementations§

Source§

impl Validator

Source

pub fn new() -> Self

An empty validator.

Source

pub fn from_rules(rules: RuleSet) -> Self

Build from a RuleSet.

Source

pub fn rules(self, field: impl Into<String>, rules: Vec<Rule>) -> Self

Add rules for field.

field may be a dotted path (address.city) or contain * to reach into an array (items.*.quantity).

Source

pub fn custom_message( self, key: impl Into<String>, message: impl Into<String>, ) -> Self

Override the message for one field.rule pair — custom_message("email.required", "We need your email.").

Source

pub fn custom_messages( self, messages: impl IntoIterator<Item = (impl Into<String>, impl Into<String>)>, ) -> Self

Add several custom messages.

Source

pub fn validate(&self, input: &Value) -> Result<(), ValidationErrors>

Validate input, returning the failures.

Source

pub fn validated(&self, input: &Value) -> Result<Value, ValidationErrors>

Validate and hand back the input, for chaining.

Source

pub fn validated_only(&self, input: &Value) -> Result<Value, ValidationErrors>

Validate and keep only the fields that had rules.

This is a security feature, not a convenience: it is what stops a client from smuggling an is_admin field into a mass assignment. Only keys you wrote a rule for survive.

Trait Implementations§

Source§

impl Debug for Validator

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Validator

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.