StrValidator

Struct StrValidator 

Source
pub struct StrValidator {
Show 16 fields pub comment: String, pub in_list: Vec<String>, pub nin_list: Vec<String>, pub matches: Option<Box<Regex>>, pub max_len: u32, pub min_len: u32, pub max_char: u32, pub min_char: u32, pub normalize: Normalize, pub ban_prefix: Vec<String>, pub ban_suffix: Vec<String>, pub ban_char: String, pub query: bool, pub regex: bool, pub ban: bool, pub size: bool,
}
Expand description

Validator for UTF-8 strings.

This validator type will only pass string values. Validation passes if:

  • The value’s length in bytes is less than or equal to the value in max_len.
  • The value’s length in bytes is greater than or equal to the value in min_len.
  • The value’s number of unicode characters is less than or equal to the value in max_char.
  • The value’s number of unicode characters is greater than or equal to the value in min_char.
  • The value does not begin with any of the prefixes in the ban_prefix list.
  • The value does not end with any of the suffixes in the ban_suffix list.
  • The value does not contain any of the characters in the ban_char string.
  • If a regular expression is present in matches, the possibly-normalized value must match against the expression.
  • If the in list is not empty, the possibly-normalized value must be among the values in the list.
  • The possibly-normalized value must not be among the values in the nin list.

The normalize field may be set to None, NFC, or NFKC, corresponding to Unicode normalization forms. When checked for in, nin, ban_prefix, ban_suffix, ban_char, and matches, the value is first put into the selected normalization form, and any in, nin, ban_prefix, and ban_suffix list strings are normalized as well.

§Defaults

Fields that aren’t specified for the validator use their defaults instead. The defaults for each field are:

  • comment: “”
  • in_list: empty
  • nin_list: empty
  • matches: None
  • max_len: u32::MAX
  • min_len: 0
  • max_char: u32::MAX
  • min_char: 0
  • normalize: Normalize::None
  • ban_prefix: empty
  • ban_suffix: empty
  • ban_char: “”
  • query: false
  • regex: false
  • size: false

§Regular Expressions

Regular expressions can be set for StrValidator using the matches field, but should be used sparingly, and should generally be avoided if possible. If they must be used, be aware of their limitations due to their memory, computation, and general consistency issues.

Before you use regular expressions or try to work around the look-around limitations, consider whether or not your validation requirement can be fulfilled by using some combination of the ban_prefix, ban_suffix, ban_char, in, and nin fields.

Regular expression can rapidly use up a lot of memory when compiled. This is one of the reasons why it is inadvisable to accept and use unknown schemas without first checking for regexes. For queries, a schema will have some upper limit on the number of allowed regular expressions, in order to mitigate possible memory exhaustion.

Beyond their memory cost, regular expressions have a second problem: there’s not really a universal standard for regular expressions; at least, not one that is rigidly followed in implementations. The Rust fog-pack library uses the regex crate for regular expressions, supporting Perl-style expression syntax, unicode character classes, and flags for unicode support and case insensitivity. Look around and backreferences are not supported. It is hoped that other implementations will support the same syntax, with the same limitations on look around and backreferences.

Finally, because unicode support is enabled, it is possible to have a string that fails on one library version and succeeds on another due to Unicode versions changing their character class definitions. This is a corner case, but any schema writer should be aware of it as a possibility.

§Unicode NFC and NFKC

Unicode normalization can be tricky to get right. Strings are never required to be in a particular normalization form, as it may be that the creator or user of a string specifically wants no normalization, but a query or schema may desire it. To this end, normalization of the string being validated, as well as the in and nin lists’ strings can all be done before running validation. This is settable through the normalization field, which can be None, NFC, or NFKC.

Fields§

§comment: String

An optional comment explaining the validator.

§in_list: Vec<String>

A vector of specific allowed values, stored under the in field. If empty, this vector is not checked against.

§nin_list: Vec<String>

A vector of specific unallowed values, stored under the nin field.

§matches: Option<Box<Regex>>

A regular expression that the value must match against.

§max_len: u32

The maximum allowed number of bytes in the string value.

§min_len: u32

The minimum allowed number of bytes in the string value.

§max_char: u32

The maximum allowed number of unicode characters in the string value.

§min_char: u32

The minimum allowed number of unicode characters in the string value.

§normalize: Normalize

The Unicode normalization setting.

§ban_prefix: Vec<String>

Banned string prefixes.

§ban_suffix: Vec<String>

Banned string suffixes.

§ban_char: String

Banned characters.

§query: bool

If true, queries against matching spots may have values in the in or nin lists.

§regex: bool

If true, queries against matching spots may use the matches value.

§ban: bool

If true, queries against matching spots may set the ban_prefix, ban_suffix, and ban_char values to non-defaults.

§size: bool

If true, queries against matching spots may set the max_len, min_len, max_char, and min_char values to non-defaults.

Implementations§

Source§

impl StrValidator

Source

pub fn new() -> Self

Make a new validator with the default configuration.

Source

pub fn comment(self, comment: impl Into<String>) -> Self

Set a comment for the validator.

Source

pub fn max_len(self, max_len: u32) -> Self

Set the maximum number of allowed bytes.

Source

pub fn min_len(self, min_len: u32) -> Self

Set the minimum number of allowed bytes.

Source

pub fn max_char(self, max_char: u32) -> Self

Set the maximum number of allowed characters.

Source

pub fn min_char(self, min_char: u32) -> Self

Set the minimum number of allowed characters.

Source

pub fn normalize(self, normalize: Normalize) -> Self

Set the unicode normalization form to use for in, nin, and matches checks.

Source

pub fn matches(self, matches: Regex) -> Self

Set the regular expression to check against.

Source

pub fn in_add(self, add: impl Into<String>) -> Self

Add a value to the in list.

Source

pub fn nin_add(self, add: impl Into<String>) -> Self

Add a value to the nin list.

Source

pub fn ban_prefix_add(self, add: impl Into<String>) -> Self

Add a value to the ban_prefix list.

Source

pub fn ban_suffix_add(self, add: impl Into<String>) -> Self

Add a value to the ban_suffix list.

Source

pub fn ban_char(self, ban_char: impl Into<String>) -> Self

Set the ban_char string.

Source

pub fn query(self, query: bool) -> Self

Set whether or not queries can use the in and nin lists.

Source

pub fn regex(self, regex: bool) -> Self

Set whether or not queries can use the matches value.

Source

pub fn ban(self, ban: bool) -> Self

Set whether or not queries can use the ban_prefix, ban_suffix, and ban_char values.

Source

pub fn size(self, ord: bool) -> Self

Set whether or not queries can use the max_len, min_len, max_char, and min_char values.

Source

pub fn build(self) -> Validator

Build this into a Validator enum.

Trait Implementations§

Source§

impl Clone for StrValidator

Source§

fn clone(&self) -> StrValidator

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for StrValidator

Source§

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

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

impl Default for StrValidator

Source§

fn default() -> Self

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

impl<'de> Deserialize<'de> for StrValidator

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl PartialEq for StrValidator

Source§

fn eq(&self, rhs: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for StrValidator

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,