Rust

Crate root

Module
What utopia_validators defines at its root.

Contents

Functions
array_list_description`ArrayList::getDescription()`: `item` is the wrapped validator's description, `None` when it is `''` or `'0'` (which PHP leaves out).ascii_lowerPHP `strtolower()`: ASCII only, whatever the locale (PHP 8.2 and later).booleanA boolean; when `loose`, also `'true'`, `'false'`, `'1'`, `'0'`, `1` and `0`.charsPHP `mb_strlen($value)`: each sequence that is not UTF-8 counts as one character.containsPHP `str_contains($haystack, $needle)`: an empty needle is in every haystack.contains_description`Contains::getDescription()`, given `implode(', ', $this->patterns)`, appended to `message`.domain`Domain::isValid()`'s format checks on a non-empty string: `filter_var(FILTER_VALIDATE_DOMAIN)` (with `FILTER_FLAG_HOSTNAME` when `hostnames`) and no trailing `.` or `-`.exclusionWhether a `Globstar` pattern is an exclusion (`!…`).explode_count`count(explode('.', $value, $limit))`.fcm_optional_errorThe error for an optional field given but not a non-empty string.fcm_required_errorThe error `FCM::isValid()` records for a required field that is missing or blank.fcm_url_errorThe error for a URL field that is not an HTTPS URL.floatAn int or a float; when `loose`, anything `is_numeric()`.globstar_regexThe regular expression `Globstar::matchGlobstar()` hands `preg_match()` for `pattern`, delimiters and anchors included (`~^…$~`).hex_colorA string of 3 or 6 hex digits, no `#`. The pattern's `$` also matches before a final newline, so one trailing `"\n"` is allowed.host_description`Host::getDescription()`, given `implode(', ', $whitelist)`, appended to `description`.hostnameThe checks of `Hostname::isValid()` before its allow list: not `''` or `'0'`, at most 253 characters (`mb_strlen()`), no `/` and no `:`.hostname_allowedWhether `value` matches an allow-list entry `allowed` that is a string: equal to it, `*`, or a `*.suffix` wildcard that `value` ends with (the root itself is not matched).https_schemePHP `strtolower($scheme) === 'https'` (ASCII lowercasing, as PHP 8 does).identifier`preg_match('/^[A-Za-z_]\w*$/D', $value) === 1`: an ASCII letter or `_`, then ASCII letters, digits and `_` (the pattern has no `/u`, so `\w` is ASCII), up to the very end.identifier_description`Identifier::getDescription()`, appended to `description`.ip`IP::isValid()`: `filter_var($value, FILTER_VALIDATE_IP[, flag])` is truthy.ip_str`ip` of a string: the filter on the string as it is (an address it accepts is never `''` or `'0'`, so accepted is truthy).is_blank`preg_match('/[^\p{Z}\p{Cf}\s]/u', $value) !== 1`: the value holds only Unicode separators, format characters and whitespace (nothing at all included), or is not UTF-8, which `preg_match()` refuses.json_array_description`JSON\ArrayValidator::getDescription()`, appended to `message`.json_length`hasValidLength()` of the JSON validators: `encoded` is `None` for `false`.json_object_description`JSON\ObjectValidator::getDescription()`, appended to `message`.label`Label::isValid()` on a string: 1 to 63 bytes of lowercase ASCII letters, digits and hyphens, not starting or ending with a hyphen. (`ctype_alnum()` of what is left without the hyphens, so a label of hyphens only is refused.)lowerPHP `mb_strtolower($value, 'UTF-8')`: full lower case, the final sigma rule, and `?` for each sequence that is not UTF-8.non_empty`FCM::isNonEmptyString()` of a string.number_lePHP `$a <= $b` on two numbers: exact between ints, as floats otherwise (NaN is never smaller or equal).numericAnything `is_numeric()`: ints, floats (`NAN` and `INF` included) and numeric strings.pem_private_key`str_starts_with($key, PEM_BEGIN) && str_ends_with(trim($key), PEM_END)`.phone`Phone::isValid()`'s pattern, `/^\+[1-9]\d{6,14}$/`.phone_normalize`Phone::normalize()`: `rawurldecode()`, then a leading space (a `+` a query string decoded as a space) or no `+` at all before 7 to 15 digits becomes a `+`.portA port from 1 to 65535, optionally after an IPv4 address or a bracketed IPv6 one and a colon (`127.0.0.1:80`, `[::1]:443`). An int is checked as its decimal string.port_hostAn IPv4 address, or an IPv6 address in brackets.port_numberDecimal digits without a leading zero, from 1 to 65535.preg_quotePHP `preg_quote($value, '~')`.private_use_scheme`URL::isPrivateUseSchemeURI()` on a string: `Uri\Rfc3986\Uri::parse()` succeeds, its (normalized) scheme contains a `.` and it has no host.rangeA number from `min` to `max`, both included, of `format` (see `range_number`).range_description`Value must be a valid range between 1 and 10`, both bounds as `number_format()` prints them.range_numberThe number `Range::isValid()` compares with its bounds, for a value that passed `is_numeric()`: as `'integer'`, an int (from `+ 0`, so a numeric string that makes one) or a float that already is `INF`/`-INF` (PHP checks `$value === INF` before `+ 0`, so a string such as `'1e400'` that overflows to `INF` is not one); as `'double'`, any number as a float; `None` when the value is not one, and for any other format.text_description`Text::getDescription()`, given `implode(', ', $this->allowList)` when the list is not empty.text_length`mb_strlen($value) >= $min` and, for a `$length` other than 0, `mb_strlen($value) <= $length`.trimPHP `trim($value)`: the default characters (`" \t\n\r\0\x0B"`) from both ends.urlWhether `filter_var($value, FILTER_VALIDATE_URL)` is not `false`.url_ascii`URL::isValid()`'s IRI step: each byte above 127 as `rawurlencode()` encodes it.url_component`parse_url($value, $component)` for a string component: `None` where PHP returns `false` (a malformed URL), `Some(None)` for `null` (no such component).url_description`URL::getDescription()`, given `implode(', ', $allowedSchemes)` when there are schemes.url_str`url` of a string: the filter on the string as it is.white_list_description`WhiteList::getDescription()`, given `implode(', ', $this->list)`, appended to `message`.wildcardWhether a `Globstar` pattern holds a wildcard (`*`, `?` or `[`).
use utopia_validators::Boolean;
Rust
pub struct Boolean;

