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_prefixlist. - The value does not end with any of the suffixes in the
ban_suffixlist. - The value does not contain any of the characters in the
ban_charstring. - If a regular expression is present in
matches, the possibly-normalized value must match against the expression. - If the
inlist 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
ninlist.
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: StringAn 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: u32The maximum allowed number of bytes in the string value.
min_len: u32The minimum allowed number of bytes in the string value.
max_char: u32The maximum allowed number of unicode characters in the string value.
min_char: u32The minimum allowed number of unicode characters in the string value.
normalize: NormalizeThe Unicode normalization setting.
ban_prefix: Vec<String>Banned string prefixes.
ban_suffix: Vec<String>Banned string suffixes.
ban_char: StringBanned characters.
query: boolIf true, queries against matching spots may have values in the in or nin lists.
regex: boolIf true, queries against matching spots may use the matches value.
ban: boolIf true, queries against matching spots may set the ban_prefix, ban_suffix, and
ban_char values to non-defaults.
size: boolIf 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
impl StrValidator
Sourcepub fn normalize(self, normalize: Normalize) -> Self
pub fn normalize(self, normalize: Normalize) -> Self
Set the unicode normalization form to use for in, nin, and matches checks.
Sourcepub fn ban_prefix_add(self, add: impl Into<String>) -> Self
pub fn ban_prefix_add(self, add: impl Into<String>) -> Self
Add a value to the ban_prefix list.
Sourcepub fn ban_suffix_add(self, add: impl Into<String>) -> Self
pub fn ban_suffix_add(self, add: impl Into<String>) -> Self
Add a value to the ban_suffix list.
Sourcepub fn query(self, query: bool) -> Self
pub fn query(self, query: bool) -> Self
Set whether or not queries can use the in and nin lists.
Sourcepub fn ban(self, ban: bool) -> Self
pub fn ban(self, ban: bool) -> Self
Set whether or not queries can use the ban_prefix, ban_suffix, and ban_char values.
Trait Implementations§
Source§impl Clone for StrValidator
impl Clone for StrValidator
Source§fn clone(&self) -> StrValidator
fn clone(&self) -> StrValidator
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more