Rust

Validators

Rust
The checks of Utopia's request validators: booleans, integers, floats, numbers, ranges, ports, hex colors, text, identifiers, white lists, substrings, DNS labels, hostnames, domains, hosts, IP addresses, URLs, phone numbers, JSON, FCM service accounts, globstar patterns and the validators that compose others.

In Appwrite#

Appwrite runs the Rust crate: the PHP extension serves the library and none of its PHP is left.

32 PHP classes served from Rust
Rust 4,431 lines (2,851 code)· PHP 26 lines (10 code)split
PartCodeCommentsBlankTotalFiles
Rust source2,8511,0585224,43116
PHP source10124261

Comments include doc comments. Each side counts its src/; the PHP side is what the package still has in PHP.

Served from Rust (32)
Utopia\Validator\AllOfUtopia\Validator\AnyOfUtopia\Validator\ArrayListUtopia\Validator\AssocUtopia\Validator\BooleanUtopia\Validator\ContainsUtopia\Validator\DomainUtopia\Validator\FloatValidatorUtopia\Validator\GlobstarUtopia\Validator\HexColorUtopia\Validator\HostUtopia\Validator\HostnameUtopia\Validator\IPUtopia\Validator\IdentifierUtopia\Validator\IntegerUtopia\Validator\JSONUtopia\Validator\JSON\ArrayValidatorUtopia\Validator\JSON\FCMUtopia\Validator\JSON\ObjectValidatorUtopia\Validator\LabelUtopia\Validator\MultipleUtopia\Validator\NoneOfUtopia\Validator\NullableUtopia\Validator\NumericUtopia\Validator\PhoneUtopia\Validator\PortUtopia\Validator\RangeUtopia\Validator\TextUtopia\Validator\URLUtopia\Validator\ValidatorUtopia\Validator\WhiteListUtopia\Validator\Wildcard

Install#

Shell
cargo add utopia-validators

Overview#

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

A Rust caller that wants the value back in its type, not a bool, uses a [Validator]: Boolean, Integer, Float, Range, Port, Text, Hostname and WhiteList, which run the same checks.

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.

API#

What utopia_validators exports at its root.

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 `[`).