true, false, 1 or 0, as a boolean or as text.

PHP: new Boolean(loose: true).

use utopia_validators::Float;
Rust
pub struct Float;

Any number, as an int, a float or text.

PHP: new FloatValidator(loose: true).

use utopia_validators::Hostname;
Rust
pub struct Hostname;

A hostname: no path, port or protocol, and at most 253 characters.

PHP: new Hostname().

use utopia_validators::Integer;
Rust
pub struct Integer {
    pub bits: i64,
    pub unsigned: bool,
}

An integer of bits bits, signed or unsigned.

PHP: Integer's $bits and $unsigned.

use utopia_validators::Port;
Rust
pub struct Port;

A port from 1 to 65535, as an int or as text.

PHP: new Port(), which also accepts a port after an IP address (127.0.0.1:80). That is not a u16, so it is refused here.

use utopia_validators::Range;
Rust
pub struct Range {
    pub min: i64,
    pub max: i64,
}

An integer from min to max, both included, as an int or as text.

PHP: new Range($min, $max).

use utopia_validators::Text;
Rust
pub struct Text {
    pub length: i64,
    pub min: i64,
}

A UTF-8 string of min to length characters.

PHP: new Text($length, $min).

use utopia_validators::WhiteList;
Rust
pub struct WhiteList<'a> {
    pub list: &'a [&'a str],
}

One of the strings of a list, exactly as the list spells it.

PHP: new WhiteList($list, strict: true).

use utopia_validators::Input;
Rust
pub enum Input<'a> {
    Null,
    Bool(bool),
    Int(i64),
    Float(f64),
    Str(&'a [u8]),
    Other,
}

