Skip to main content

Crate utopia_validators

Crate utopia_validators 

Source
Expand description

The checks of utopia-php/validators (packages/validators), as PHP runs them.

Each function is the body of a PHP validator’s method over a value as PHP sees it (Input): the same coercions (is_numeric(), $value + 0), the same comparisons and the same messages. The validator classes themselves, and the state their constructors keep, live in the PHP extension (crates/extension), which passes that state in.

PHPRust
Boolean::isValid()boolean
Integer::isValid(), getDescription(), getFormat()Integer
FloatValidator::isValid()float
Numeric::isValid()numeric
Range::isValid(), getDescription()range, range_description
Port::isValid()port
HexColor::isValid()hex_color
Text::isValid(), getDescription()is_blank, text_length, text_description
Identifier::isValid(), getDescription()identifier, identifier_description
WhiteList::__construct(), getDescription()ascii_lower, white_list_description
Contains::isValid(), getDescription()lower, contains, contains_description
Label::isValid(), getDescription()label, LABEL_DESCRIPTION
Hostname::isValid()hostname, hostname_allowed
Domain::isValid()domain, explode_count
IP::isValid()ip
URL::isValid(), getDescription(), isPrivateUseSchemeURI()url_ascii, url, url_component, private_use_scheme, url_description
Host::getDescription()host_description
Phone::isValid(), normalize()phone, phone_normalize
ArrayList::getDescription()array_list_description
JSON\ArrayValidator, JSON\ObjectValidator: getDescription(), hasValidLength()json_array_description, json_object_description, json_length
JSON\FCM::isValid()FCM_REQUIRED, FCM_OPTIONAL, FCM_URL, non_empty, pem_private_key, https_scheme
Globstar::matchGlobstar()globstar_regex, exclusion, wildcard

The composing validators (Nullable, ArrayList, AllOf, AnyOf, NoneOf, Multiple) mostly ask the validators they wrap, which the extension does through the engine, as do the PHP functions whose state is observable (json_decode(), json_encode(), preg_match(), filter_var()).

Comparing a value with the PHP values a validator holds (the loose in_array() of Text’s allow list and of WhiteList, (string) of a pattern, Domain’s restrictions) is left to the PHP engine.

Structs§

Integer
An integer of bits bits, signed or unsigned.

Enums§

FilterValue
A PHP value given to or returned by filter_var().
Input
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.
IntegerError
Why new Integer() refuses its arguments; IntegerError::message is the InvalidArgumentException’s.
IpType
The IP versions IP accepts: IP::ALL, IP::V4 or IP::V6.
Number
A PHP number after $value + 0.
UrlComponent
A PHP_URL_* selector for parse_url($url, $component).

Constants§

ASSOC_DESCRIPTION
Assoc::getDescription().
DOMAIN_DESCRIPTION
Domain::getDescription().
FCM_DESCRIPTION
FCM::getDescription() before a check has failed.
FCM_EMAIL_ERROR
The error for a client_email filter_var() refuses.
FCM_KEY_ERROR
The error for a private_key that is not PEM.
FCM_OPTIONAL
FCM::OPTIONAL_FIELDS.
FCM_REQUIRED
FCM::REQUIRED_FIELDS: each field and what it is for, in PHP’s order.
FCM_TYPE_ERROR
The error for a type other than service_account.
FCM_URL
FCM::URL_FIELDS.
GLOBSTAR_DESCRIPTION
Globstar::getDescription().
HOSTNAME_DESCRIPTION
Hostname::getDescription().
HOST_DESCRIPTION_PREFIX
What Host::getDescription() puts before implode(', ', $whitelist).
IP_DESCRIPTION
IP::getDescription().
JSON_DESCRIPTION
JSON::getDescription().
LABEL_DESCRIPTION
Label::getDescription(): Label::MAX_LENGTH is LABEL_MAX_LENGTH.
LABEL_MAX_LENGTH
Label::MAX_LENGTH: the longest DNS label.
LOOPBACK_HOSTS
The hosts URL’s httpsOrLoopback lets plain http reach.
PEM_BEGIN
The first line of a PEM private key.
PEM_END
The last line of a PEM private key.
PHONE_DESCRIPTION
Phone::getDescription().
WHITE_LIST_DESCRIPTION
What WhiteList::getDescription() puts around implode(', ', $this->list).

Functions§

array_list_description
ArrayList::getDescription(): item is the wrapped validator’s description, None when it is '' or '0' (which PHP leaves out).
ascii_lower
PHP strtolower(): ASCII only, whatever the locale (PHP 8.2 and later).
boolean
A boolean; when loose, also 'true', 'false', '1', '0', 1 and 0.
chars
PHP mb_strlen($value): each sequence that is not UTF-8 counts as one character.
contains
PHP 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 -.
exclusion
Whether a Globstar pattern is an exclusion (!…).
explode_count
count(explode('.', $value, $limit)).
fcm_optional_error
The error for an optional field given but not a non-empty string.
fcm_required_error
The error FCM::isValid() records for a required field that is missing or blank.
fcm_url_error
The error for a URL field that is not an HTTPS URL.
float
An int or a float; when loose, anything is_numeric().
globstar_regex
The regular expression Globstar::matchGlobstar() hands preg_match() for pattern, delimiters and anchors included (~^…$~).
hex_color
A 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.
hostname
The checks of Hostname::isValid() before its allow list: not '' or '0', at most 253 characters (mb_strlen()), no / and no :.
hostname_allowed
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).
https_scheme
PHP 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.)
lower
PHP 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_le
PHP $a <= $b on two numbers: exact between ints, as floats otherwise (NaN is never smaller or equal).
numeric
Anything 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 +.
port
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.
port_host
An IPv4 address, or an IPv6 address in brackets.
port_number
Decimal digits without a leading zero, from 1 to 65535.
preg_quote
PHP 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.
range
A 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_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.
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.
trim
PHP trim($value): the default characters (" \t\n\r\0\x0B") from both ends.
url
Whether 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.
wildcard
Whether a Globstar pattern holds a wildcard (*, ? or [).