Struct fog_pack::validator::StrValidator[][src]

pub struct StrValidator {
    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 query: bool,
    pub regex: 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.
  • 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, and matches, the value is first put into the selected normalization form, and any in and nin 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
  • 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.

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

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.

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

Make a new validator with the default configuration.

Set a comment for the validator.

Set the maximum number of allowed bytes.

Set the minimum number of allowed bytes.

Set the maximum number of allowed characters.

Set the minimum number of allowed characters.

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

Set the regular expression to check against.

Add a value to the in list.

Add a value to the nin list.

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

Set whether or not queries can use the bits_clr and bits_set values.

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

Build this into a Validator enum.

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

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

Deserialize this value from the given Serde deserializer. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

Should always be Self

The resulting type after obtaining ownership.

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

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

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

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.