A PHP value as the validators look at it: scalars by value, strings as bytes, and everything else (arrays, objects, resources) as Input::Other, which no check here accepts as a number or a string.

use utopia_validators::IntegerError;
Rust
pub enum IntegerError {
    Bits,
    Unsigned64,
}

Why new Integer() refuses its arguments; IntegerError::message is the InvalidArgumentException's.

use utopia_validators::IpType;
Rust
pub enum IpType {
    All,
    V4,
    V6,
}

The IP versions IP accepts: IP::ALL, IP::V4 or IP::V6.

use utopia_validators::Validator;
Rust
pub trait Validator

A check of one value that returns it in the type it was checked as.

Rust
use utopia_validators::{Input, Port, Validator};

assert_eq!(Port.validate(Input::Str(b"6379")), Some(6379));
assert_eq!(Port.validate(Input::Int(70000)), None);
assert_eq!(Port.description(), "Value must be a port between 1 and 65535");
PHP
use Utopia\Validator\Port;

$port = new Port();
var_dump($port->isValid('6379'));  // true
var_dump($port->isValid(70000));   // false
use utopia_validators::array_list_description;
Rust
pub fn array_list_description(length: i64, item: Option<&[u8]>, message: &mut Vec<u8>)

ArrayList::getDescription(): item is the wrapped validator's description, None when it is '' or '0' (which PHP leaves out).

Appended to message.

use utopia_validators::ascii_lower;
Rust
pub fn ascii_lower(value: &[u8]) -> Vec<u8>

PHP strtolower(): ASCII only, whatever the locale (PHP 8.2 and later).

