Contents
use utopia_validators::Boolean;pub struct Boolean;true, false, 1 or 0, as a boolean or as text.
PHP: new Boolean(loose: true).
use utopia_validators::Float;pub struct Float;Any number, as an int, a float or text.
PHP: new FloatValidator(loose: true).
use utopia_validators::Hostname;pub struct Hostname;A hostname: no path, port or protocol, and at most 253 characters.
PHP: new Hostname().
use utopia_validators::Integer;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;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;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;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;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;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;pub enum IntegerError {
Bits,
Unsigned64,
}Why new Integer() refuses its arguments; IntegerError::message is the
InvalidArgumentException's.
use utopia_validators::IpType;pub enum IpType {
All,
V4,
V6,
}The IP versions IP accepts: IP::ALL, IP::V4 or IP::V6.
use utopia_validators::Validator;pub trait ValidatorA check of one value that returns it in the type it was checked as.
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");use Utopia\Validator\Port;
$port = new Port();
var_dump($port->isValid('6379')); // true
var_dump($port->isValid(70000)); // falseuse utopia_validators::array_list_description;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;pub fn ascii_lower(value: &[u8]) -> Vec<u8>PHP strtolower(): ASCII only, whatever the locale (PHP 8.2 and later).
use utopia_validators::boolean;pub fn boolean(loose: bool, value: Input<'_>) -> boolA boolean; when loose, also 'true', 'false', '1', '0', 1 and 0.
PHP: Boolean::isValid().
use utopia_validators::chars;pub fn chars(value: &[u8]) -> i64PHP mb_strlen($value): each sequence that is not UTF-8 counts as one character.
use utopia_validators::contains;pub fn contains(haystack: &[u8], needle: &[u8]) -> boolPHP str_contains($haystack, $needle): an empty needle is in every haystack.
use utopia_validators::contains_description;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;pub fn domain(value: &[u8], hostnames: bool) -> boolDomain::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;pub fn exclusion(pattern: &[u8]) -> boolWhether a Globstar pattern is an exclusion (!…).
use utopia_validators::explode_count;pub fn explode_count(value: &[u8], limit: i64) -> i64count(explode('.', $value, $limit)).
use utopia_validators::fcm_optional_error;pub fn fcm_optional_error(field: &str) -> StringThe error for an optional field given but not a non-empty string.
use utopia_validators::fcm_required_error;pub fn fcm_required_error(field: &str, purpose: &str) -> StringThe error FCM::isValid() records for a required field that is missing or blank.
use utopia_validators::fcm_url_error;pub fn fcm_url_error(field: &str) -> StringThe error for a URL field that is not an HTTPS URL.
use utopia_validators::float;pub fn float(loose: bool, value: Input<'_>) -> boolAn int or a float; when loose, anything is_numeric().
PHP: FloatValidator::isValid().
use utopia_validators::globstar_regex;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;pub fn hex_color(value: Input<'_>) -> boolA 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;pub fn host_description(whitelist: &[u8], description: &mut Vec<u8>)Host::getDescription(), given implode(', ', $whitelist), appended to description.
use utopia_validators::hostname;pub fn hostname(value: &[u8]) -> boolThe 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;pub fn hostname_allowed(value: &[u8], allowed: &[u8]) -> boolWhether 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;pub fn https_scheme(scheme: &[u8]) -> boolPHP strtolower($scheme) === 'https' (ASCII lowercasing, as PHP 8 does).
use utopia_validators::identifier;pub fn identifier(value: &[u8]) -> boolpreg_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;pub fn identifier_description(length: i64, description: &mut Vec<u8>)Identifier::getDescription(), appended to description.
use utopia_validators::ip;pub fn ip(value: &Value, kind: IpType) -> boolIP::isValid(): filter_var($value, FILTER_VALIDATE_IP[, flag]) is truthy.
use utopia_validators::ip_str;pub fn ip_str(value: &[u8], kind: IpType) -> boolip 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;pub fn is_blank(value: &[u8]) -> boolpreg_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;pub fn json_array_description(length: i64, message: &mut Vec<u8>)JSON\ArrayValidator::getDescription(), appended to message.
use utopia_validators::json_length;pub fn json_length(length: i64, encoded: Option<&[u8]>) -> boolhasValidLength() of the JSON validators: encoded is None for false.
use utopia_validators::json_object_description;pub fn json_object_description(length: i64, message: &mut Vec<u8>)JSON\ObjectValidator::getDescription(), appended to message.
use utopia_validators::label;pub fn label(value: &[u8]) -> boolLabel::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;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;pub fn non_empty(value: &[u8]) -> boolFCM::isNonEmptyString() of a string.
use utopia_validators::number_le;pub fn number_le(a: Number, b: Number) -> boolPHP $a <= $b on two numbers: exact between ints, as floats otherwise (NaN is never
smaller or equal).
use utopia_validators::numeric;pub fn numeric(value: Input<'_>) -> boolAnything is_numeric(): ints, floats (NAN and INF included) and numeric strings.
PHP: Numeric::isValid().
use utopia_validators::pem_private_key;pub fn pem_private_key(key: &[u8]) -> boolstr_starts_with($key, PEM_BEGIN) && str_ends_with(trim($key), PEM_END).
use utopia_validators::phone;pub fn phone(value: &[u8]) -> boolPhone::isValid()'s pattern, /^\+[1-9]\d{6,14}$/.
use utopia_validators::phone_normalize;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;pub fn port(value: Input<'_>) -> boolA 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;pub fn port_host(host: &[u8]) -> boolAn IPv4 address, or an IPv6 address in brackets.
PHP: Port::isValidHost() (private).
use utopia_validators::port_number;pub fn port_number(port: &[u8]) -> boolDecimal digits without a leading zero, from 1 to 65535.
PHP: Port::isValidPort() (private).
use utopia_validators::preg_quote;pub fn preg_quote(value: &[u8]) -> Vec<u8>PHP preg_quote($value, '~').
use utopia_validators::private_use_scheme;pub fn private_use_scheme(value: &[u8]) -> boolURL::isPrivateUseSchemeURI() on a string: Uri\Rfc3986\Uri::parse() succeeds, its
(normalized) scheme contains a . and it has no host.
use utopia_validators::range;pub fn range(min: Number, max: Number, format: &[u8], value: Input<'_>) -> boolA number from min to max, both included, of format (see range_number).
PHP: Range::isValid().
use utopia_validators::range_description;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;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;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;pub fn text_length(value: &[u8], min: i64, length: i64) -> boolmb_strlen($value) >= $min and, for a $length other than 0, mb_strlen($value) <= $length.
PHP: Text::isValid()'s length checks.
use utopia_validators::trim;pub fn trim(value: &[u8]) -> &[u8]PHP trim($value): the default characters (" \t\n\r\0\x0B") from both ends.
use utopia_validators::url;pub fn url(value: &Value) -> boolWhether filter_var($value, FILTER_VALIDATE_URL) is not false.
use utopia_validators::url_ascii;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;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;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;pub fn url_str(value: &[u8]) -> boolurl of a string: the filter on the string as it is.
use utopia_validators::white_list_description;pub fn white_list_description(list: &[u8], message: &mut Vec<u8>)WhiteList::getDescription(), given implode(', ', $this->list), appended to message.
use utopia_validators::wildcard;pub fn wildcard(pattern: &[u8]) -> boolWhether a Globstar pattern holds a wildcard (*, ? or [).
pub const ASSOC_DESCRIPTION: &str = "Value must be a valid object.";Assoc::getDescription().
pub const DOMAIN_DESCRIPTION: &str = "Value must be a valid domain";Domain::getDescription().
pub const FCM_DESCRIPTION: &str = "Value must be valid Google service account JSON for FCM";FCM::getDescription() before a check has failed.
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.
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.
pub const FCM_OPTIONAL: [&str; 6] = _;FCM::OPTIONAL_FIELDS.
pub const FCM_REQUIRED: [(&str, &str); 5] = _;FCM::REQUIRED_FIELDS: each field and what it is for, in PHP's order.
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.
pub const FCM_URL: [&str; 4] = _;FCM::URL_FIELDS.
pub const GLOBSTAR_DESCRIPTION: &str = "Value must match a specific inclusion, or a wildcard inclusion not overridden by any exclusion.";Globstar::getDescription().
pub const HOST_DESCRIPTION_PREFIX: &str = "URL host must be one of: ";What Host::getDescription() puts before implode(', ', $whitelist).
pub const HOSTNAME_DESCRIPTION: &str = "Value must be a valid hostname without path, port and protocol.";Hostname::getDescription().
pub const IP_DESCRIPTION: &str = "Value must be a valid IP address";IP::getDescription().
pub const JSON_DESCRIPTION: &str = "Value must be a valid JSON string";JSON::getDescription().
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.
pub const LABEL_MAX_LENGTH: i64 = 63i64;Label::MAX_LENGTH: the longest DNS label.
pub const LOOPBACK_HOSTS: [&[u8]; 3] = _;The hosts URL's httpsOrLoopback lets plain http reach.
pub const PEM_BEGIN: &str = "-----BEGIN PRIVATE KEY-----";The first line of a PEM private key.
pub const PEM_END: &str = "-----END PRIVATE KEY-----";The last line of a PEM private key.
pub const PHONE_DESCRIPTION: &str = "Phone number must start with a '+' and contain between 7 and 15 digits.";Phone::getDescription().
pub const WHITE_LIST_DESCRIPTION: (&str, &str) = _;What WhiteList::getDescription() puts around implode(', ', $this->list).