use utopia_validators::boolean;
Rust
pub fn boolean(loose: bool, value: Input<'_>) -> bool

A boolean; when loose, also 'true', 'false', '1', '0', 1 and 0.

PHP: Boolean::isValid().

use utopia_validators::chars;
Rust
pub fn chars(value: &[u8]) -> i64

PHP mb_strlen($value): each sequence that is not UTF-8 counts as one character.

use utopia_validators::contains;
Rust
pub fn contains(haystack: &[u8], needle: &[u8]) -> bool

PHP str_contains($haystack, $needle): an empty needle is in every haystack.

use utopia_validators::contains_description;
Rust
pub fn contains_description(patterns: &[u8], strict: bool, message: &mut Vec<u8>)

Contains::getDescription(), given implode(', ', $this->patterns), appended to message.

use utopia_validators::domain;
Rust
pub fn domain(value: &[u8], hostnames: bool) -> bool

Domain::isValid()'s format checks on a non-empty string: filter_var(FILTER_VALIDATE_DOMAIN) (with FILTER_FLAG_HOSTNAME when hostnames) and no trailing . or -.

use utopia_validators::exclusion;
Rust
pub fn exclusion(pattern: &[u8]) -> bool

Whether a Globstar pattern is an exclusion (!…).

use utopia_validators::explode_count;
Rust
pub fn explode_count(value: &[u8], limit: i64) -> i64

count(explode('.', $value, $limit)).

use utopia_validators::fcm_optional_error;
Rust
pub fn fcm_optional_error(field: &str) -> String

The error for an optional field given but not a non-empty string.

use utopia_validators::fcm_required_error;
Rust
pub fn fcm_required_error(field: &str, purpose: &str) -> String

The error FCM::isValid() records for a required field that is missing or blank.

use utopia_validators::fcm_url_error;
Rust
pub fn fcm_url_error(field: &str) -> String

The error for a URL field that is not an HTTPS URL.

use utopia_validators::float;
Rust
pub fn float(loose: bool, value: Input<'_>) -> bool

An int or a float; when loose, anything is_numeric().

PHP: FloatValidator::isValid().

use utopia_validators::globstar_regex;
Rust
pub fn globstar_regex(pattern: &[u8]) -> Vec<u8>

The regular expression Globstar::matchGlobstar() hands preg_match() for pattern, delimiters and anchors included (~^…$~).

use utopia_validators::hex_color;
Rust
pub fn hex_color(value: Input<'_>) -> bool

A string of 3 or 6 hex digits, no #. The pattern's $ also matches before a final newline, so one trailing "\n" is allowed.

PHP: HexColor::isValid() (/^([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/).

use utopia_validators::host_description;
Rust
pub fn host_description(whitelist: &[u8], description: &mut Vec<u8>)

Host::getDescription(), given implode(', ', $whitelist), appended to description.

use utopia_validators::hostname;
Rust
pub fn hostname(value: &[u8]) -> bool

The checks of Hostname::isValid() before its allow list: not '' or '0', at most 253 characters (mb_strlen()), no / and no :.

use utopia_validators::hostname_allowed;
Rust
pub fn hostname_allowed(value: &[u8], allowed: &[u8]) -> bool

Whether value matches an allow-list entry allowed that is a string: equal to it, *, or a *.suffix wildcard that value ends with (the root itself is not matched).

use utopia_validators::https_scheme;
Rust
pub fn https_scheme(scheme: &[u8]) -> bool

PHP strtolower($scheme) === 'https' (ASCII lowercasing, as PHP 8 does).

use utopia_validators::identifier;
Rust
pub fn identifier(value: &[u8]) -> bool

preg_match('/^[A-Za-z_]\w*$/D', $value) === 1: an ASCII letter or _, then ASCII letters, digits and _ (the pattern has no /u, so \w is ASCII), up to the very end.

PHP: Identifier::isValid() after Text::isValid().

use utopia_validators::identifier_description;
Rust
pub fn identifier_description(length: i64, description: &mut Vec<u8>)

Identifier::getDescription(), appended to description.

use utopia_validators::ip;
Rust
pub fn ip(value: &Value, kind: IpType) -> bool

IP::isValid(): filter_var($value, FILTER_VALIDATE_IP[, flag]) is truthy.

use utopia_validators::ip_str;
Rust
pub fn ip_str(value: &[u8], kind: IpType) -> bool

ip of a string: the filter on the string as it is (an address it accepts is never '' or '0', so accepted is truthy).

use utopia_validators::is_blank;
Rust
pub fn is_blank(value: &[u8]) -> bool

preg_match('/[^\p{Z}\p{Cf}\s]/u', $value) !== 1: the value holds only Unicode separators, format characters and whitespace (nothing at all included), or is not UTF-8, which preg_match() refuses.

PHP: Text::isValid() with $requireNonBlank.

use utopia_validators::json_array_description;
Rust
pub fn json_array_description(length: i64, message: &mut Vec<u8>)

JSON\ArrayValidator::getDescription(), appended to message.

use utopia_validators::json_length;
Rust
pub fn json_length(length: i64, encoded: Option<&[u8]>) -> bool

hasValidLength() of the JSON validators: encoded is None for false.

use utopia_validators::json_object_description;
Rust
pub fn json_object_description(length: i64, message: &mut Vec<u8>)

JSON\ObjectValidator::getDescription(), appended to message.

use utopia_validators::label;
Rust
pub fn label(value: &[u8]) -> bool

Label::isValid() on a string: 1 to 63 bytes of lowercase ASCII letters, digits and hyphens, not starting or ending with a hyphen. (ctype_alnum() of what is left without the hyphens, so a label of hyphens only is refused.)

use utopia_validators::lower;
Rust
pub fn lower(value: &[u8]) -> Vec<u8>

PHP mb_strtolower($value, 'UTF-8'): full lower case, the final sigma rule, and ? for each sequence that is not UTF-8.

PHP: Contains::isValid() when it is not strict.

use utopia_validators::non_empty;
Rust
pub fn non_empty(value: &[u8]) -> bool

FCM::isNonEmptyString() of a string.

use utopia_validators::number_le;
Rust
pub fn number_le(a: Number, b: Number) -> bool

PHP $a <= $b on two numbers: exact between ints, as floats otherwise (NaN is never smaller or equal).

use utopia_validators::numeric;
Rust
pub fn numeric(value: Input<'_>) -> bool

Anything is_numeric(): ints, floats (NAN and INF included) and numeric strings.

PHP: Numeric::isValid().

use utopia_validators::pem_private_key;
Rust
pub fn pem_private_key(key: &[u8]) -> bool

str_starts_with($key, PEM_BEGIN) && str_ends_with(trim($key), PEM_END).

use utopia_validators::phone;
Rust
pub fn phone(value: &[u8]) -> bool

Phone::isValid()'s pattern, /^\+[1-9]\d{6,14}$/.

use utopia_validators::phone_normalize;
Rust
pub fn phone_normalize(value: &[u8]) -> Vec<u8>

Phone::normalize(): rawurldecode(), then a leading space (a + a query string decoded as a space) or no + at all before 7 to 15 digits becomes a +.

use utopia_validators::port;
Rust
pub fn port(value: Input<'_>) -> bool

A port from 1 to 65535, optionally after an IPv4 address or a bracketed IPv6 one and a colon (127.0.0.1:80, [::1]:443). An int is checked as its decimal string.

PHP: Port::isValid().

use utopia_validators::port_host;
Rust
pub fn port_host(host: &[u8]) -> bool

An IPv4 address, or an IPv6 address in brackets.

PHP: Port::isValidHost() (private).

use utopia_validators::port_number;
Rust
pub fn port_number(port: &[u8]) -> bool

Decimal digits without a leading zero, from 1 to 65535.

PHP: Port::isValidPort() (private).

use utopia_validators::preg_quote;
Rust
pub fn preg_quote(value: &[u8]) -> Vec<u8>

PHP preg_quote($value, '~').

use utopia_validators::private_use_scheme;
Rust
pub fn private_use_scheme(value: &[u8]) -> bool

URL::isPrivateUseSchemeURI() on a string: Uri\Rfc3986\Uri::parse() succeeds, its (normalized) scheme contains a . and it has no host.

use utopia_validators::range;
Rust
pub fn range(min: Number, max: Number, format: &[u8], value: Input<'_>) -> bool

A number from min to max, both included, of format (see range_number).

PHP: Range::isValid().

use utopia_validators::range_description;
Rust
pub fn range_description(min: Number, max: Number, out: &mut Vec<u8>)

Value must be a valid range between 1 and 10, both bounds as number_format() prints them.

PHP: Range::getDescription(). Appended to out.

use utopia_validators::range_number;
Rust
pub fn range_number(format: &[u8], value: Input<'_>) -> Option<Number>

The number Range::isValid() compares with its bounds, for a value that passed is_numeric(): as 'integer', an int (from + 0, so a numeric string that makes one) or a float that already is INF/-INF (PHP checks $value === INF before + 0, so a string such as '1e400' that overflows to INF is not one); as 'double', any number as a float; None when the value is not one, and for any other format.

use utopia_validators::text_description;
Rust
pub fn text_description(
    length: i64,
    min: i64,
    allow_list: Option<&[u8]>,
    require_non_blank: bool,
    message: &mut Vec<u8>,
)

Text::getDescription(), given implode(', ', $this->allowList) when the list is not empty.

Appended to message.

use utopia_validators::text_length;
Rust
pub fn text_length(value: &[u8], min: i64, length: i64) -> bool

mb_strlen($value) >= $min and, for a $length other than 0, mb_strlen($value) <= $length.

PHP: Text::isValid()'s length checks.

use utopia_validators::trim;
Rust
pub fn trim(value: &[u8]) -> &[u8]

PHP trim($value): the default characters (" \t\n\r\0\x0B") from both ends.

use utopia_validators::url;
Rust
pub fn url(value: &Value) -> bool

Whether filter_var($value, FILTER_VALIDATE_URL) is not false.

use utopia_validators::url_ascii;
Rust
pub fn url_ascii(value: &[u8]) -> Cow<'_, [u8]>

URL::isValid()'s IRI step: each byte above 127 as rawurlencode() encodes it.

use utopia_validators::url_component;
Rust
pub fn url_component(value: &[u8], component: Component) -> Option<Option<Vec<u8>>>

parse_url($value, $component) for a string component: None where PHP returns false (a malformed URL), Some(None) for null (no such component).

use utopia_validators::url_description;
Rust
pub fn url_description(
    schemes: Option<&[u8]>,
    allow_fragments: bool,
    https_or_loopback: bool,
    description: &mut Vec<u8>,
)

URL::getDescription(), given implode(', ', $allowedSchemes) when there are schemes.

Appended to description.

use utopia_validators::url_str;
Rust
pub fn url_str(value: &[u8]) -> bool

url of a string: the filter on the string as it is.

use utopia_validators::white_list_description;
Rust
pub fn white_list_description(list: &[u8], message: &mut Vec<u8>)

WhiteList::getDescription(), given implode(', ', $this->list), appended to message.

use utopia_validators::wildcard;
Rust
pub fn wildcard(pattern: &[u8]) -> bool

Whether a Globstar pattern holds a wildcard (*, ? or [).

Rust
pub const ASSOC_DESCRIPTION: &str = "Value must be a valid object.";

Assoc::getDescription().

Rust
pub const DOMAIN_DESCRIPTION: &str = "Value must be a valid domain";

Domain::getDescription().

Rust
pub const FCM_DESCRIPTION: &str = "Value must be valid Google service account JSON for FCM";

FCM::getDescription() before a check has failed.

Rust
pub const FCM_EMAIL_ERROR: &str = "FCM service account JSON field 'client_email' must contain a valid email address.";

The error for a client_email filter_var() refuses.

Rust
pub const FCM_KEY_ERROR: &str = "FCM service account JSON field 'private_key' must contain a PEM-encoded private key.";

The error for a private_key that is not PEM.

Rust
pub const FCM_OPTIONAL: [&str; 6] = _;

FCM::OPTIONAL_FIELDS.

Rust
pub const FCM_REQUIRED: [(&str, &str); 5] = _;

FCM::REQUIRED_FIELDS: each field and what it is for, in PHP's order.

Rust
pub const FCM_TYPE_ERROR: &str = "FCM service account JSON field 'type' must be 'service_account'.";

The error for a type other than service_account.

Rust
pub const FCM_URL: [&str; 4] = _;

FCM::URL_FIELDS.

Rust
pub const GLOBSTAR_DESCRIPTION: &str = "Value must match a specific inclusion, or a wildcard inclusion not overridden by any exclusion.";

Globstar::getDescription().

Rust
pub const HOST_DESCRIPTION_PREFIX: &str = "URL host must be one of: ";

What Host::getDescription() puts before implode(', ', $whitelist).

Rust
pub const HOSTNAME_DESCRIPTION: &str = "Value must be a valid hostname without path, port and protocol.";

Hostname::getDescription().

Rust
pub const IP_DESCRIPTION: &str = "Value must be a valid IP address";

IP::getDescription().

Rust
pub const JSON_DESCRIPTION: &str = "Value must be a valid JSON string";

JSON::getDescription().

Rust
pub const LABEL_DESCRIPTION: &str = "Value must be 1 to 63 lowercase letters, digits and hyphens, and must not start or end with a hyphen.";

Label::getDescription(): Label::MAX_LENGTH is LABEL_MAX_LENGTH.

Rust
pub const LABEL_MAX_LENGTH: i64 = 63i64;

Label::MAX_LENGTH: the longest DNS label.

Rust
pub const LOOPBACK_HOSTS: [&[u8]; 3] = _;

The hosts URL's httpsOrLoopback lets plain http reach.

Rust
pub const PEM_BEGIN: &str = "-----BEGIN PRIVATE KEY-----";

The first line of a PEM private key.

Rust
pub const PEM_END: &str = "-----END PRIVATE KEY-----";

The last line of a PEM private key.

Rust
pub const PHONE_DESCRIPTION: &str = "Phone number must start with a '+' and contain between 7 and 15 digits.";

Phone::getDescription().

Rust
pub const WHITE_LIST_DESCRIPTION: (&str, &str) = _;

What WhiteList::getDescription() puts around implode(', ', $this->list).